PSCF v1.1
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
15namespace Util
16{
17
35 template <typename Data>
36 class PArray
37 {
38
39 public:
40
41 // Protected default constructor
42
46 virtual ~PArray();
47
53 int capacity() const;
54
60 int size() const;
61
67 void begin(PArrayIterator<Data>& iterator) const;
68
74 void begin(ConstPArrayIterator<Data>& iterator) const;
75
82 Data& operator [] (int i) const;
83
84 protected:
85
87 PArray();
88
90 Data** ptrs_;
91
94
96 int size_;
97
98 private:
99
103 PArray(PArray<Data> const & other);
104
108 PArray<Data>& operator = (PArray<Data> const & other);
109
110 };
111
112 /*
113 * Constructor.
114 */
115 template <typename Data>
117 : ptrs_(0),
118 capacity_(0),
119 size_(0)
120 {}
121
122 /*
123 * Destructor.
124 */
125 template <typename Data>
127 {}
128
129 /*
130 * Return allocated size.
131 */
132 template <typename Data>
133 inline int PArray<Data>::capacity() const
134 { return capacity_; }
135
136 /*
137 * Return logical size.
138 */
139 template <typename Data>
140 inline int PArray<Data>::size() const
141 { return size_; }
142
143 /*
144 * Set an PArrayIterator to the beginning of this PArray.
145 */
146 template <typename Data>
147 inline void PArray<Data>::begin(PArrayIterator<Data>& iterator) const
148 {
149 if (ptrs_ && size_ > 0) {
150 iterator.setCurrent(ptrs_);
151 iterator.setEnd(ptrs_ + size_);
152 } else {
153 iterator.setNull();
154 }
155 }
156
157 /*
158 * Set an ConstPArrayIterator to the beginning of this PArray.
159 */
160 template <typename Data>
161 inline
163 {
164 if (ptrs_ && size_ > 0) {
165 iterator.setCurrent(ptrs_);
166 iterator.setEnd(ptrs_ + size_);
167 } else {
168 iterator.setNull();
169 }
170 }
171
172 /*
173 * Subscript - return a reference.
174 */
175 template <typename Data>
176 inline Data& PArray<Data>::operator [] (int i) const
177 {
178 assert(ptrs_);
179 assert(i >= 0);
180 assert(i < size_);
181 return *(ptrs_[i]);
182 }
183
184}
185#endif
Forward iterator for a PArray.
void setEnd(Data **ptr)
Set the value of the end pointer.
void setCurrent(Data **ptr)
Set the current pointer value.
void setNull()
Nullify the iterator.
Forward iterator for a PArray.
void setNull()
Nullify the iterator.
void setEnd(Data **ptr)
Set the value of the end pointer.
void setCurrent(Data **ptr)
Set the current pointer value.
An array that only holds pointers to its elements.
Definition: PArray.h:37
virtual ~PArray()
Destructor.
Definition: PArray.h:126
int size() const
Return logical size.
Definition: PArray.h:140
int capacity_
Allocated size of ptrs_ array.
Definition: PArray.h:93
int capacity() const
Return allocated size.
Definition: PArray.h:133
PArray()
Constructor (protected to prevent instantiation).
Definition: PArray.h:116
int size_
Logical size (number of elements with initialized data).
Definition: PArray.h:96
Data & operator[](int i) const
Mimic C array subscripting.
Definition: PArray.h:176
void begin(PArrayIterator< Data > &iterator) const
Set a PArrayIterator to the beginning of this PArray.
Definition: PArray.h:147
Data ** ptrs_
PArray of of pointers to Data objects.
Definition: PArray.h:90
File containing preprocessor macros for error handling.
Utility classes for scientific computation.
Definition: accumulators.mod:1