OOFEM 3.0
Loading...
Searching...
No Matches
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 - 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 "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
50namespace oofem {
51
52void IntArray :: zero()
53{
54 std::fill(values.begin(), values.end(), 0);
55}
56
57
58void IntArray :: add(int value)
59{
60 for (int &x: values) x += value;
61}
62
63
64void IntArray :: resizeWithValues(int n, int allocChunk)
65{
66 if ( allocChunk > 0 && (int)this->values.capacity() < n ) {
67 this->values.reserve(n + allocChunk);
68 }
69 this->values.resize(n);
70}
71
72
73void IntArray :: resize(int n)
74{
75 this->values.assign(n, 0);
76}
77
78
79void IntArray :: preallocate(int futureSize)
80{
81 values.reserve(futureSize);
82}
83
84
85void IntArray :: enumerate(int maxValue)
86{
87 this->values.resize(maxValue);
88 for ( int i = 1; i <= maxValue; ++i ) {
89 this->at(i) = i;
90 }
91}
92
93
94void IntArray :: followedBy(const IntArray &b, int allocChunk)
95{
96 if ( allocChunk && (int)values.capacity() < this->giveSize() + b.giveSize() ) {
97 values.reserve(values.capacity() + allocChunk + b.giveSize());
98 }
99 values.insert(values.end(), b.values.begin(), b.values.end());
100}
101
102
103void IntArray :: followedBy(int b, int allocChunk)
104{
105 if ( allocChunk && (int)values.capacity() < this->giveSize() + 1 ) {
106 values.reserve(values.capacity() + allocChunk + 1);
107 }
108 values.push_back(b);
109}
110
111
112void IntArray :: erase(int _pos)
113{
114#ifndef NDEBUG
115 this->checkBounds(_pos);
116#endif
117 values.erase(values.begin() + _pos - 1);
118}
119
120
121bool IntArray :: containsOnlyZeroes() const
122{
123 for ( auto x: values ) {
124 if ( x ) {
125 return false;
126 }
127 }
128
129 return true;
130}
131
132
133int IntArray :: minimum() const
134{
135#ifndef NDEBUG
136 if ( this->isEmpty() ) {
137 OOFEM_ERROR("Empty array.");
138 }
139#endif
140 return *std::min_element(values.begin(), values.end());
141}
142
143
144int IntArray :: maximum() const
145{
146#ifndef NDEBUG
147 if ( this->isEmpty() ) {
148 OOFEM_ERROR("Empty array.");
149 }
150#endif
151 return *std::max_element(values.begin(), values.end());
152}
153
154
155void IntArray :: findNonzeros(const IntArray &logical)
156{
157 int newsize = 0;
158 for ( const int &x: logical.values) {
159 if ( x ) {
160 ++newsize;
161 }
162 }
163 this->values.resize(newsize);
164
165 int pos = 1;
166 for ( int i = 1; i <= logical.giveSize(); ++i ) {
167 if ( logical.at(i) ) {
168 this->at(pos++) = i;
169 }
170 }
171}
172
173
174void IntArray :: printYourself() const
175// Prints the receiver on screen.
176{
177 printf("IntArray of size : %d\n", this->giveSize());
178 for ( int i = 1; i <= this->giveSize(); ++i ) {
179 if ( i > 42 ) {
180 printf(" (other components not printed)");
181 break;
182 } else {
183 printf( "%d ", this->at(i) );
184 }
185 }
186
187 printf("\n");
188}
189
190
191void IntArray :: printYourself(const std::string name) const
192// Prints the receiver on screen.
193{
194 printf("%s (%d): ", name.c_str(), this->giveSize());
195 for ( int i = 1; i <= this->giveSize(); ++i ) {
196 if ( i > 42 ) {
197 printf(" (other components not printed)");
198 break;
199 } else {
200 printf( "%d ", this->at(i) );
201 }
202 }
203
204 printf("\n");
205}
206
207bool IntArray :: isAllFinite() const
208{
209 for ( int val : values ) {
210 if( !std::isfinite((double)val) ) {
211 return false;
212 }
213 }
214
215 return true;
216}
217
218void IntArray :: pY() const {
220}
221
222void IntArray :: printYourselfToFile(const std::string filename, const bool showDimensions) const
223// Prints the receiver to file.
224{
225 std :: ofstream arrayfile (filename);
226 if (arrayfile.is_open()) {
227 if (showDimensions)
228 arrayfile << "IntArray of size : " << this->giveSize() << "\n";
229 for ( int x: *this ) {
230 arrayfile << x << "\t";
231 }
232 arrayfile.close();
233 } else {
234 OOFEM_ERROR("Failed to write to file");
235 }
236}
237
238contextIOResultType IntArray :: storeYourself(DataStream &stream) const
239{
240 // write size
241 if ( !stream.write(this->size()) ) {
242 return ( CIO_IOERR );
243 }
244
245 // write raw data
246 if ( !stream.write(values.data(), this->size()) ) {
247 return ( CIO_IOERR );
248 }
249
250 // return result back
251 return CIO_OK;
252}
253
254contextIOResultType IntArray :: restoreYourself(DataStream &stream)
255{
256 // read size
257 std::size_t size;
258 if ( !stream.read(size) ) {
259 return ( CIO_IOERR );
260 }
261
262 values.resize(size);
263
264 // read raw data
265 if ( !stream.read(values.data(), size) ) {
266 return ( CIO_IOERR );
267 }
268
269 // return result back
270 return CIO_OK;
271}
272
273
274int IntArray :: givePackSize(DataStream &buff) const
275{
276 return buff.givePackSizeOfSizet(1) + buff.givePackSizeOfInt(this->giveSize());
277}
278
279
280int IntArray :: findFirstIndexOf(int value) const
281{
282 // finds index of value in receiver
283 auto it = std::find(values.begin(), values.end(), value);
284 // if such value does not exists, returns zero index
285 if ( it == values.end() ) {
286 return 0;
287 } else {
288 return (int)(it - values.begin() + 1);
289 }
290}
291
292
293int IntArray :: findSorted(int _val) const
294{
295 return std::binary_search (values.begin(), values.end(), _val);
296}
297
298
299void IntArray :: insertSorted(int val, int allocChunk)
300{
301 if ( allocChunk > 0 && values.size() + 1 >= values.capacity() ) {
302 values.reserve(allocChunk + values.capacity());
303 }
304 auto low = std::lower_bound(values.begin(), values.end(), val);
305 values.insert(low, val);
306}
307
308
309bool IntArray :: insertSortedOnce(int val, int allocChunk)
310{
311 if ( allocChunk > 0 && values.size() + 1 >= values.capacity() ) {
312 values.reserve(allocChunk + values.capacity());
313 }
314 auto low = std::lower_bound(values.begin(), values.end(), val);
315 if ( low == values.end() || *low != val ) {
316 values.insert(low, val);
317 return true;
318 }
319 return false;
320}
321
322
323void IntArray :: eraseSorted(int value)
324{
325 auto low = std::lower_bound(values.begin(), values.end(), value);
326 if ( *low == value ) {
327 values.erase(low);
328 }
329}
330
331
332int IntArray :: findCommonValuesSorted(const IntArray &iarray, IntArray &common, int allocChunk) const
333{
334 int i = 0;
335
336 for ( int val: iarray ) {
337
338 while ( i < this->giveSize() ) {
339 if ( values [ i ] == val ) {
340 common.followedBy(val, allocChunk);
341 i++;
342 break;
343 }
344
345 if ( values [ i ] > val ) {
346 break;
347 }
348
349 i++;
350 }
351
352 if ( i == this->giveSize() ) {
353 break;
354 }
355 }
356
357 return ( common.giveSize() );
358}
359
360
361void IntArray :: insertOnce(int _p)
362{
363 if ( !this->findFirstIndexOf(_p) ) {
364 this->followedBy(_p, 2);
365 }
366}
367
368
369void IntArray :: sort()
370{
371 std::sort(this->begin(), this->end());
372}
373
374
375std :: ostream &operator<<(std :: ostream &out, const IntArray &x)
376{
377 out << x.giveSize();
378 for ( const int &val: x ) {
379 out << " " << val;
380 }
381 return out;
382}
383} // end namespace oofem
virtual int read(int *data, std::size_t count)=0
Reads count integer values into array pointed by data.
virtual int givePackSizeOfSizet(std::size_t count)=0
virtual int write(const int *data, std::size_t count)=0
Writes count integer values from array pointed by data.
virtual int givePackSizeOfInt(std::size_t count)=0
void checkBounds(std::size_t i) const
Definition intarray.h:159
std::vector< int >::iterator end()
Definition intarray.h:72
void followedBy(const IntArray &b, int allocChunk=0)
Definition intarray.C:94
IntArray(int n=0)
Constructor for sized array. Data is zeroed.
Definition intarray.h:81
std::vector< int >::iterator begin()
Definition intarray.h:71
bool isEmpty() const
Definition intarray.h:217
std::vector< int > values
Stored values.
Definition intarray.h:66
void printYourself() const
Prints receiver on stdout.
Definition intarray.C:174
int findFirstIndexOf(int value) const
Definition intarray.C:280
std::size_t size() const
Definition intarray.h:212
int & at(std::size_t i)
Definition intarray.h:104
int giveSize() const
Definition intarray.h:211
#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