OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
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 - 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 #ifndef intarray_h
36 #define intarray_h
37 
38 #include "oofemcfg.h"
39 #include "contextioresulttype.h"
40 #include "contextmode.h"
41 
42 #include <cstdio>
43 #include <vector>
44 #include <iosfwd>
45 #include <initializer_list>
46 
47 namespace oofem {
48 class DataStream;
49 
61 class OOFEM_EXPORT IntArray
62 {
63 private:
65  std::vector< int > values;
66 
67 public:
69 
70  std::vector< int > :: iterator begin() { return this->values.begin(); }
71  std::vector< int > :: iterator end() { return this->values.end(); }
72  std::vector< int > :: const_iterator begin() const { return this->values.begin(); }
73  std::vector< int > :: const_iterator end() const { return this->values.end(); }
75 
77  IntArray(int n = 0) : values(n) { }
79  IntArray(const IntArray &src) : values(src.values) { }
81  IntArray(IntArray &&src) : values(std::move(src.values)) { }
83  inline IntArray(std :: initializer_list< int >list) : values(list) { }
85  ~IntArray() {};
86 
88  IntArray &operator = (const IntArray &src) { values = src.values; return *this; }
90  IntArray &operator = (IntArray &&src) { values = std::move(src.values); return *this; }
92  inline IntArray &operator = (std :: initializer_list< int >list) { values = list; return *this; }
93 
100 #ifdef DEBUG
101  int &at(int i);
102 #else
103  inline int &at(int i) { return values [ i - 1 ]; }
104 #endif
105 
111 #ifdef DEBUG
112  int at(int i) const;
113 #else
114  inline int at(int i) const { return values [ i - 1 ]; }
115 #endif
116 
122 #ifdef DEBUG
123  int &operator() (int i);
124 #else
125  inline int &operator() (int i) { return values [ i ]; }
126 #endif
127 
133 #ifdef DEBUG
134  const int &operator() (int i) const;
135 #else
136  inline const int &operator() (int i) const { return values [ i ]; }
137 #endif
138 
139 #ifdef DEBUG
140  int &operator[] ( int i );
141 #else
142  inline int &operator[] ( int i ) { return values [ i ]; }
143 #endif
144 
145 #ifdef DEBUG
146  const int &operator[] ( int i ) const;
147 #else
148  inline const int &operator[] ( int i ) const { return values [ i ]; }
149 #endif
150 
151 #ifdef DEBUG
152 
158  void checkBounds(int i) const;
159 #endif
160 
167  void resizeWithValues(int n, int allocChunk = 0);
173  void resize(int n);
177  void clear() { this->values.clear(); }
182  void preallocate(int futureSize);
187  void enumerate(int maxVal);
194  void followedBy(const IntArray &b, int allocChunk = 0);
201  void followedBy(int b, int allocChunk = 0);
203  int giveSize() const { return (int)values.size(); }
208  bool isEmpty() const { return values.size() == 0; }
213  bool containsOnlyZeroes() const;
218  int findSorted(int value) const;
223  int minimum() const;
228  int maximum() const;
233  void findNonzeros(const IntArray &logical);
238  bool containsSorted(int value) const { return ( findSorted(value) > 0 ); }
239 
246  void insertSorted(int value, int allocChunk = 0);
255  bool insertSortedOnce(int value, int allocChunk = 0);
262  void eraseSorted(int value);
272  int findCommonValuesSorted(const IntArray &iarray, IntArray &common, int allocChunk = 0) const;
273 
279  int findFirstIndexOf(int value) const;
283  bool contains(int value) const { return ( findFirstIndexOf(value) > 0 ); }
289  void insertOnce(int p);
293  void sort();
300  void erase(int pos);
305  void add(int val);
306 
308  void zero();
309 
311  void printYourself() const;
312 
314  void pY() const;
315 
320  void printYourself(const std::string name) const;
321 
327  void printYourselfToFile(const std::string filename, const bool showDimensions=true) const;
328 
330  bool isFinite() const;
331 
336  inline const int *givePointer() const { return values.data(); }
337  inline int *givePointer() { return values.data(); }
338 
342  contextIOResultType storeYourself(DataStream &stream) const;
346  contextIOResultType restoreYourself(DataStream &stream);
351  int givePackSize(DataStream &buff) const;
352 
353 
354  friend std :: ostream &operator << ( std :: ostream & out, const IntArray & x );
355 
356 #ifdef BOOST_PYTHON
357  void __setitem__(int i, int val) { this->at(i + 1) = val; }
358  int __getitem__(int i) { return this->at(i + 1); }
359  void beCopyOf(const IntArray &src) { this->operator = ( src ); }
360 #endif
361 };
362 
363 
364 template< class operation > int
365 quickSortPartition(IntArray &arry, int l, int r, operation op) {
366  int i = l - 1, j = r;
367  int v = arry.at(r);
368  int swap;
369 
370  for ( ; ; ) {
371  while ( ( op(arry.at(++i), v) ) < 0 ) {
372  ;
373  }
374 
375  while ( ( op( v, arry.at(--j) ) ) < 0 ) {
376  if ( j == l ) {
377  break;
378  }
379  }
380 
381  if ( i >= j ) {
382  break;
383  }
384 
385  swap = arry.at(i);
386  arry.at(i) = arry.at(j);
387  arry.at(j) = swap;
388  }
389 
390  swap = arry.at(i);
391  arry.at(i) = arry.at(r);
392  arry.at(r) = swap;
393  return i;
394 }
395 
396 
397 
398 template< class operation > void quickSort(IntArray &arry, int l, int r, operation op) {
399  if ( r <= l ) {
400  return;
401  }
402 
403  int i = quickSortPartition(arry, l, r, op);
404  quickSort(arry, l, i - 1, op);
405  quickSort(arry, i + 1, r, op);
406 }
407 
408 
416 template< class operation > void sort(IntArray &arry, operation op) { quickSort(arry, 1, arry.giveSize(), op); }
417 } // end namespace oofem
418 #endif // intarray_h
bool contains(int value) const
Definition: intarray.h:283
std::vector< int > values
Stored values.
Definition: intarray.h:65
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
int at(int i) const
Coefficient access function.
Definition: intarray.h:114
IntArray(std::initializer_list< int >list)
Initializer list constructor.
Definition: intarray.h:83
std::vector< int >::iterator end()
Definition: intarray.h:71
std::vector< int >::const_iterator end() const
Definition: intarray.h:73
const int * givePointer() const
Breaks encapsulation.
Definition: intarray.h:336
IntArray(IntArray &&src)
Move constructor. Creates the array from another array.
Definition: intarray.h:81
Class implementing an array of integers.
Definition: intarray.h:61
int & at(int i)
Coefficient access function.
Definition: intarray.h:103
void sort(IntArray &arry, operation op)
Sorts the receiver using quicksort algorithm.
Definition: intarray.h:416
int * givePointer()
Definition: intarray.h:337
~IntArray()
Destructor.
Definition: intarray.h:85
std::ostream & operator<<(std::ostream &out, const Dictionary &r)
Definition: dictionary.C:253
void clear()
Clears the array (zero size).
Definition: intarray.h:177
void quickSort(IntArray &arry, int l, int r, operation op)
Definition: intarray.h:398
int quickSortPartition(IntArray &arry, int l, int r, operation op)
Definition: intarray.h:365
IntArray(const IntArray &src)
Copy constructor. Creates the array from another array.
Definition: intarray.h:79
IntArray(int n=0)
Constructor for sized array. Data is zeroed.
Definition: intarray.h:77
std::vector< int >::iterator begin()
Definition: intarray.h:70
std::vector< int >::const_iterator begin() const
Definition: intarray.h:72
int giveSize() const
Definition: intarray.h:203
the oofem namespace is to define a context or scope in which all oofem names are defined.
bool containsSorted(int value) const
Checks if sorted receiver contains a given value.
Definition: intarray.h:238

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