OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
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 - 2013 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 
44 namespace oofem {
46 // Destructor.
47 {
48  this->clear();
49 }
50 
51 void
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 
66 int
68 {
69  Pair *next;
70  int size = 0;
71 
72  next = first;
73  while ( next ) {
74  size++;
75  next = next->giveNext();
76  }
77 
78  return size;
79 }
80 
81 Pair *Dictionary :: add(int k, double v)
82 // Adds the pair (k,v) to the receiver. Returns this new pair.
83 {
84  Pair *newPair;
85 
86 # ifdef DEBUG
87  if ( this->includes(k) ) {
88  OOFEM_ERROR("key (%d) already exists", k);
89  }
90 
91 # endif
92 
93  newPair = new Pair(k, v);
94  if ( last ) {
95  last->append(newPair);
96  } else { // empty dictionary
97  first = newPair;
98  }
99 
100  last = newPair;
101 
102  return newPair;
103 }
104 
105 
106 double &Dictionary :: at(int aKey)
107 // Returns the value of the pair which key is aKey. If such pair does
108 // not exist, creates it and assign value 0.
109 {
110  Pair *next, *newPair;
111 
112  next = first;
113  while ( next ) {
114  if ( next->giveKey() == aKey ) {
115  return next->giveValue();
116  }
117 
118  next = next->giveNext();
119  }
120 
121  newPair = this->add(aKey, 0); // pair does not exist yet
122  return newPair->giveValue();
123 }
124 
125 
127 // Returns True if the receiver contains a pair which key is aKey, else
128 // returns False.
129 {
130  Pair *next;
131 
132  next = first;
133  while ( next ) {
134  if ( next->giveKey() == aKey ) {
135  return true;
136  }
137 
138  next = next->giveNext();
139  }
140 
141  return false;
142 }
143 
144 
146 // Prints the receiver on screen.
147 {
148  Pair *next;
149 
150  printf("Dictionary : \n");
151 
152  next = first;
153  while ( next ) {
154  next->printYourself();
155  next = next->giveNext();
156  }
157 }
158 
159 
160 void
161 Dictionary :: formatAsString(std :: string &str)
162 {
163  Pair *next;
164  char buffer [ 64 ];
165 
166  next = first;
167  while ( next ) {
168  sprintf( buffer, " %c %e", next->giveKey(), next->giveValue() );
169  str += buffer;
170  next = next->giveNext();
171  }
172 }
173 
174 
176 //
177 // saves full node context (saves state variables, that completely describe
178 // current state)
179 //
180 {
181  int nitems = 0;
182  int key;
183  double value;
184  Pair *next;
185 
186  next = first;
187  while ( next ) {
188  nitems++;
189  next = next->giveNext();
190  }
191 
192  // write size
193  if ( !stream.write(nitems) ) {
195  }
196 
197  // write raw data
198  next = first;
199  while ( next ) {
200  key = next->giveKey();
201  value = next->giveValue();
202  if ( !stream.write(key) ) {
204  }
205 
206  if ( !stream.write(value) ) {
208  }
209 
210  next = next->giveNext();
211  }
212 
213  // return result back
214  return CIO_OK;
215 }
216 
217 
219 //
220 // restores full node context (saves state variables, that completely describe
221 // current state)
222 //
223 {
224  int size;
225  int key;
226  double value;
227 
228  // delete currently occupied space
229  this->clear();
230 
231  // read size
232  if ( !stream.read(size) ) {
234  }
235 
236  // read particular pairs
237  for ( int i = 1; i <= size; i++ ) {
238  if ( !stream.read(key) ) {
240  }
241 
242  if ( !stream.read(value) ) {
244  }
245 
246  this->at(key) = value;
247  }
248 
249  return CIO_OK;
250 }
251 
252 
253 std :: ostream &operator << ( std :: ostream & out, const Dictionary & r )
254 {
255  int count = 0;
256  Pair *next = r.first;
257  while ( next ) {
258  count++;
259  next = next->giveNext();
260  }
261 
262  out << count;
263  next = r.first;
264  while ( next ) {
265  out << " " << next->giveKey() << " " << next->giveValue();
266  next = next->giveNext();
267  }
268  return out;
269 }
270 } // end namespace oofem
int giveKey()
Returns the receiver key.
Definition: pair.h:70
void printYourself()
Prints the receiver on screen.
Definition: dictionary.C:145
int giveSize()
Returns number of pairs of receiver.
Definition: dictionary.C:67
Pair * last
Last pair.
Definition: dictionary.h:64
double & at(int aKey)
Returns the value of the pair which key is aKey.
Definition: dictionary.C:106
The purpose of DataStream abstract class is to allow to store/restore context to different streams...
Definition: datastream.h:54
friend std::ostream & operator<<(std::ostream &out, const Dictionary &r)
Definition: dictionary.C:253
bool includes(int aKey)
Checks if dictionary includes given key.
Definition: dictionary.C:126
contextIOResultType saveContext(DataStream &stream, ContextMode mode, void *obj=NULL)
Saves the receiver contends (state) to given stream.
Definition: dictionary.C:175
void formatAsString(std::string &str)
Formats itself as string.
Definition: dictionary.C:161
General IO error.
Pair * giveNext()
Returns pointer to the next pair.
Definition: pair.h:72
virtual int read(int *data, int count)=0
Reads count integer values into array pointed by data.
#define THROW_CIOERR(e)
Definition: contextioerr.h:61
double & giveValue()
Returns associated value.
Definition: pair.h:74
virtual int write(const int *data, int count)=0
Writes count integer values from array pointed by data.
#define OOFEM_ERROR(...)
Definition: error.h:61
void printYourself()
Prints receiver to screen.
Definition: pair.h:76
void clear()
Clears the receiver.
Definition: dictionary.C:52
Pair * add(int aKey, double value)
Adds a new Pair with given keyword and value into receiver.
Definition: dictionary.C:81
This class implements a linked list whose entries are Pairs (see below).
Definition: dictionary.h:58
long ContextMode
Context mode (mask), defining the type of information written/read to/from context.
Definition: contextmode.h:43
This class implements key/value associations - the key and its associated value.
Definition: pair.h:51
void append(Pair *p)
Appends a given pair to itself (sets the pointer to next pair to given Pair).
Definition: pair.h:68
Pair * first
First pair.
Definition: dictionary.h:62
the oofem namespace is to define a context or scope in which all oofem names are defined.
contextIOResultType restoreContext(DataStream &stream, ContextMode mode, void *obj=NULL)
Restores the receiver contents (state) from given stream.
Definition: dictionary.C:218
~Dictionary()
Destructor.
Definition: dictionary.C:45

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:28 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011