FreeFOAM The Cross-Platform CFD Toolkit
FixedListI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include <OpenFOAM/UList.H>
27 #include <OpenFOAM/SLList.H>
28 #include <OpenFOAM/contiguous.H>
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class T, unsigned Size>
34 {}
35 
36 
37 template<class T, unsigned Size>
39 {
40  for (register unsigned i=0; i<Size; i++)
41  {
42  v_[i] = v[i];
43  }
44 }
45 
46 
47 template<class T, unsigned Size>
49 {
50  for (register unsigned i=0; i<Size; i++)
51  {
52  v_[i] = t;
53  }
54 }
55 
56 
57 template<class T, unsigned Size>
59 {
60  checkSize(lst.size());
61 
62  for (register unsigned i=0; i<Size; i++)
63  {
64  v_[i] = lst[i];
65  }
66 }
67 
68 
69 template<class T, unsigned Size>
71 {
72  checkSize(lst.size());
73 
74  register label i = 0;
75  for
76  (
77  typename SLList<T>::const_iterator iter = lst.begin();
78  iter != lst.end();
79  ++iter
80  )
81  {
82  operator[](i++) = iter();
83  }
84 }
85 
86 
87 template<class T, unsigned Size>
89 {
90  for (register unsigned i=0; i<Size; i++)
91  {
92  v_[i] = lst[i];
93  }
94 }
95 
96 
97 template<class T, unsigned Size>
100 {
101  return autoPtr< FixedList<T, Size> >(new FixedList<T, Size>(*this));
102 }
103 
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
107 template<class T, unsigned Size>
109 {
110  return *reinterpret_cast< FixedList<T, Size>* >(0);
111 }
112 
113 
114 template<class T, unsigned Size>
115 inline Foam::label Foam::FixedList<T, Size>::fcIndex(const label i) const
116 {
117  return (i == Size-1 ? 0 : i+1);
118 }
119 
120 
121 template<class T, unsigned Size>
122 inline Foam::label Foam::FixedList<T, Size>::rcIndex(const label i) const
123 {
124  return (i ? i-1 : Size-1);
125 }
126 
127 
128 // Check start is within valid range (0 ... size-1).
129 template<class T, unsigned Size>
130 inline void Foam::FixedList<T, Size>::checkStart(const label start) const
131 {
132  if (start < 0 || (start && unsigned(start) >= Size))
133  {
134  FatalErrorIn("FixedList<T, Size>::checkStart(const label)")
135  << "start " << start << " out of range 0 ... " << (Size-1)
136  << abort(FatalError);
137  }
138 }
139 
140 
141 // Check size is within valid range (0 ... size).
142 template<class T, unsigned Size>
143 inline void Foam::FixedList<T, Size>::checkSize(const label size) const
144 {
145  if (size < 0 || unsigned(size) > Size)
146  {
147  FatalErrorIn("FixedList<T, Size>::checkSize(const label)")
148  << "size " << size << " out of range 0 ... " << (Size)
149  << abort(FatalError);
150  }
151 }
152 
153 
154 // Check index i is within valid range (0 ... size-1)
155 // The check for zero-sized list is already done in static assert
156 template<class T, unsigned Size>
157 inline void Foam::FixedList<T, Size>::checkIndex(const label i) const
158 {
159  if (i < 0 || unsigned(i) >= Size)
160  {
161  FatalErrorIn("FixedList<T, Size>::checkIndex(const label)")
162  << "index " << i << " out of range 0 ... " << (Size-1)
163  << abort(FatalError);
164  }
165 }
166 
167 
168 template<class T, unsigned Size>
169 inline void Foam::FixedList<T, Size>::resize(const label s)
170 {
171 # ifdef FULLDEBUG
172  checkSize(s);
173 # endif
174 }
175 
176 template<class T, unsigned Size>
177 inline void Foam::FixedList<T, Size>::setSize(const label s)
178 {
179 # ifdef FULLDEBUG
180  checkSize(s);
181 # endif
182 }
183 
184 template<class T, unsigned Size>
186 {
187  for (register unsigned i=0; i<Size; i++)
188  {
189  v_[i] = lst[i];
190  }
191 }
192 
193 
194 template<class T, unsigned Size>
195 inline const T*
197 {
198  return v_;
199 }
200 
201 
202 template<class T, unsigned Size>
203 inline T*
205 {
206  return v_;
207 }
208 
209 
210 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
211 
212 // element access
213 template<class T, unsigned Size>
215 {
216 # ifdef FULLDEBUG
217  checkIndex(i);
218 # endif
219  return v_[i];
220 }
221 
222 
223 // const element access
224 template<class T, unsigned Size>
225 inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
226 {
227 # ifdef FULLDEBUG
228  checkIndex(i);
229 # endif
230  return v_[i];
231 }
232 
233 
234 template<class T, unsigned Size>
235 inline void Foam::FixedList<T, Size>::operator=(const T lst[Size])
236 {
237  for (register unsigned i=0; i<Size; i++)
238  {
239  v_[i] = lst[i];
240  }
241 }
242 
243 template<class T, unsigned Size>
245 {
246  checkSize(lst.size());
247 
248  for (register unsigned i=0; i<Size; i++)
249  {
250  v_[i] = lst[i];
251  }
252 }
253 
254 template<class T, unsigned Size>
256 {
257  checkSize(lst.size());
258 
259  register label i = 0;
260  for
261  (
262  typename SLList<T>::const_iterator iter = lst.begin();
263  iter != lst.end();
264  ++iter
265  )
266  {
267  operator[](i++) = iter();
268  }
269 }
270 
271 template<class T, unsigned Size>
273 {
274  for (register unsigned i=0; i<Size; i++)
275  {
276  v_[i] = t;
277  }
278 }
279 
280 
281 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
282 
283 template<class T, unsigned Size>
286 {
287  return v_;
288 }
289 
290 
291 template<class T, unsigned Size>
294 {
295  return v_;
296 }
297 
298 
299 template<class T, unsigned Size>
302 {
303  return v_;
304 }
305 
306 
307 template<class T, unsigned Size>
310 {
311  return &v_[Size];
312 }
313 
314 
315 template<class T, unsigned Size>
318 {
319  return &v_[Size];
320 }
321 
322 
323 template<class T, unsigned Size>
326 {
327  return &v_[Size];
328 }
329 
330 
331 template<class T, unsigned Size>
334 {
335  return &v_[Size-1];
336 }
337 
338 
339 template<class T, unsigned Size>
342 {
343  return &v_[Size-1];
344 }
345 
346 
347 template<class T, unsigned Size>
350 {
351  return &v_[Size-1];
352 }
353 
354 
355 template<class T, unsigned Size>
358 {
359  return &v_[-1];
360 }
361 
362 
363 template<class T, unsigned Size>
366 {
367  return &v_[-1];
368 }
369 
370 
371 template<class T, unsigned Size>
374 {
375  return &v_[-1];
376 }
377 
378 
379 template<class T, unsigned Size>
380 inline Foam::label Foam::FixedList<T, Size>::size() const
381 {
382  return Size;
383 }
384 
385 
386 template<class T, unsigned Size>
387 inline Foam::label Foam::FixedList<T, Size>::max_size() const
388 {
389  return Size;
390 }
391 
392 
393 template<class T, unsigned Size>
395 {
396  return false;
397 }
398 
399 
400 template<class T, unsigned Size>
401 template<class HashT>
403 (
404  const FixedList<T, Size>& lst,
405  unsigned seed
406 ) const
407 {
408  if (contiguous<T>())
409  {
410  // hash directly
411  return Hasher(lst.v_, sizeof(lst.v_), seed);
412  }
413  else
414  {
415  // hash incrementally
416  unsigned val = seed;
417 
418  for (register unsigned i=0; i<Size; i++)
419  {
420  val = HashT()(lst[i], val);
421  }
422 
423  return val;
424  }
425 }
426 
427 
428 // ************************ vim: set sw=4 sts=4 et: ************************ //