OOFEM 3.0
Loading...
Searching...
No Matches
intarrayf.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
53template< class T >
55{
56private:
59
60public:
62
63 T :: iterator begin() { return this->values.begin(); }
64 T :: iterator end() { return this->values.end(); }
65 T :: const_iterator begin() const { return this->values.begin(); }
66 T :: const_iterator end() const { return this->values.end(); }
68
70 IntArray(int n = 0) : values(n) { }
72 IntArray(const IntArray &src) : values(src.values) { }
74 IntArray(IntArray &&src) : values(std::move(src.values)) { }
76 inline IntArray(std :: initializer_list< int >list) : values(list) { }
78 ~IntArray() = default;
79
81 IntArray &operator = (const IntArray &src) { values = src.values; return *this; }
83 IntArray &operator = (IntArray &&src) { values = std::move(src.values); return *this; }
85 inline IntArray &operator = (std :: initializer_list< int >list) { values = list; return *this; }
86
93 inline int &at(int i)
94 {
95#ifndef NDEBUG
96 this->checkBounds(i);
97#endif
98 return values[ i - 1 ];
99 }
100
106 inline int at(int i) const
107 {
108#ifndef NDEBUG
109 this->checkBounds(i);
110#endif
111 return values[ i - 1 ];
112 }
113
119 inline int &operator() (int i) { return this->operator[](i); }
120 inline int &operator[] (int i)
121 {
122#ifndef NDEBUG
123 this->checkBounds(i + 1);
124#endif
125 return values[ i ];
126 }
127
133 inline const int &operator() (int i) const { return this->operator[](i); }
134 inline const int &operator[] (int i) const
135 {
136#ifndef NDEBUG
137 this->checkBounds(i + 1);
138#endif
139 return values[ i ];
140 }
141
148 void checkBounds(int i) const
149 {
150 if ( i <= 0 ) {
151 OOFEM_ERROR("array error on index : %d <= 0", i);
152 } else if ( i > this->giveSize() ) {
153 OOFEM_ERROR("array error on index : %d > %d", i, this->giveSize());
154 }
155 }
156
164 void resizeWithValues(int n, int allocChunk = 0);
170 void resize(int n);
174 void clear() { this->values.clear(); }
179 void preallocate(int futureSize);
184 void enumerate(int maxVal);
191 void followedBy(const IntArray &b, int allocChunk = 0);
198 void followedBy(int b, int allocChunk = 0);
200 int giveSize() const { return (int)values.size(); }
205 bool isEmpty() const { return values.size() == 0; }
210 bool containsOnlyZeroes() const;
215 int findSorted(int value) const;
220 int minimum() const;
225 int maximum() const;
230 void findNonzeros(const IntArray &logical);
235 bool containsSorted(int value) const { return ( findSorted(value) > 0 ); }
236
243 void insertSorted(int value, int allocChunk = 0);
252 bool insertSortedOnce(int value, int allocChunk = 0);
259 void eraseSorted(int value);
269 int findCommonValuesSorted(const IntArray &iarray, IntArray &common, int allocChunk = 0) const;
270
276 int findFirstIndexOf(int value) const;
280 bool contains(int value) const { return ( findFirstIndexOf(value) > 0 ); }
286 void insertOnce(int p);
290 void sort();
297 void erase(int pos);
302 void add(int val);
303
305 void zero();
306
308 void printYourself() const;
309
311 void pY() const;
312
317 void printYourself(const std::string name) const;
318
324 void printYourselfToFile(const std::string filename, const bool showDimensions=true) const;
325
327 bool isAllFinite() const;
328
333 inline const int *givePointer() const { return values.data(); }
334 inline int *givePointer() { return values.data(); }
335
348 int givePackSize(DataStream &buff) const;
349
350
351 friend std :: ostream &operator << ( std :: ostream & out, const IntArray & x );
352};
353
354
355template< class operation > int
356quickSortPartition(IntArray &arry, int l, int r, operation op)
357{
358 int i = l - 1, j = r;
359 int v = arry.at(r);
360 int swap;
361
362 for ( ; ; ) {
363 while ( ( op(arry.at(++i), v) ) < 0 ) {
364 ;
365 }
366
367 while ( ( op( v, arry.at(--j) ) ) < 0 ) {
368 if ( j == l ) {
369 break;
370 }
371 }
372
373 if ( i >= j ) {
374 break;
375 }
376
377 swap = arry.at(i);
378 arry.at(i) = arry.at(j);
379 arry.at(j) = swap;
380 }
381
382 swap = arry.at(i);
383 arry.at(i) = arry.at(r);
384 arry.at(r) = swap;
385 return i;
386}
387
388
389
390template< class operation > void quickSort(IntArray &arry, int l, int r, operation op)
391{
392 if ( r <= l ) {
393 return;
394 }
395
396 int i = quickSortPartition(arry, l, r, op);
397 quickSort(arry, l, i - 1, op);
398 quickSort(arry, i + 1, r, op);
399}
400
401
409template< class operation > void sort(IntArray &arry, operation op) { quickSort(arry, 1, arry.giveSize(), op); }
410} // end namespace oofem
411#endif // intarray_h
int giveSize() const
Definition intarrayf.h:200
void insertOnce(int p)
void printYourselfToFile(const std::string filename, const bool showDimensions=true) const
const int * givePointer() const
Definition intarrayf.h:333
void findNonzeros(const IntArray &logical)
bool containsOnlyZeroes() const
IntArray(std ::initializer_list< int >list)
Initializer list constructor.
Definition intarrayf.h:76
IntArray(const IntArray &src)
Copy constructor. Creates the array from another array.
Definition intarrayf.h:72
void enumerate(int maxVal)
int at(int i) const
Definition intarrayf.h:106
IntArray(int n=0)
Constructor for sized array. Data is zeroed.
Definition intarrayf.h:70
bool containsSorted(int value) const
Definition intarrayf.h:235
int findSorted(int value) const
T::iterator begin()
Definition intarrayf.h:63
int & operator[](int i)
Definition intarrayf.h:120
void printYourself() const
Prints receiver on stdout.
int findFirstIndexOf(int value) const
void resizeWithValues(int n, int allocChunk=0)
void preallocate(int futureSize)
int givePackSize(DataStream &buff) const
int maximum() const
contextIOResultType storeYourself(DataStream &stream) const
int minimum() const
void erase(int pos)
void resize(int n)
T::const_iterator end() const
Definition intarrayf.h:66
contextIOResultType restoreYourself(DataStream &stream)
T::iterator end()
Definition intarrayf.h:64
void zero()
Sets all component to zero.
bool insertSortedOnce(int value, int allocChunk=0)
void insertSorted(int value, int allocChunk=0)
~IntArray()=default
Destructor.
bool isEmpty() const
Definition intarrayf.h:205
void followedBy(int b, int allocChunk=0)
void checkBounds(int i) const
Definition intarrayf.h:148
int & at(int i)
Definition intarrayf.h:93
void printYourself(const std::string name) const
void pY() const
Abbreviation for printYourself().
int findCommonValuesSorted(const IntArray &iarray, IntArray &common, int allocChunk=0) const
void followedBy(const IntArray &b, int allocChunk=0)
bool contains(int value) const
Definition intarrayf.h:280
T values
Stored values.
Definition intarrayf.h:58
void add(int val)
bool isAllFinite() const
Returns true if no element is NAN or infinite.
int * givePointer()
Definition intarrayf.h:334
void eraseSorted(int value)
T::const_iterator begin() const
Definition intarrayf.h:65
IntArray(IntArray &&src)
Move constructor. Creates the array from another array.
Definition intarrayf.h:74
std::vector< int > values
Stored values.
Definition intarray.h:66
int & at(std::size_t i)
Definition intarray.h:104
#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
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