Update references to hapi_src to hapi_impl and revert hapiRegisterCallbacks
[charm.git] / src / util / random_sequence.h
blobbd7d4702e261533c83795d2393e553f6708a8388
1 #ifndef RANDOM_SEQUENCE_H_
2 #define RANDOM_SEQUENCE_H_
4 #include "cksequence_internal.h"
5 #include "pup.h"
7 #include <vector>
8 #include <algorithm>
9 #include <string.h>
10 #include <iostream>
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)))
16 /**
17 * Iterator for the RandomSequence
19 * @tparam T
21 template <typename T>
22 class RandomIterator : public CkSequenceIteratorInternal<T> {
23 public:
24 typename std::vector<T>::iterator it_;
26 RandomIterator() {}
28 RandomIterator(typename std::vector<T>::iterator it) : it_(it) {}
30 T& operator*() {
31 return *it_;
34 void operator++() {
35 ++it_;
38 void operator++(int) {
39 ++it_;
42 void operator--() {
43 --it_;
46 void operator--(int) {
47 --it_;
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_);
59 template <typename T>
60 class BitVectorIterator : public CkSequenceIteratorInternal<T> {
61 public:
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_)) {
67 index_++;
71 T operator*() {
72 return (start_ + index_);
75 void operator++() {
76 while ((++index_ < max_+1) && !IsSet(bit_vector_, index_)) {
80 void operator++(int) {
81 while ((++index_ < max_+1) && !IsSet(bit_vector_, index_)) {
85 void operator--() {
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_);
101 private:
102 char* bit_vector_;
103 int start_;
104 int index_;
105 int max_;
111 * @tparam T
113 template <typename T>
114 class RandomSequence : public CkSequenceInternal<T> {
116 public:
118 RandomSequence() {
121 RandomSequence(char*& bit_vector, int start, int end) {
122 min_ = start % 8;
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) {
132 num_elements_ = 0;
133 max_ = 0;
134 if (begin == end) {
135 return;
137 min_ = *begin;
138 for (GenericIterator it = begin; it != end; ++it) {
139 num_elements_++;
140 if (max_ < *it) {
141 max_ = *it;
143 if (*it < min_) {
144 min_ = *it;
147 max_;
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;
158 ~RandomSequence() {
161 void Insert(const T& element);
163 void Remove(const T& element);
165 int num_elements() const;
167 int mem_size() const;
169 T min() const {
170 return start_;
173 T max() const {
174 return start_ + max_;
177 Type type() const {
178 return RANDOM;
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) {
190 p|num_elements_;
191 p|start_;
192 p|min_;
193 p|max_;
194 if (p.isUnpacking()) {
195 bit_vector_ = (char *) malloc ((max_+1)/8 + 1);
197 PUParray(p, bit_vector_, ((max_+1)/8 + 1));
200 private:
201 int num_elements_;
202 T start_;
203 T min_;
204 T max_;
205 char* bit_vector_;
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_) {
218 max_ = ele_ind;
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_