1 #ifndef RANDOM_SEQUENCE_H_
2 #define RANDOM_SEQUENCE_H_
4 #include "cksequence_internal.h"
12 #define Set(a, ind) a[(ind/8)] = (a[(ind/8)] | (1<<(ind%8)))
13 #define Reset(a, ind) a[(ind/8)] = (a[(ind/8)] & (~(1<<(ind%8))))
14 #define IsSet(a, ind) (a[(ind/8)] & (1<<(ind%8)))
17 * Iterator for the RandomSequence
22 class RandomIterator
: public CkSequenceIteratorInternal
<T
> {
24 typename
std::vector
<T
>::iterator it_
;
28 RandomIterator(typename
std::vector
<T
>::iterator it
) : it_(it
) {}
38 void operator++(int) {
46 void operator--(int) {
50 bool operator==(const CkSequenceIteratorInternal
<T
>& rhs
) const {
51 return (this->it_
== ((RandomIterator
*)&rhs
)->it_
);
54 bool operator!=(const CkSequenceIteratorInternal
<T
>& rhs
) const {
55 return (this->it_
!= ((RandomIterator
*)&rhs
)->it_
);
60 class BitVectorIterator
: public CkSequenceIteratorInternal
<T
> {
62 BitVectorIterator() {}
64 BitVectorIterator(char*& bit_vector
, int start
, int index
, int max
) :
65 bit_vector_(bit_vector
), start_(start
), index_(index
), max_(max
) {
66 while ((index_
< max_
+ 1) && !IsSet(bit_vector_
, index_
)) {
72 return (start_
+ index_
);
76 while ((++index_
< max_
+1) && !IsSet(bit_vector_
, index_
)) {
80 void operator++(int) {
81 while ((++index_
< max_
+1) && !IsSet(bit_vector_
, index_
)) {
88 void operator--(int) {
91 bool operator==(const CkSequenceIteratorInternal
<T
>& rhs
) const {
92 return (this->bit_vector_
== ((BitVectorIterator
*)&rhs
)->bit_vector_
&&
93 this->index_
== ((BitVectorIterator
*)&rhs
)->index_
);
96 bool operator!=(const CkSequenceIteratorInternal
<T
>& rhs
) const {
97 return (this->bit_vector_
!= ((BitVectorIterator
*)&rhs
)->bit_vector_
||
98 this->index_
!= ((BitVectorIterator
*)&rhs
)->index_
);
113 template <typename T
>
114 class RandomSequence
: public CkSequenceInternal
<T
> {
121 RandomSequence(char*& bit_vector
, int start
, int end
) {
123 start_
= start
- min_
;
124 max_
= min_
+ (end
- start
);
125 std::cout
<< "starting element " << start_
<< " ending ele " << end
<< " max " << max_
<< " min " << min_
<< std::endl
;
126 bit_vector_
= (char *) malloc ((max_
+1)/8 + 1);
127 memcpy(bit_vector_
, &bit_vector
[(start
/8)], (max_
+1)/8 + 1);
130 template <typename GenericIterator
>
131 RandomSequence(const GenericIterator
& begin
, const GenericIterator
& end
) {
138 for (GenericIterator it
= begin
; it
!= end
; ++it
) {
148 std::cout
<< "max " << max_
<< std::endl
;
149 bit_vector_
= (char *) malloc ((max_
+1)/8 + 1);
150 memset(bit_vector_
, 0, (max_
+1)/8 + 1);
152 for (GenericIterator it
= begin
; it
!= end
; ++it
) {
153 Set(bit_vector_
, (*it
));
155 std::cout
<< "Malloc bits " << ((max_
+1)/8 + 1) << std::endl
;
161 void Insert(const T
& element
);
163 void Remove(const T
& element
);
165 int num_elements() const;
167 int mem_size() const;
174 return start_
+ max_
;
181 CkSequenceIteratorInternal
<T
>* begin() {
182 return new BitVectorIterator
<T
>(bit_vector_
, start_
, min_
, max_
);
185 CkSequenceIteratorInternal
<T
>* end() {
186 return new BitVectorIterator
<T
>(bit_vector_
, start_
, max_
+1, max_
);
189 void pup(PUP::er
&p
) {
194 if (p
.isUnpacking()) {
195 bit_vector_
= (char *) malloc ((max_
+1)/8 + 1);
197 PUParray(p
, bit_vector_
, ((max_
+1)/8 + 1));
208 template <typename T
>
209 inline void RandomSequence
<T
>::Insert(const T
& element
) {
210 int ele_ind
= element
- start_
;
211 if (ele_ind
/8 > (max_
+1)/8) {
212 int diff
= ((ele_ind
+ 1) / 8) - (( max_
+ 1) / 8);
213 bit_vector_
= (char *) realloc(bit_vector_
, (ele_ind
+1)/8 + 1);
214 memset(&bit_vector_
[((max_
+1)/8) + 1], 0, diff
);
216 Set(bit_vector_
, ele_ind
);
217 if (ele_ind
> max_
) {
222 template <typename T
>
223 inline void RandomSequence
<T
>::Remove(const T
& element
) {
226 template <typename T
>
227 inline int RandomSequence
<T
>::num_elements() const {
228 return num_elements_
;
231 template <typename T
>
232 inline int RandomSequence
<T
>::mem_size() const {
233 // return sizeof(T) * container_.size();
234 return ((max_
+1)/8 + 3);
238 #endif // RANDOM_SEQUENCE_H_