Simpatico  v1.10
PArray.h
1 #ifndef UTIL_P_ARRAY_H
2 #define UTIL_P_ARRAY_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/containers/PArrayIterator.h>
12 #include <util/containers/ConstPArrayIterator.h>
13 #include <util/global.h>
14 
15 namespace Util
16 {
17 
32  template <typename Data>
33  class PArray
34  {
35 
36  public:
37 
38  // Protected default constructor
39 
43  virtual ~PArray();
44 
50  int capacity() const;
51 
57  int size() const;
58 
64  void begin(PArrayIterator<Data>& iterator) const;
65 
71  void begin(ConstPArrayIterator<Data>& iterator) const;
72 
79  Data& operator [] (int i) const;
80 
81  protected:
82 
84  PArray();
85 
87  Data** ptrs_;
88 
90  int capacity_;
91 
93  int size_;
94 
95  private:
96 
100  PArray(const PArray<Data>& other);
101 
105  PArray<Data>& operator = (const PArray<Data>& other);
106 
107  };
108 
109  /*
110  * Constructor.
111  */
112  template <typename Data>
114  : ptrs_(0),
115  capacity_(0),
116  size_(0)
117  {}
118 
119  /*
120  * Destructor.
121  */
122  template <typename Data>
124  {}
125 
126  /*
127  * Return allocated size.
128  */
129  template <typename Data>
130  inline int PArray<Data>::capacity() const
131  { return capacity_; }
132 
133  /*
134  * Return logical size.
135  */
136  template <typename Data>
137  inline int PArray<Data>::size() const
138  { return size_; }
139 
145  template <typename Data>
146  inline void PArray<Data>::begin(PArrayIterator<Data>& iterator) const
147  {
148  if (ptrs_ && size_ > 0) {
149  iterator.setCurrent(ptrs_);
150  iterator.setEnd(ptrs_ + size_);
151  } else {
152  iterator.setNull();
153  }
154  }
155 
161  template <typename Data>
162  inline void PArray<Data>::begin(ConstPArrayIterator<Data>& iterator) const
163  {
164  if (ptrs_ && size_ > 0) {
165  iterator.setCurrent(ptrs_);
166  iterator.setEnd(ptrs_ + size_);
167  } else {
168  iterator.setNull();
169  }
170  }
171 
178  template <typename Data>
179  inline Data& PArray<Data>::operator [] (int i) const
180  {
181  assert(ptrs_);
182  assert(i >= 0);
183  assert(i < size_);
184  return *(ptrs_[i]);
185  }
186 
187 }
188 #endif
PArray()
Constructor (protected to prevent instantiation).
Definition: PArray.h:113
int capacity_
Allocated size of ptrs_ array.
Definition: PArray.h:90
void begin(PArrayIterator< Data > &iterator) const
Set a PArrayIterator to the beginning of this PArray.
Definition: PArray.h:146
int capacity() const
Return allocated size.
Definition: PArray.h:130
Forward iterator for a PArray.
File containing preprocessor macros for error handling.
void setCurrent(Data **ptr)
Set the current pointer value.
void setNull()
Nullify the iterator.
void setEnd(Data **ptr)
Set the value of the end pointer.
virtual ~PArray()
Destructor.
Definition: PArray.h:123
Utility classes for scientific computation.
Definition: accumulators.mod:1
int size_
Logical size (number of elements with initialized data).
Definition: PArray.h:93
Forward iterator for a PArray.
Definition: ArraySet.h:19
Data ** ptrs_
PArray of of pointers to Data objects.
Definition: PArray.h:87
void setNull()
Nullify the iterator.
void setEnd(Data **ptr)
Set the value of the end pointer.
int size() const
Return logical size.
Definition: PArray.h:137
Data & operator[](int i) const
Mimic C array subscripting.
Definition: PArray.h:179
An array that only holds pointers to its elements.
Definition: PArray.h:33
void setCurrent(Data **ptr)
Set the current pointer value.