OOFEM 3.0
Loading...
Searching...
No Matches
intarray.h
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#ifndef intarray_h
36#define intarray_h
37
38#include "oofemenv.h"
39#include "contextioresulttype.h"
40#include "contextmode.h"
41#include "error.h"
42
43#include <cstdio>
44#include <vector>
45#include <iosfwd>
46#include <initializer_list>
47
48namespace oofem {
49class DataStream;
50
63{
64private:
66 std::vector< int > values;
67
68public:
70
71 std::vector< int > :: iterator begin() { return this->values.begin(); }
72 std::vector< int > :: iterator end() { return this->values.end(); }
73 std::vector< int > :: const_iterator begin() const { return this->values.begin(); }
74 std::vector< int > :: const_iterator end() const { return this->values.end(); }
76
77 constexpr static int Dim = 1;
78 typedef int Scalar;
79
81 IntArray(int n = 0) : values(n) { }
83 IntArray(const IntArray &src) : values(src.values) { }
85 IntArray(IntArray &&src) noexcept : values(std::move(src.values)) { }
87 inline IntArray(std :: initializer_list< int >list) : values(list) { }
90
92 IntArray &operator = (const IntArray &src) { values = src.values; return *this; }
94 IntArray &operator = (IntArray &&src) noexcept { values = std::move(src.values); return *this; }
96 inline IntArray &operator = (std :: initializer_list< int >list) { values = list; return *this; }
97
104 inline int &at(std::size_t i)
105 {
106#ifndef NDEBUG
107 this->checkBounds(i);
108#endif
109 return values[ i - 1 ];
110 }
111
117 inline int at(std::size_t i) const
118 {
119#ifndef NDEBUG
120 this->checkBounds(i);
121#endif
122 return values[ i - 1 ];
123 }
124
130 inline int &operator() (std::size_t i) { return this->operator[](i); }
131 inline int &operator[] (std::size_t i)
132 {
133#ifndef NDEBUG
134 this->checkBounds(i + 1);
135#endif
136 return values[ i ];
137 }
138
144 inline const int &operator() (std::size_t i) const { return this->operator[](i); }
145 inline const int &operator[] (std::size_t i) const
146 {
147#ifndef NDEBUG
148 this->checkBounds(i + 1);
149#endif
150 return values[ i ];
151 }
152
159 void checkBounds(std::size_t i) const
160 {
161 if ( i <= 0 ) {
162 OOFEM_ERROR("array error on index : %ld <= 0", i);
163 } else if ( i > values.size()) {
164 OOFEM_ERROR("array error on index : %ld > %d", i, this->giveSize());
165 }
166 }
167
175 void resizeWithValues(int n, int allocChunk = 0);
181 void resize(int n);
185 void clear() { this->values.clear(); }
190 void preallocate(int futureSize);
195 void enumerate(int maxVal);
202 void followedBy(const IntArray &b, int allocChunk = 0);
209 void followedBy(int b, int allocChunk = 0);
211 int giveSize() const { return (int)values.size(); }
212 std::size_t size() const { return values.size(); }
217 bool isEmpty() const { return values.size() == 0; }
222 bool containsOnlyZeroes() const;
227 int findSorted(int value) const;
232 int minimum() const;
237 int maximum() const;
242 void findNonzeros(const IntArray &logical);
247 bool containsSorted(int value) const { return ( findSorted(value) > 0 ); }
248
255 void insertSorted(int value, int allocChunk = 0);
264 bool insertSortedOnce(int value, int allocChunk = 0);
271 void eraseSorted(int value);
281 int findCommonValuesSorted(const IntArray &iarray, IntArray &common, int allocChunk = 0) const;
282
288 int findFirstIndexOf(int value) const;
292 bool contains(int value) const { return ( findFirstIndexOf(value) > 0 ); }
298 void insertOnce(int p);
302 void sort();
309 void erase(int pos);
314 void add(int val);
315
317 void zero();
318
320 void printYourself() const;
321
323 void pY() const;
324
329 void printYourself(const std::string name) const;
330
336 void printYourselfToFile(const std::string filename, const bool showDimensions=true) const;
337
339 bool isAllFinite() const;
340
345 inline const int *givePointer() const { return values.data(); }
346 inline int *givePointer() { return values.data(); }
347
351 contextIOResultType storeYourself(DataStream &stream) const;
355 contextIOResultType restoreYourself(DataStream &stream);
360 int givePackSize(DataStream &buff) const;
361
362
363 friend std :: ostream &operator << ( std :: ostream & out, const IntArray & x );
364
365 std::vector<int> minusOne() const { std::vector<int> ret(values.size()); for(size_t i=0; i<values.size(); i++) ret[i]=values[i]-1; return ret; }
366};
367
368
369template< class operation > int
370quickSortPartition(IntArray &arry, int l, int r, operation op)
371{
372 int i = l - 1, j = r;
373 int v = arry.at(r);
374 int swap;
375
376 for ( ; ; ) {
377 while ( ( op(arry.at(++i), v) ) < 0 ) {
378 ;
379 }
380
381 while ( ( op( v, arry.at(--j) ) ) < 0 ) {
382 if ( j == l ) {
383 break;
384 }
385 }
386
387 if ( i >= j ) {
388 break;
389 }
390
391 swap = arry.at(i);
392 arry.at(i) = arry.at(j);
393 arry.at(j) = swap;
394 }
395
396 swap = arry.at(i);
397 arry.at(i) = arry.at(r);
398 arry.at(r) = swap;
399 return i;
400}
401
402
403
404template< class operation > void quickSort(IntArray &arry, int l, int r, operation op)
405{
406 if ( r <= l ) {
407 return;
408 }
409
410 int i = quickSortPartition(arry, l, r, op);
411 quickSort(arry, l, i - 1, op);
412 quickSort(arry, i + 1, r, op);
413}
414
415
423template< class operation > void sort(IntArray &arry, operation op) { quickSort(arry, 1, arry.giveSize(), op); }
424} // end namespace oofem
425#endif // intarray_h
std::vector< int > minusOne() const
Definition intarray.h:365
void checkBounds(std::size_t i) const
Definition intarray.h:159
IntArray(IntArray &&src) noexcept
Move constructor. Creates the array from another array.
Definition intarray.h:85
std::vector< int >::iterator end()
Definition intarray.h:72
std::vector< int >::const_iterator end() const
Definition intarray.h:74
bool containsSorted(int value) const
Definition intarray.h:247
IntArray(int n=0)
Constructor for sized array. Data is zeroed.
Definition intarray.h:81
int & operator[](std::size_t i)
Definition intarray.h:131
IntArray(std ::initializer_list< int >list)
Initializer list constructor.
Definition intarray.h:87
int findSorted(int value) const
Definition intarray.C:293
bool contains(int value) const
Definition intarray.h:292
int * givePointer()
Definition intarray.h:346
std::vector< int >::iterator begin()
Definition intarray.h:71
bool isEmpty() const
Definition intarray.h:217
const int * givePointer() const
Definition intarray.h:345
std::vector< int > values
Stored values.
Definition intarray.h:66
std::vector< int >::const_iterator begin() const
Definition intarray.h:73
~IntArray()
Destructor.
Definition intarray.h:89
int findFirstIndexOf(int value) const
Definition intarray.C:280
static constexpr int Dim
Definition intarray.h:77
IntArray(const IntArray &src)
Copy constructor. Creates the array from another array.
Definition intarray.h:83
std::size_t size() const
Definition intarray.h:212
int & at(std::size_t i)
Definition intarray.h:104
int at(std::size_t i) const
Definition intarray.h:117
int giveSize() const
Definition intarray.h:211
#define OOFEM_ERROR(...)
Definition error.h:79
void quickSort(IntArray &arry, int l, int r, operation op)
Definition intarray.h:404
int quickSortPartition(IntArray &arry, int l, int r, operation op)
Definition intarray.h:370
FloatMatrixF< N, M > zero()
Constructs a zero matrix (this is the default behavior when constructing a matrix,...
void sort(IntArray &arry, operation op)
Definition intarray.h:423
#define OOFEM_EXPORT
Definition oofemcfg.h:7

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