/usr/include/trilinos/fei_ctg_set.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | #ifndef _fei_ctg_set_hpp_
#define _fei_ctg_set_hpp_
/*--------------------------------------------------------------------*/
/* Copyright 2006 Sandia Corporation. */
/* Under the terms of Contract DE-AC04-94AL85000, there is a */
/* non-exclusive license for use of this work by or on behalf */
/* of the U.S. Government. Export of this program may require */
/* a license from the United States Government. */
/*--------------------------------------------------------------------*/
#include <fei_macros.hpp>
#include <fei_iostream.hpp>
#include <cstring>
#include <vector>
#include <fei_ArrayUtils.hpp>
namespace fei {
const int Set_end_val = -99999999;
/** A specialized container that mimics std::set in many ways.
This set can only be used for integer types (such as short, int, long).
This container is optimized for inserting/storing indices that have
significant contiguous sections or ranges.
Data is stored in an array in pairs, where each pair represents a
contiguous range. The first item in a pair is the beginning of the range,
and the second item in a pair is one greater than the end of the range.
Example:
Assume the data to be stored is 0, 1, 2, 3, 6, 7, 8, which forms two
contiguous ranges 0 - 3 and 6 - 8. This container will store this data
in an array like this: 0, 4, 6, 9.
*/
template<typename T>
class ctg_set {
public:
/** constructor */
ctg_set(int alloc_incr=32)
: dataptr_(0), len_(0), highwatermark_(0), alloc_incr_(alloc_incr) {}
/** constructor */
ctg_set(const ctg_set<T>& src)
: dataptr_(0), len_(0),
highwatermark_(src.highwatermark_), alloc_incr_(src.alloc_incr_)
{
if (highwatermark_>0) {
expand_dataptr(highwatermark_);
len_ = src.len_;
for(int i=0; i<len_; ++i) dataptr_[i] = src.dataptr_[i];
}
}
/** destructor */
virtual ~ctg_set(){clear();}
/** alias for template parameter */
typedef T key_type;
/** const_iterator */
class const_iterator {
public:
/** constructor */
const_iterator() : set_(0),
val_(Set_end_val), limit_(Set_end_val), i_(0) {}
/** constructor */
const_iterator(const ctg_set<T>* _set,
const T& val,
int i)
: set_(_set),
val_(val), limit_(Set_end_val), i_(i)
{
if (set_ != 0) {
if (set_->len_ > 0) {
limit_ = set_->dataptr_[i+1]-1;
}
}
}
/** constructor */
const_iterator(const const_iterator& src)
: set_(src.set_),
val_(src.val_), limit_(src.limit_), i_(src.i_) {}
/** destructor */
virtual ~const_iterator(){}
/** operator* */
const T& operator*() const { return(val_); }
/** operator++ */
const_iterator& operator++()
{
if (val_ < limit_) {
++val_;
}
else {
if (i_ < set_->len_-2) {
i_ += 2;
val_ = set_->dataptr_[i_];
limit_ = set_->dataptr_[i_+1]-1;
}
else {
val_ = Set_end_val;
limit_ = Set_end_val;
}
}
return(*this);
}
/** operator= */
const_iterator& operator=(const const_iterator& src)
{
set_ = src.set_;
i_ = src.i_;
val_ = src.val_;
limit_ = src.limit_;
return(*this);
}
/** operator== */
bool operator==(const const_iterator& rhs)
{
return(val_ == rhs.val_);
}
/** operator!= */
bool operator!=(const const_iterator& rhs)
{
return(val_ != rhs.val_);
}
private:
const ctg_set<T>* set_;
T val_;
T limit_;
int i_;
};
/** iterator pointing to the beginning of ctg_set's contents */
const_iterator begin() const
{
T val = Set_end_val;
if (len_>0) {
val = dataptr_[0];
}
return(const_iterator(this, val, 0));
}
/** iterator pointing one past the end of ctg_set's contents */
static const_iterator end() { return(const_iterator(0, Set_end_val, 0)); }
/** assignment operator= */
ctg_set<T>& operator=(const ctg_set<T>& src)
{
highwatermark_ = src.highwatermark_;
expand_dataptr(highwatermark_);
len_ = src.len_;
return(*this);
}
/** operator!= */
bool operator!=(const ctg_set<T>& rhs)
{
if (len_ != rhs.len_) {
return(true);
}
for(int i=0; i<len_; ++i) {
if (dataptr_[i] != rhs.dataptr_[i]) {
return(true);
}
}
return(false);
}
/** Clear the contents of this set. (Get rid of the contents.)
*/
int clear()
{
delete [] dataptr_;
dataptr_ = 0;
len_ = 0;
highwatermark_ = 0;
return(0);
}
/** Insert item in this set, if not already present.
Return value is a pair with an iterator, and a bool indicating whether
the insertion was made.
*/
std::pair<const_iterator,bool> insert(const T& item)
{
if (len_ > highwatermark_) {
FEI_COUT << "error"<<FEI_ENDL;
}
if (len_ < 1) {
highwatermark_ = alloc_incr_;
expand_dataptr(highwatermark_);
dataptr_[0] = item;
dataptr_[1] = item+1;
len_ = 2;
return(std::pair<const_iterator,bool>(const_iterator(this, item, 0),true));
}
int insertPoint = fei::lowerBound(item, dataptr_, len_);
if (insertPoint < len_) {
//dataptr_[insertPoint] is the first entry in dataptr_ that is not
//less than item.
//The possibilities are:
//
//1. dataptr_[insertPoint] equals item, so:
//
// if (insertPoint is an even number) {
// return since item is present
// }
// else {
// expand the range below item to include item
// (by incrementing dataptr_[insertPoint])
// check whether dataptr_[insertPoint] == dataptr_[insertPoint+1]
// if so, merge the range at insertPoint-1 with the
// range at insertPoint+1
// return
// }
//
//2. dataptr_[insertPoint] is greater than item, so:
//
// if (insertPoint is an even number) {
// if (item == dataptr_[insertPoint]-1) {
// expand the range at insertPoint to include item, by
// simply decrementing dataptr_[insertPoint]
// }
// else {
// insert a new range at insertPoint
// }
// }
// else {
// return since item is already present in the range at
// dataptr_[insertPoint-1]
// }
//
if (dataptr_[insertPoint] == item) {
if (insertPoint%2 == 0) {
//insertPoint is an even number, so return since item is present
return(std::pair<const_iterator,bool>(const_iterator(this, item, insertPoint),false));
}
//Since insertPoint is an odd number, item lies just outside an existing
//range so we simply need to add item to the range by incrementing
//dataptr_[insertPoint].
++dataptr_[insertPoint];
//now check whether this newly expanded range should be merged
//with the range above it
if (insertPoint < len_-1) {
if (dataptr_[insertPoint] == dataptr_[insertPoint+1]) {
dataptr_[insertPoint] = dataptr_[insertPoint+2];
len_ -= 2;
int nmove=len_-insertPoint-1;
if (nmove > 0) {
T* dest = dataptr_+insertPoint+1;
T* src = dest+2;
std::memmove(dest, src, nmove*sizeof(T));
}
}
}
return(std::pair<const_iterator,bool>(const_iterator(this, item, insertPoint-1),true));
}
else {
//dataptr_[insertPoint] is greater than item.
if (insertPoint%2 == 0) {
if (item == dataptr_[insertPoint]-1) {
--dataptr_[insertPoint];
return(std::pair<const_iterator,bool>(const_iterator(this, item, insertPoint),true));
}
else {
//insert a new range at insertPoint
int nmove = len_-insertPoint;
if (len_+2 > highwatermark_) {
highwatermark_ += alloc_incr_;
expand_dataptr(highwatermark_);
}
len_ += 2;
if (nmove > 0) {
T* dest = dataptr_+insertPoint+2;
T* src = dest - 2;
std::memmove(dest, src, nmove*sizeof(T));
}
dataptr_[insertPoint] = item;
dataptr_[insertPoint+1] = item+1;
return(std::pair<const_iterator,bool>(const_iterator(this, item, insertPoint),true));
}
}
else {
return(std::pair<const_iterator,bool>(const_iterator(this, item, insertPoint-1),false));
}
}
}
//If we get to here, insertPoint >= len_, meaning we need to append
//a new range.
if (len_+2 > highwatermark_) {
highwatermark_ += alloc_incr_;
expand_dataptr(highwatermark_);
}
len_ += 2;
dataptr_[insertPoint] = item;
dataptr_[insertPoint+1] = item+1;
return(std::pair<const_iterator,bool>(const_iterator(this, item, insertPoint),true));
}
/** insert2 -- power-users only */
void insert2(const T& item)
{
if (len_ < 1) {
highwatermark_ = alloc_incr_;
expand_dataptr(highwatermark_);
dataptr_[0] = item;
dataptr_[1] = item+1;
len_ = 2;
return;
}
int insertPoint = fei::lowerBound(item, dataptr_, len_);
if (insertPoint < len_) {
//dataptr_[insertPoint] is the first entry in dataptr_ that is not
//less than item.
//The possibilities are:
//
//1. insertPoint is an even number:
// dataptr_[insertPoint] is the start of an existing range.
// diff = dataptr_[insertPoint] - item;
// switch(diff) {
// case 0 : // item in range, so return
// case 1 : // item just below range, so expand range and return
// default: // insert new range for item
// }
//
//2. insertPoint is an odd number:
// dataptr_[insertPoint] is the end of an existing range
// diff = dataptr_[insertPoint] - item;
// switch(diff) {
// case 0 : {
// // item just above range, so expand range
// // check whether range should be merged with range above
// }
// default: // item in range, so return
// }
//
if (insertPoint%2==0) {
switch(dataptr_[insertPoint]-item) {
case 0: break; //item in range
case 1: {//expand range downwards
--dataptr_[insertPoint];
break;
}
default: {// insert new range for item
//insert a new range at insertPoint
int nmove = len_-insertPoint;
if (len_+2 > highwatermark_) {
highwatermark_ += alloc_incr_;
expand_dataptr(highwatermark_);
}
len_ += 2;
T* dest = dataptr_+insertPoint+2;
T* src = dest - 2;
std::memmove(dest, src, nmove*sizeof(T));
dataptr_[insertPoint] = item;
dataptr_[insertPoint+1] = item+1;
}
}
}
else {//insertPoint is odd number
if (dataptr_[insertPoint] == item) {
// item just above range, so expand range
++dataptr_[insertPoint];
// check whether range should be merged with range above
if (insertPoint < len_-1 &&
dataptr_[insertPoint] == dataptr_[insertPoint+1]) {
dataptr_[insertPoint] = dataptr_[insertPoint+2];
len_ -= 2;
int nmove=len_-insertPoint-1;
if (nmove > 0) {
T* dest = dataptr_+insertPoint+1;
T* src = dest+2;
std::memmove(dest, src, nmove*sizeof(T));
}
}
}//end if (dataptr_[insertPoint]==item)...
// else do nothing, item is already in existing range
}
return;
} // end if (insertPoint < len_)...
//If we get to here, insertPoint >= len_, meaning we need to append
//a new range.
if (len_+2 > highwatermark_) {
highwatermark_ += alloc_incr_;
expand_dataptr(highwatermark_);
}
dataptr_[insertPoint] = item;
dataptr_[insertPoint+1] = item+1;
len_ += 2;
}
/** insert2_dense_group -- not really implemented correctly */
int insert2_dense_group(const T& starting_index, int group_size)
{
for(int i=0; i<group_size; ++i) {
insert2(starting_index+i);
}
return(0);
}
/** find specified item in ctg_set. If not found, return iterator==end(). */
const_iterator find(const T& item)
{
if (len_ < 1) {
return(const_iterator(0, Set_end_val, 0));
}
int insertPoint = -1;
int index = fei::binarySearch(item, dataptr_, len_, insertPoint);
if (index < 0) {
if (insertPoint%2==0) {
return(end());
}
else {
return(const_iterator(this, item, insertPoint-1));
}
}
if (index%2==0) {
return( const_iterator(this, item, index) );
}
return(const_iterator(0, Set_end_val, 0));
}
/** Copy contents of ctg_set into array of length len.
The number of items copied into array is min(len, size()).
*/
int copy_to_array(int len, T* items) const
{
const_iterator iter = begin(), iter_end = end();
int offset = 0;
for(; iter != iter_end; ++iter) {
if (offset >= len) {
break;
}
items[offset++] = *iter;
}
return(0);
}
/** Copy contents of ctg_set into std::vector. */
int copy_to_vector(std::vector<T>& items) const
{
int sz = size();
items.resize(sz);
T* itemsPtr = &(items[0]);
return(copy_to_array(sz, itemsPtr));
}
/** return size of ctg_set. */
int size() const
{
int setsize = 0;
int offset = 0;
while(offset<(len_-1)) {
setsize += dataptr_[offset+1]-dataptr_[offset];
offset += 2;
}
return(setsize);
}
private:
void expand_dataptr(int newlen)
{
//on entry to this function, dataptr_ has length 'len_'.
//we assume newlen is greater than len_.
//after we create newptr with length 'newlen', we copy
//len_ positions from dataptr_ to newptr.
T* newptr = new T[newlen];
for(int i=0; i<len_; ++i) newptr[i] = dataptr_[i];
delete [] dataptr_;
dataptr_ = newptr;
}
friend class const_iterator;
T* dataptr_;
int len_;
int highwatermark_;
int alloc_incr_;
};//class ctg_set
}//namespace fei
#endif
|