2 #ifndef _fei_ctg_set_hpp_
3 #define _fei_ctg_set_hpp_
13 #include <fei_macros.hpp>
14 #include <fei_iostream.hpp>
18 #include <fei_ArrayUtils.hpp>
22 const int Set_end_val = -99999999;
43 : dataptr_(0), len_(0), highwatermark_(0), alloc_incr_(alloc_incr) {}
47 : dataptr_(0), len_(0),
48 highwatermark_(src.highwatermark_), alloc_incr_(src.alloc_incr_)
50 if (highwatermark_>0) {
51 expand_dataptr(highwatermark_);
53 for(
int i=0; i<len_; ++i) dataptr_[i] = src.dataptr_[i];
68 val_(Set_end_val), limit_(Set_end_val), i_(0) {}
75 val_(val), limit_(Set_end_val), i_(i)
79 limit_ = set_->dataptr_[i+1]-1;
87 val_(src.val_), limit_(src.limit_), i_(src.i_) {}
102 if (i_ < set_->len_-2) {
104 val_ = set_->dataptr_[i_];
105 limit_ = set_->dataptr_[i_+1]-1;
109 limit_ = Set_end_val;
128 return(val_ == rhs.val_);
134 return(val_ != rhs.val_);
151 return(const_iterator(
this, val, 0));
155 static const_iterator
end() {
return(const_iterator(0, Set_end_val, 0)); }
160 highwatermark_ = src.highwatermark_;
161 expand_dataptr(highwatermark_);
170 if (len_ != rhs.len_) {
174 for(
int i=0; i<len_; ++i) {
175 if (dataptr_[i] != rhs.dataptr_[i]) {
198 std::pair<const_iterator,bool>
insert(
const T& item)
200 if (len_ > highwatermark_) {
201 FEI_COUT <<
"error"<<FEI_ENDL;
204 highwatermark_ = alloc_incr_;
205 expand_dataptr(highwatermark_);
207 dataptr_[1] = item+1;
209 return(std::pair<const_iterator,bool>(const_iterator(
this, item, 0),
true));
214 if (insertPoint < len_) {
251 if (dataptr_[insertPoint] == item) {
252 if (insertPoint%2 == 0) {
254 return(std::pair<const_iterator,bool>(const_iterator(
this, item, insertPoint),
false));
260 ++dataptr_[insertPoint];
264 if (insertPoint < len_-1) {
265 if (dataptr_[insertPoint] == dataptr_[insertPoint+1]) {
266 dataptr_[insertPoint] = dataptr_[insertPoint+2];
268 int nmove=len_-insertPoint-1;
270 T* dest = dataptr_+insertPoint+1;
272 std::memmove(dest, src, nmove*
sizeof(T));
277 return(std::pair<const_iterator,bool>(const_iterator(
this, item, insertPoint-1),
true));
282 if (insertPoint%2 == 0) {
283 if (item == dataptr_[insertPoint]-1) {
284 --dataptr_[insertPoint];
285 return(std::pair<const_iterator,bool>(const_iterator(
this, item, insertPoint),
true));
289 int nmove = len_-insertPoint;
290 if (len_+2 > highwatermark_) {
291 highwatermark_ += alloc_incr_;
292 expand_dataptr(highwatermark_);
296 T* dest = dataptr_+insertPoint+2;
298 std::memmove(dest, src, nmove*
sizeof(T));
300 dataptr_[insertPoint] = item;
301 dataptr_[insertPoint+1] = item+1;
303 return(std::pair<const_iterator,bool>(const_iterator(
this, item, insertPoint),
true));
307 return(std::pair<const_iterator,bool>(const_iterator(
this, item, insertPoint-1),
false));
314 if (len_+2 > highwatermark_) {
315 highwatermark_ += alloc_incr_;
316 expand_dataptr(highwatermark_);
319 dataptr_[insertPoint] = item;
320 dataptr_[insertPoint+1] = item+1;
322 return(std::pair<const_iterator,bool>(const_iterator(
this, item, insertPoint),
true));
329 highwatermark_ = alloc_incr_;
330 expand_dataptr(highwatermark_);
332 dataptr_[1] = item+1;
339 if (insertPoint < len_) {
366 if (insertPoint%2==0) {
367 switch(dataptr_[insertPoint]-item) {
370 --dataptr_[insertPoint];
375 int nmove = len_-insertPoint;
376 if (len_+2 > highwatermark_) {
377 highwatermark_ += alloc_incr_;
378 expand_dataptr(highwatermark_);
382 T* dest = dataptr_+insertPoint+2;
384 std::memmove(dest, src, nmove*
sizeof(T));
386 dataptr_[insertPoint] = item;
387 dataptr_[insertPoint+1] = item+1;
392 if (dataptr_[insertPoint] == item) {
394 ++dataptr_[insertPoint];
397 if (insertPoint < len_-1 &&
398 dataptr_[insertPoint] == dataptr_[insertPoint+1]) {
399 dataptr_[insertPoint] = dataptr_[insertPoint+2];
401 int nmove=len_-insertPoint-1;
403 T* dest = dataptr_+insertPoint+1;
405 std::memmove(dest, src, nmove*
sizeof(T));
417 if (len_+2 > highwatermark_) {
418 highwatermark_ += alloc_incr_;
419 expand_dataptr(highwatermark_);
421 dataptr_[insertPoint] = item;
422 dataptr_[insertPoint+1] = item+1;
429 for(
int i=0; i<group_size; ++i) {
437 const_iterator
find(
const T& item)
440 return(const_iterator(0, Set_end_val, 0));
443 int insertPoint = -1;
447 if (insertPoint%2==0) {
451 return(const_iterator(
this, item, insertPoint-1));
456 return( const_iterator(
this, item, index) );
459 return(const_iterator(0, Set_end_val, 0));
467 const_iterator iter =
begin(), iter_end =
end();
469 for(; iter != iter_end; ++iter) {
474 items[offset++] = *iter;
485 T* itemsPtr = &(items[0]);
494 while(offset<(len_-1)) {
495 setsize += dataptr_[offset+1]-dataptr_[offset];
503 void expand_dataptr(
int newlen)
510 T* newptr =
new T[newlen];
511 for(
int i=0; i<len_; ++i) newptr[i] = dataptr_[i];
516 friend class const_iterator;
int insert2_dense_group(const T &starting_index, int group_size)
int copy_to_vector(std::vector< T > &items) const
ctg_set(int alloc_incr=32)
const_iterator find(const T &item)
const_iterator(const ctg_set< T > *_set, const T &val, int i)
int binarySearch(const T &item, const T *list, int len)
int lowerBound(const T &item, const T *list, int len)
const_iterator begin() const
bool operator!=(const ctg_set< T > &rhs)
void insert2(const T &item)
ctg_set(const ctg_set< T > &src)
const_iterator & operator=(const const_iterator &src)
const_iterator(const const_iterator &src)
int copy_to_array(int len, T *items) const
bool operator!=(const const_iterator &rhs)
const T & operator*() const
static const_iterator end()
std::pair< const_iterator, bool > insert(const T &item)
bool operator==(const const_iterator &rhs)
const_iterator & operator++()
virtual ~const_iterator()
ctg_set< T > & operator=(const ctg_set< T > &src)