EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_simple_container.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ simple container headers
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2008
25 #
26 # Contributors:
27 #
28 ###############################################################################
29 */
30 
31 
32 #ifndef EGS_SIMPLE_CONTAINER_
33 #define EGS_SIMPLE_CONTAINER_
34 
35 #include "egs_functions.h"
36 
44 template <class T> class EGS_EXPORT EGS_SimpleContainer {
45 
46 public:
47 
48  EGS_SimpleContainer() : n_have(0), n_tot(0), n_start(4), n_max(1000000) {};
49 
50  EGS_SimpleContainer(int size) : n_have(0), n_tot(size), n_start(4),
51  n_max(1000000) {
52  if (n_tot > 0) {
53  array = new T [n_tot];
54  }
55  else {
56  n_tot = 0;
57  }
58  };
59 
61  if (n_tot > 0) {
62  delete [] array;
63  }
64  };
65 
66  void add(const T &t) {
67  if (n_have >= n_tot) {
68  grow();
69  }
70  array[n_have++] = t;
71  };
72 
73  void clear() {
74  n_have = 0;
75  };
76 
77  T &operator[](int j) {
78  return array[j];
79  };
80 
81  const T &operator[](int j) const {
82  return array[j];
83  };
84 
85  void setNmax(int Nmax) {
86  if (Nmax > n_tot) {
87  n_max = Nmax;
88  }
89  };
90 
91  T &pop() {
92  return array[--n_have];
93  }
94 
95  unsigned int size() const {
96  return n_have;
97  };
98 
99  unsigned int maxSize() const {
100  return n_tot;
101  };
102 
103  unsigned int maxAllowedSize() const {
104  return n_max;
105  };
106 
107 
108 protected:
109 
110  void grow() {
111  if (n_tot > 0) {
112  int nnew = 2*n_tot;
113  if (nnew > n_max) {
114  nnew = n_max;
115  if (nnew <= n_tot) egsFatal("EGS_SimpleContainer::grow(): "
116  "reached maximum allowed size of %d\n",n_max);
117  }
118  T *tmp = new T [nnew];
119  for (int j=0; j<n_have; j++) {
120  tmp[j] = array[j];
121  }
122  delete [] array;
123  array = tmp;
124  n_tot = nnew;
125  }
126  else {
127  array = new T [n_start];
128  n_tot = n_start;
129  }
130  };
131 
132  int n_have;
133  int n_tot;
134  int n_start;
135  int n_max;
136  T *array;
137 
138 };
139 
140 #endif
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
Global egspp functions header file.
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.