OOFEM 3.0
Loading...
Searching...
No Matches
dictionary.C
Go to the documentation of this file.
1/*
2 *
3 * ##### ##### ###### ###### ### ###
4 * ## ## ## ## ## ## ## ### ##
5 * ## ## ## ## #### #### ## # ##
6 * ## ## ## ## ## ## ## ##
7 * ## ## ## ## ## ## ## ##
8 * ##### ##### ## ###### ## ##
9 *
10 *
11 * OOFEM : Object Oriented Finite Element Code
12 *
13 * Copyright (C) 1993 - 2025 Borek Patzak
14 *
15 *
16 *
17 * Czech Technical University, Faculty of Civil Engineering,
18 * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 */
34
35#include "dictionary.h"
36#include "logger.h"
37#include "datastream.h"
38#include "contextioerr.h"
39#include "contextmode.h"
40
41#include <cstdlib>
42#include <ostream>
43
44namespace oofem {
45Dictionary :: ~Dictionary()
46// Destructor.
47{
48 this->clear();
49}
50
51void
52Dictionary :: clear()
53{
54 Pair *Next;
55
56 while ( first ) {
57 Next = first->giveNext();
58 delete first;
59 first = Next;
60 }
61
62 first = NULL;
63 last = NULL;
64}
65
66int
67Dictionary :: giveSize()
68{
69 int size = 0;
70 Pair *next = first;
71 while ( next ) {
72 size++;
73 next = next->giveNext();
74 }
75
76 return size;
77}
78
79Pair *Dictionary :: add(int k, double v)
80// Adds the pair (k,v) to the receiver. Returns this new pair.
81{
82
83# ifdef DEBUG
84 if ( this->includes(k) ) {
85 OOFEM_ERROR("key (%d) already exists", k);
86 }
87
88# endif
89
90 Pair *newPair = new Pair(k, v);
91 if ( last ) {
92 last->append(newPair);
93 } else { // empty dictionary
94 first = newPair;
95 }
96
97 last = newPair;
98
99 return newPair;
100}
101
102
103double &Dictionary :: at(int aKey)
104// Returns the value of the pair which key is aKey. If such pair does
105// not exist, creates it and assign value 0.
106{
107 Pair *next = first;
108 while ( next ) {
109 if ( next->giveKey() == aKey ) {
110 return next->giveValue();
111 }
112
113 next = next->giveNext();
114 }
115
116 Pair *newPair = this->add(aKey, 0); // pair does not exist yet
117 return newPair->giveValue();
118}
119
120
121double Dictionary :: at(int aKey) const
122{
123 Pair *next = first;
124 while ( next ) {
125 if ( next->giveKey() == aKey ) {
126 return next->giveValue();
127 }
128
129 next = next->giveNext();
130 }
131 OOFEM_ERROR("Requested key missing from dictionary");
132}
133
134
135bool Dictionary :: includes(int aKey) const
136// Returns True if the receiver contains a pair which key is aKey, else
137// returns False.
138{
139 Pair *next = first;
140 while ( next ) {
141 if ( next->giveKey() == aKey ) {
142 return true;
143 }
144
145 next = next->giveNext();
146 }
147
148 return false;
149}
150
151
152void Dictionary :: printYourself()
153// Prints the receiver on screen.
154{
155 Pair *next;
156
157 printf("Dictionary : \n");
158
159 next = first;
160 while ( next ) {
161 next->printYourself();
162 next = next->giveNext();
163 }
164}
165
166
167void
168Dictionary :: formatAsString(std :: string &str)
169{
170 Pair *next;
171 char buffer [ 64 ];
172
173 next = first;
174 while ( next ) {
175 sprintf( buffer, " %c %e", next->giveKey(), next->giveValue() );
176 str += buffer;
177 next = next->giveNext();
178 }
179}
180
181
182void Dictionary :: saveContext(DataStream &stream)
183{
184 int nitems = 0;
185 int key;
186 double value;
187 Pair *next;
188
189 next = first;
190 while ( next ) {
191 nitems++;
192 next = next->giveNext();
193 }
194
195 // write size
196 if ( !stream.write(nitems) ) {
198 }
199
200 // write raw data
201 next = first;
202 while ( next ) {
203 key = next->giveKey();
204 value = next->giveValue();
205 if ( !stream.write(key) ) {
207 }
208
209 if ( !stream.write(value) ) {
211 }
212
213 next = next->giveNext();
214 }
215}
216
217
218void Dictionary :: restoreContext(DataStream &stream)
219{
220 int size;
221 int key;
222 double value;
223
224 // delete currently occupied space
225 this->clear();
226
227 // read size
228 if ( !stream.read(size) ) {
230 }
231
232 // read particular pairs
233 for ( int i = 1; i <= size; i++ ) {
234 if ( !stream.read(key) ) {
236 }
237
238 if ( !stream.read(value) ) {
240 }
241
242 this->at(key) = value;
243 }
244}
245
246
247std :: ostream &operator << ( std :: ostream & out, const Dictionary & r )
248{
249 int count = 0;
250 Pair *next = r.first;
251 while ( next ) {
252 count++;
253 next = next->giveNext();
254 }
255
256 out << count;
257 next = r.first;
258 while ( next ) {
259 out << " " << next->giveKey() << " " << next->giveValue();
260 next = next->giveNext();
261 }
262 return out;
263}
264} // end namespace oofem
virtual int read(int *data, std::size_t count)=0
Reads count integer values into array pointed by data.
virtual int write(const int *data, std::size_t count)=0
Writes count integer values from array pointed by data.
Dictionary()
Constructor, creates empty dictionary.
Definition dictionary.h:68
void clear()
Clears the receiver.
Definition dictionary.C:52
double & at(int aKey)
Definition dictionary.C:103
bool includes(int aKey) const
Definition dictionary.C:135
Pair * first
First pair.
Definition dictionary.h:62
Pair * add(int aKey, double value)
Definition dictionary.C:79
Pair * last
Last pair.
Definition dictionary.h:64
Pair * giveNext()
Returns pointer to the next pair.
Definition pair.h:72
int giveKey()
Returns the receiver key.
Definition pair.h:70
double & giveValue()
Returns associated value.
Definition pair.h:74
void printYourself()
Prints receiver to screen.
Definition pair.h:76
#define THROW_CIOERR(e)
#define OOFEM_ERROR(...)
Definition error.h:79
std::ostream & operator<<(std ::ostream &out, const Dictionary &r)
Definition dictionary.C:247
@ CIO_IOERR
General IO error.

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011