1 #ifndef UTIL_ARRAY_SET_H 2 #define UTIL_ARRAY_SET_H 11 #include <util/containers/Array.h> 12 #include <util/containers/PArray.h> 13 #include <util/misc/Memory.h> 45 template <
typename Data>
111 void remove(
const Data& data);
144 int index(
const Data& data)
const;
172 int id(
const Data *ptr)
const;
186 template <
typename Data>
195 template <
typename Data>
202 Memory::deallocate<int>(tags_,
capacity_);
209 template <
typename Data>
214 if (capacity == 0)
UTIL_THROW(
"Zero capacity");
215 if (capacity < 0)
UTIL_THROW(
"Negative capacity");
216 if (array == 0)
UTIL_THROW(
"Null array pointer");
218 if (tags_)
UTIL_THROW(
"tags_ array already allocated");
222 Memory::allocate<int>(tags_,
capacity);
236 template <
typename Data>
243 template <
typename Data>
246 Data*
const ptr = &data;
249 if (ptr < data_ || ptr >= data_ +
capacity_) {
252 int arrayId = id(ptr);
253 int setId = tags_[arrayId];
255 UTIL_THROW(
"Attempt to add element that is already in set");
260 tags_[id(ptr)] =
size_;
267 template <
typename Data>
270 const Data*
const ptr = &data;
273 if (ptr < data_ || ptr >= data_ +
capacity_) {
276 int arrayId = id(ptr);
277 int setId = tags_[arrayId];
284 assert(
ptrs_[setId] == ptr);
288 if (setId !=
size_ - 1) {
290 tags_[id(
ptrs_[setId])] = setId;
300 template <
typename Data>
305 UTIL_THROW(
"Attempt to pop from empty ArraySet");
318 template <
typename Data>
334 template <
typename Data>
337 const Data*
const ptr = &data;
338 if (ptr < data_ || ptr >= data_ +
capacity_) {
341 return tags_[id(ptr)];
347 template <
typename Data>
349 {
return ptrs_ != 0; }
354 template <
typename Data>
361 UTIL_THROW(
"ptrs_ is allocated but tags_ is not");
369 if (j >= capacity_) {
372 if (
id(
ptrs_[j]) != i) {
379 UTIL_THROW(
"Number of nonnegative tags != size");
384 for (i = 0; i <
size_ ; ++i) {
392 UTIL_THROW(
"Null pointer ptrs_[i] for i < size_");
396 UTIL_THROW(
"Number of non-Null pointers != size");
402 UTIL_THROW(
"Non-null pointer ptrs_[i] for i >= size_");
422 template <
typename Data>
424 {
return int(ptr - data_); }
int capacity_
Allocated size of ptrs_ array.
void clear()
Reset to empty state.
void dump() const
Write the internal state of the ArraySet to std::cout.
void append(Data &data)
Append an element to the set.
int capacity() const
Return allocated size.
Array container class template.
File containing preprocessor macros for error handling.
void remove(const Data &data)
Remove an element from the set.
bool isValid() const
Return true if the ArraySet is valid, or throw an exception.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
int index(const Data &data) const
Return the current index of an element within the set, if any.
A container for pointers to a subset of elements of an associated array.
Utility classes for scientific computation.
int size_
Logical size (number of elements with initialized data).
bool isAllocated() const
Return true if the ArraySet is initialized, false otherwise.
void allocate(const Data *array, int capacity)
Associate with a C array and allocate required memory.
Forward iterator for a PArray.
Data ** ptrs_
PArray of of pointers to Data objects.
virtual ~ArraySet()
Destructor.
int size() const
Return logical size.
int capacity() const
Return allocated size.
An array that only holds pointers to its elements.
Data & pop()
Pop the topmost from the set.