NEST  2.6.0,not_revisioned_source_dir@0
array.h
Go to the documentation of this file.
1 /*
2  * array.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef ARRAY_H
24 #define ARRAY_H
25 /*
26  Template for an array class.
27 */
28 
29 // special array. discuss vector and valarray implementations.
30 // specify!
31 //
32 // resizing controled from outside, especially shrinking
33 //
34 //
35 // p.47
36 //
37 //
38 #include <iostream>
39 #include <iomanip>
40 #include <cstddef>
41 #include <cassert>
42 #include <algorithm>
43 
44 template<class T>
45 class array
46 {
47  private:
48  T* p;
49  size_t n;
50 public:
51  array() : p(NULL), n(0) { };
52  array(size_t, const T& = T() );
53  array(const array<T>&);
54  ~array();
55 
56  T* begin(void)
57  {
58  return p;
59  }
60 
61  T* end(void)
62  {
63  return p+n;
64  }
65 
66  void resize(size_t, const T& = T());
67 
68  size_t size(void) const
69  {
70  return n;
71  }
72 
73  bool empty(void) const
74  {
75  return n==0;
76  }
77 
78  T& operator[](size_t);
79  const T& operator[](size_t) const;
80  void fill(const T&);
81 
82  array<T>& operator=(const array<T> &); // argument changed to const &
83 };
84 
85 
86 
87 
88 template<class T>
89 array<T>::array(size_t n_s, const T& t)
90  : n(n_s)
91 {
92 // p=new T[n](t); // yields warnings with -pedantic
93  p=new T[n];
94  assert(p!=NULL);
95  fill(t);
96 }
97 
98 template<class T>
100  :p(NULL), n(0)
101 {
102  if(a.p != NULL)
103  {
104  n=a.n;
105  p=new T[n];
106  assert(p!=NULL);
107 // for(size_t i=0; i<n; p[i]=a.p[i++]);
108  copy(&a.p[0],&a.p[n],&p[0]);
109  }
110 
111 }
112 
113 template<class T>
115 {
116  if (p!=NULL)
117  delete[] p;
118 }
119 
120 template<class T>
121 void array<T>::fill(const T& e)
122 {
123 // for(size_t i=0; i<n; p[i++]= e);
124  std::fill(&p[0],&p[n],e); // force global name
125 }
126 
127 
128 template<class T>
129 void array<T>::resize(size_t n_s, const T& t)
130 {
131  // T* h= new T[n_s](t); // yields warning with -pedantic
132  T* h= new T[n_s];
133  assert(h!=NULL);
134 
135  for(T *hi=h; hi <h+n_s; ++hi)
136  (*hi)=t;
137 
138  size_t min_l,max_l;
139 
140  if (n < n_s)
141  {
142  min_l = n; max_l = n_s;
143 // for(size_t i= n; i< n_s; h[i++] = T() ); // initialize rest
144 // ::fill(&h[0],&h[n_s],t);
145  }
146  else
147  {
148  min_l = n_s; max_l = n;
149  }
150 
151  if(p != NULL)
152  {
153 // for(size_t i=0; i< min_l; h[i]=p[i++]); // copy old parts
154  copy(&p[0],&p[min_l],&h[0]);
155  delete[] p;
156  }
157 
158  p=h;
159  n=n_s;
160 }
161 
162 template<class T>
164 {
165  assert(i<n);
166 
167  return p[i];
168 }
169 
170 template<class T>
171 const T& array<T>::operator[](size_t i) const
172 {
173  assert(i<n);
174 
175  return p[i];
176 }
177 
178 template<class T>
180 {
181  if (this!=&a)
182  {
183  if (p!=NULL)
184  {
185  delete[] p;
186  p = NULL; // all this make something
187  n = 0; // like a = array<int> work
188  }
189  if(a.p !=NULL)
190  {
191  n=a.n;
192  p=new T[n];
193  assert(p!=NULL);
194 // for(size_t i=0; i<n; p[i]=a.p[i++]);
195  copy(&a.p[0],&a.p[n],&p[0]);
196  }
197 
198  }
199 
200  return *this;
201 }
202 
203 
204 #endif
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
T * p
Definition: array.h:48
Definition: array.h:45
const Name a("a")
Specific to Brette & Gerstner 2005 (aeif_cond-*)
Definition: nest_names.h:41
void resize(size_t, const T &=T())
Definition: array.h:129
assert(pNet!=0)
const Name h("h")
Summed input to a neuron (Ginzburg neuron)
Definition: nest_names.h:158
size_t size(void) const
Definition: array.h:68
array< T > & operator=(const array< T > &)
Definition: array.h:179
bool empty(void) const
Definition: array.h:73
size_t n
Definition: array.h:49
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
array()
Definition: array.h:51
T * end(void)
Definition: array.h:61
void fill(const T &)
Definition: array.h:121
~array()
Definition: array.h:114
const Name p("p")
current release probability (Tsodyks2_connection)
Definition: nest_names.h:218
T & operator[](size_t)
Definition: array.h:163
const double e
Definition: numerics.cpp:62
T * begin(void)
Definition: array.h:56