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(Data
const & data);
144 int index(Data
const & data)
const;
172 int id(Data
const* ptr)
const;
186 template <
typename Data>
195 template <
typename Data>
209 template <
typename Data>
216 if (array == 0)
UTIL_THROW(
"Null array pointer");
218 if (tags_)
UTIL_THROW(
"tags_ array already allocated");
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");
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>
423 inline int ArraySet<Data>::id(Data
const * ptr)
const
424 {
return int(ptr - data_); }
void remove(Data const &data)
Remove an element from the set.
virtual ~ArraySet()
Destructor.
void append(Data &data)
Append an element to the set.
bool isValid() const
Return true if the ArraySet is valid, or throw an exception.
void dump() const
Write the internal state of the ArraySet to std::cout.
bool isAllocated() const
Return true if the ArraySet is initialized, false otherwise.
void clear()
Reset to empty state.
void allocate(Data const *array, int capacity)
Associate with a C array and allocate required memory.
int index(Data const &data) const
Return the current index of an element within the set, if any.
Data & pop()
Pop the topmost element from the set.
Array container class template.
int capacity() const
Return allocated size.
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
Forward iterator for a PArray.
int size() const
Return logical size.
int capacity_
Allocated size of ptrs_ array.
int capacity() const
Return allocated size.
PArray()
Constructor (protected to prevent instantiation).
int size_
Logical size (number of elements with initialized data).
Data ** ptrs_
PArray of of pointers to Data objects.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Utility classes for scientific computation.