OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
intarray.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 "intarray.h"
36 #include "error.h"
37 #include "datastream.h"
38 
39 #include <cstdarg>
40 #include <cstdlib>
41 #include <cstring>
42 #include <algorithm>
43 #include <memory>
44 #include <numeric>
45 #include <cmath>
46 #include <iostream>
47 #include <fstream>
48 #include <iomanip>
49 
50 namespace oofem {
51 
53 {
54  std::fill(values.begin(), values.end(), 0);
55 }
56 
57 
58 void IntArray :: add(int value)
59 {
60  for (int &x: values) x += value;
61 }
62 
63 
64 #ifdef DEBUG
65 int &IntArray :: at(int i)
66 {
67  this->checkBounds(i);
68  return values [ i - 1 ];
69 }
70 
71 int IntArray :: at(int i) const
72 {
73  this->checkBounds(i);
74  return values [ i - 1 ];
75 }
76 
77 int &IntArray :: operator()(int i)
78 {
79  this->checkBounds(i);
80  return values [ i ];
81 }
82 
83 const int &IntArray :: operator()(int i) const
84 {
85  this->checkBounds(i);
86  return values [ i ];
87 }
88 
89 int &IntArray :: operator[](int i)
90 {
91  this->checkBounds(i);
92  return values [ i ];
93 }
94 
95 const int &IntArray :: operator[](int i) const
96 {
97  this->checkBounds(i);
98  return values [ i ];
99 }
100 
101 void IntArray :: checkBounds(int i) const
102 // Checks that the receiver includes an index i.
103 {
104  if ( i < 0 ) {
105  OOFEM_ERROR("array error on index : %d < 0", i);
106  }
107 
108  if ( i > this->giveSize() ) {
109  OOFEM_ERROR("array error on index : %d > %d", i, this->giveSize());
110  }
111 }
112 #endif
113 
114 
115 void IntArray :: resizeWithValues(int n, int allocChunk)
116 {
117  if ( allocChunk > 0 && (int)this->values.capacity() < n ) {
118  this->values.reserve(n + allocChunk);
119  }
120  this->values.resize(n);
121 }
122 
123 
125 {
126  this->values.assign(n, 0);
127 }
128 
129 
130 void IntArray :: preallocate(int futureSize)
131 {
132  values.reserve(futureSize);
133 }
134 
135 
136 void IntArray :: enumerate(int maxValue)
137 {
138  this->values.resize(maxValue);
139  for ( int i = 1; i <= maxValue; ++i ) {
140  this->at(i) = i;
141  }
142 }
143 
144 
145 void IntArray :: followedBy(const IntArray &b, int allocChunk)
146 {
147  if ( allocChunk && (int)values.capacity() < this->giveSize() + b.giveSize() ) {
148  values.reserve(values.capacity() + allocChunk + b.giveSize());
149  }
150  values.insert(values.end(), b.values.begin(), b.values.end());
151 }
152 
153 
154 void IntArray :: followedBy(int b, int allocChunk)
155 {
156  if ( allocChunk && (int)values.capacity() < this->giveSize() + 1 ) {
157  values.reserve(values.capacity() + allocChunk + 1);
158  }
159  values.push_back(b);
160 }
161 
162 
163 void IntArray :: erase(int _pos)
164 {
165 #ifdef DEBUG
166  this->checkBounds(_pos);
167 #endif
168  values.erase(values.begin() + _pos - 1);
169 }
170 
171 
173 {
174  for ( auto x: values ) {
175  if ( x ) {
176  return false;
177  }
178  }
179 
180  return true;
181 }
182 
183 
185 {
186 #ifdef DEBUG
187  if ( this->isEmpty() ) {
188  OOFEM_ERROR("Empty array.");
189  }
190 #endif
191  return *std::min_element(values.begin(), values.end());
192 }
193 
194 
196 {
197 #ifdef DEBUG
198  if ( this->isEmpty() ) {
199  OOFEM_ERROR("Empty array.");
200  }
201 #endif
202  return *std::max_element(values.begin(), values.end());
203 }
204 
205 
206 void IntArray :: findNonzeros(const IntArray &logical)
207 {
208  int newsize = 0;
209  for ( const int &x: logical.values) {
210  if ( x ) {
211  ++newsize;
212  }
213  }
214  this->values.resize(newsize);
215 
216  int pos = 1;
217  for ( int i = 1; i <= logical.giveSize(); ++i ) {
218  if ( logical.at(i) ) {
219  this->at(pos++) = i;
220  }
221  }
222 }
223 
224 
226 // Prints the receiver on screen.
227 {
228  printf("IntArray of size : %d\n", this->giveSize());
229  for ( int i = 1; i <= this->giveSize(); ++i ) {
230  if ( i > 42 ) {
231  printf(" (other components not printed)");
232  break;
233  } else {
234  printf( "%d ", this->at(i) );
235  }
236  }
237 
238  printf("\n");
239 }
240 
241 
242 void IntArray :: printYourself(const std::string name) const
243 // Prints the receiver on screen.
244 {
245  printf("%s (%d): ", name.c_str(), this->giveSize());
246  for ( int i = 1; i <= this->giveSize(); ++i ) {
247  if ( i > 42 ) {
248  printf(" (other components not printed)");
249  break;
250  } else {
251  printf( "%d ", this->at(i) );
252  }
253  }
254 
255  printf("\n");
256 }
257 
259 {
260  for(int val : values) {
261  if(!std::isfinite((double)val)) {
262  return false;
263  }
264  }
265 
266  return true;
267 }
268 
269 void IntArray :: pY() const {
270  printYourself();
271 }
272 
273 void IntArray :: printYourselfToFile(const std::string filename, const bool showDimensions) const
274 // Prints the receiver to file.
275 {
276  std :: ofstream arrayfile (filename);
277  if (arrayfile.is_open()) {
278  if (showDimensions)
279  arrayfile << "IntArray of size : " << this->giveSize() << "\n";
280  for ( int x: *this ) {
281  arrayfile << x << "\t";
282  }
283  arrayfile.close();
284  } else {
285  OOFEM_ERROR("Failed to write to file");
286  }
287 }
288 
290 {
291  // write size
292  if ( !stream.write(this->giveSize()) ) {
293  return ( CIO_IOERR );
294  }
295 
296  // write raw data
297  if ( !stream.write(values.data(), this->giveSize()) ) {
298  return ( CIO_IOERR );
299  }
300 
301  // return result back
302  return CIO_OK;
303 }
304 
306 {
307  // read size
308  int size;
309  if ( !stream.read(size) ) {
310  return ( CIO_IOERR );
311  }
312 
313  values.resize(size);
314 
315  // read raw data
316  if ( !stream.read(values.data(), size) ) {
317  return ( CIO_IOERR );
318  }
319 
320  // return result back
321  return CIO_OK;
322 }
323 
324 
326 {
327  return buff.givePackSizeOfInt(1) + buff.givePackSizeOfInt(this->giveSize());
328 }
329 
330 
331 int IntArray :: findFirstIndexOf(int value) const
332 {
333  // finds index of value in receiver
334  auto it = std::find(values.begin(), values.end(), value);
335  // if such value does not exists, returns zero index
336  if ( it == values.end() ) {
337  return 0;
338  } else {
339  return (int)(it - values.begin() + 1);
340  }
341 }
342 
343 
344 int IntArray :: findSorted(int _val) const
345 {
346  return std::binary_search (values.begin(), values.end(), _val);
347 }
348 
349 
350 void IntArray :: insertSorted(int val, int allocChunk)
351 {
352  if ( allocChunk > 0 && values.size() + 1 >= values.capacity() ) {
353  values.reserve(allocChunk + values.capacity());
354  }
355  auto low = std::lower_bound(values.begin(), values.end(), val);
356  values.insert(low, val);
357 }
358 
359 
360 bool IntArray :: insertSortedOnce(int val, int allocChunk)
361 {
362  if ( allocChunk > 0 && values.size() + 1 >= values.capacity() ) {
363  values.reserve(allocChunk + values.capacity());
364  }
365  auto low = std::lower_bound(values.begin(), values.end(), val);
366  if ( low == values.end() || *low != val ) {
367  values.insert(low, val);
368  return true;
369  }
370  return false;
371 }
372 
373 
374 void IntArray :: eraseSorted(int value)
375 {
376  auto low = std::lower_bound(values.begin(), values.end(), value);
377  if ( *low == value ) {
378  values.erase(low);
379  }
380 }
381 
382 
383 int IntArray :: findCommonValuesSorted(const IntArray &iarray, IntArray &common, int allocChunk) const
384 {
385  int i = 0;
386 
387  for ( int val: iarray ) {
388 
389  while ( i < this->giveSize() ) {
390  if ( values [ i ] == val ) {
391  common.followedBy(val, allocChunk);
392  i++;
393  break;
394  }
395 
396  if ( values [ i ] > val ) {
397  break;
398  }
399 
400  i++;
401  }
402 
403  if ( i == this->giveSize() ) {
404  break;
405  }
406  }
407 
408  return ( common.giveSize() );
409 }
410 
411 
413 {
414  if ( !this->findFirstIndexOf(_p) ) {
415  this->followedBy(_p, 2);
416  }
417 }
418 
419 
421 {
422  std::sort(this->begin(), this->end());
423 }
424 
425 
426 std :: ostream &operator<<(std :: ostream &out, const IntArray &x)
427 {
428  out << x.giveSize();
429  for ( const int &val: x ) {
430  out << " " << val;
431  }
432  return out;
433 }
434 } // end namespace oofem
void eraseSorted(int value)
Erase the element of given value.
Definition: intarray.C:374
contextIOResultType storeYourself(DataStream &stream) const
Stores array to output stream.
Definition: intarray.C:289
void enumerate(int maxVal)
Resizes receiver and enumerates from 1 to the maximum value given.
Definition: intarray.C:136
int & operator[](int i)
Definition: intarray.h:142
std::vector< int > values
Stored values.
Definition: intarray.h:65
void printYourself() const
Prints receiver on stdout.
Definition: intarray.C:225
void erase(int pos)
Erase the element at given position (1-based index) Receiver will shrink accordingly, the values at positions (pos+1,...,size) will be moved to positions (pos,...,size-1)
Definition: intarray.C:163
The purpose of DataStream abstract class is to allow to store/restore context to different streams...
Definition: datastream.h:54
bool isEmpty() const
Checks if receiver is empty (i.e., zero sized).
Definition: intarray.h:208
void zero()
Sets all component to zero.
Definition: intarray.C:52
int minimum() const
Finds the minimum component in the array.
Definition: intarray.C:184
int givePackSize(DataStream &buff) const
Returns how much space is needed to pack receivers message.
Definition: intarray.C:325
bool insertSortedOnce(int value, int allocChunk=0)
Inserts given value into a receiver, which is assumed to be sorted.
Definition: intarray.C:360
std::vector< int >::iterator end()
Definition: intarray.h:71
General IO error.
void findNonzeros(const IntArray &logical)
Finds all indices where the input array is nonzero.
Definition: intarray.C:206
Class implementing an array of integers.
Definition: intarray.h:61
int & at(int i)
Coefficient access function.
Definition: intarray.h:103
virtual int read(int *data, int count)=0
Reads count integer values into array pointed by data.
friend std::ostream & operator<<(std::ostream &out, const IntArray &x)
Definition: intarray.C:426
void sort(IntArray &arry, operation op)
Sorts the receiver using quicksort algorithm.
Definition: intarray.h:416
void sort()
Sorts array.
Definition: intarray.C:420
bool containsOnlyZeroes() const
Checks if receiver is all zero.
Definition: intarray.C:172
void printYourselfToFile(const std::string filename, const bool showDimensions=true) const
Print receiver to file.
Definition: intarray.C:273
virtual int write(const int *data, int count)=0
Writes count integer values from array pointed by data.
bool isFinite() const
Returns true if no element is NAN or infinite.
Definition: intarray.C:258
void insertSorted(int value, int allocChunk=0)
Inserts given value into a receiver, which is assumed to be sorted.
Definition: intarray.C:350
void insertOnce(int p)
Insert once (does not make any assumption about receiver state or ordering, quite inefficient)...
Definition: intarray.C:412
#define OOFEM_ERROR(...)
Definition: error.h:61
int maximum() const
Finds the maximum component in the array.
Definition: intarray.C:195
void resizeWithValues(int n, int allocChunk=0)
Checks size of receiver towards requested bounds.
Definition: intarray.C:115
void resize(int n)
Checks size of receiver towards requested bounds.
Definition: intarray.C:124
int & operator()(int i)
Coefficient access function.
Definition: intarray.h:125
contextIOResultType restoreYourself(DataStream &stream)
Restores array from image on stream.
Definition: intarray.C:305
void add(int val)
Adds given scalar to all values of receiver.
Definition: intarray.C:58
int findSorted(int value) const
Finds the first occurrence of given value, assuming that the receiver is sorted.
Definition: intarray.C:344
void pY() const
Abbreviation for printYourself().
Definition: intarray.C:269
void followedBy(const IntArray &b, int allocChunk=0)
Appends array b at the end of receiver.
Definition: intarray.C:145
std::vector< int >::iterator begin()
Definition: intarray.h:70
int giveSize() const
Definition: intarray.h:203
void preallocate(int futureSize)
Preallocates receiver to given futureSize if larger then allocatedSize.
Definition: intarray.C:130
the oofem namespace is to define a context or scope in which all oofem names are defined.
int findCommonValuesSorted(const IntArray &iarray, IntArray &common, int allocChunk=0) const
Extracts common values in receiver and iarray.
Definition: intarray.C:383
int findFirstIndexOf(int value) const
Finds index of first occurrence of given value in array.
Definition: intarray.C:331
virtual int givePackSizeOfInt(int count)=0

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:29 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011