PSCF v1.1
ListArray.h
1#ifndef UTIL_LIST_ARRAY_H
2#define UTIL_LIST_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/Array.h>
12#include <util/containers/Node.h>
13#include <util/containers/List.h>
14#include <util/misc/Memory.h>
15#include <util/global.h>
16
17namespace Util
18{
19
29 template <typename Data>
30 class ListArray
31 {
32
33 public:
34
38 ListArray();
39
45 virtual ~ListArray();
46
53 void allocate(int capacity, int nList);
54
60 int nList() const;
61
67 int capacity() const
68 { return capacity_; }
69
76 Data& operator[] (int i)
77 {
78 assert(nodes_ != 0);
79 assert(i < capacity_);
80 assert(i >= 0);
81 return nodes_[i].data();
82 }
83
90 const Data& operator[] (int i) const
91 {
92 assert(nodes_ != 0);
93 assert(i < capacity_);
94 assert(i >= 0 );
95 return nodes_[i].data();
96 }
97
104 List<Data>& list(int i);
105
112 const List<Data>& list(int i) const;
113
120 Node<Data>& node(int i);
121
125 bool isValid() const;
126
127 private:
128
129 // C array of Node<Data> objects
130 Node<Data> *nodes_;
131
132 // C array of List<Data> objects
133 List<Data> *lists_;
134
135 // Allocated size of nodes_ array.
136 int capacity_;
137
138 // Number of lists.
139 int nList_;
140
141 };
142
143
144 /*
145 * Default constructor.
146 */
147 template <typename Data>
149 : nodes_(0),
150 lists_(0),
151 capacity_(0),
152 nList_(0)
153 {}
154
155 /*
156 * Destructor.
157 *
158 * Delete dynamically allocated arrays of Data, Node, and List objects.
159 */
160 template <typename Data>
162 {
163 if (nodes_) {
164 Memory::deallocate< Node<Data> >(nodes_, capacity_);
165 }
166 if (lists_) {
167 Memory::deallocate< List<Data> >(lists_, nList_);
168 }
169 }
170
171 /*
172 * Allocate arrays of Data objects, list nodes, and linked lists.
173 *
174 * \param capacity size of arrays of Data and ListNode<Data> objects
175 * \param nList size of array of List<Data> linked list objects
176 */
177 template <typename Data>
178 void ListArray<Data>::allocate(int capacity, int nList)
179 {
180 int i;
181 capacity_ = capacity;
182 nList_ = nList;
183
184 // Allocate array of nodes
185 Memory::allocate< Node<Data> >(nodes_, capacity_);
186
187 // Allocate and initialize array of lists
188 Memory::allocate< List<Data> >(lists_, nList_);
189 for (i=0; i < nList_; ++i) {
190 lists_[i].initialize(nodes_, capacity_);
191 }
192
193 }
194
195 /*
196 * Get the number of associated linked lists.
197 *
198 * \return size of array of associated List<Data> objects.
199 */
200 template <typename Data>
201 inline int ListArray<Data>::nList() const
202 { return nList_;}
203
204 /*
205 * Return a reference to a specific List.
206 *
207 * \param i array index
208 * \return reference to List number i
209 */
210 template <typename Data>
212 {
213 assert(lists_ != 0);
214 assert(i >= 0);
215 assert(i < nList_);
216
217 return *(lists_ + i);
218 }
219
220 /*
221 * Return a const reference to a specific List.
222 *
223 * \param i array index
224 * \return reference to List number i
225 */
226 template <typename Data>
228 {
229 assert(lists_ != 0);
230 assert(i >= 0);
231 assert(i < nList_);
232
233 return *(lists_ + i);
234 }
235
236 /*
237 * Return reference to node number i.
238 *
239 * \param i array index
240 * \return reference to Data object element number i
241 */
242 template <typename Data>
244 {
245 assert(nodes_ != 0);
246 assert(i >= 0);
247 assert(i < capacity_);
248
249 return *(nodes_ + i);
250 }
251
252 /*
253 * Return true if the ListAray is valid, or throw an exception.
254 */
255 template <typename Data>
257 {
258 if (nodes_ != 0) {
259 if (lists_ == 0) {
260 UTIL_THROW("nodes_ is allocated but lists_ is not");
261 }
262
263 // Check validity of all lists
264 for (int i=0; i < nList_ ; ++i) {
265 if (!lists_[i].isValid()){
266 UTIL_THROW("Invalid list in ListArray");
267 }
268 }
269
270 } else {
271
272 if (lists_ != 0) {
273 UTIL_THROW("nodes_ is not allocated but lists_ is");
274 }
275
276 }
277 return true;
278
279 }
280
281}
282#endif
An array of objects that are accessible by one or more linked List objects.
Definition: ListArray.h:31
ListArray()
Constructor.
Definition: ListArray.h:148
Node< Data > & node(int i)
Return reference to node number i.
Definition: ListArray.h:243
List< Data > & list(int i)
Return a reference to a specific List.
Definition: ListArray.h:211
void allocate(int capacity, int nList)
Allocate arrays of Node and List objects.
Definition: ListArray.h:178
virtual ~ListArray()
Destructor.
Definition: ListArray.h:161
int capacity() const
Return allocated size of underlying array of nodes.
Definition: ListArray.h:67
bool isValid() const
Return true if the ListAray is valid, or throw an exception.
Definition: ListArray.h:256
int nList() const
Get the number of associated linked lists.
Definition: ListArray.h:201
Data & operator[](int i)
Return data for node element i.
Definition: ListArray.h:76
Linked list class template.
Definition: List.h:33
Linked List Node, class template.
Definition: Node.h:23
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1