00001 #ifndef RANDOM_SEQUENCE_H_
00002 #define RANDOM_SEQUENCE_H_
00003
00004 #include "cksequence_internal.h"
00005 #include "pup.h"
00006
00007 #include <vector>
00008 #include <algorithm>
00009 #include <string.h>
00010 #include <iostream>
00011
00012 #define Set(a, ind) a[(ind/8)] = (a[(ind/8)] | (1<<(ind%8)))
00013 #define Reset(a, ind) a[(ind/8)] = (a[(ind/8)] & (~(1<<(ind%8))))
00014 #define IsSet(a, ind) (a[(ind/8)] & (1<<(ind%8)))
00015
00021 template <typename T>
00022 class RandomIterator : public CkSequenceIteratorInternal<T> {
00023 public:
00024 typename std::vector<T>::iterator it_;
00025
00026 RandomIterator() {}
00027
00028 RandomIterator(typename std::vector<T>::iterator it) : it_(it) {}
00029
00030 T& operator*() {
00031 return *it_;
00032 }
00033
00034 void operator++() {
00035 ++it_;
00036 }
00037
00038 void operator++(int) {
00039 ++it_;
00040 }
00041
00042 void operator--() {
00043 --it_;
00044 }
00045
00046 void operator--(int) {
00047 --it_;
00048 }
00049
00050 bool operator==(const CkSequenceIteratorInternal<T>& rhs) const {
00051 return (this->it_ == ((RandomIterator *)&rhs)->it_);
00052 }
00053
00054 bool operator!=(const CkSequenceIteratorInternal<T>& rhs) const {
00055 return (this->it_ != ((RandomIterator *)&rhs)->it_);
00056 }
00057 };
00058
00059 template <typename T>
00060 class BitVectorIterator : public CkSequenceIteratorInternal<T> {
00061 public:
00062 BitVectorIterator() {}
00063
00064 BitVectorIterator(char*& bit_vector, int start, int index, int max) :
00065 bit_vector_(bit_vector), start_(start), index_(index), max_(max) {
00066 while ((index_ < max_ + 1) && !IsSet(bit_vector_, index_)) {
00067 index_++;
00068 }
00069 }
00070
00071 T operator*() {
00072 return (start_ + index_);
00073 }
00074
00075 void operator++() {
00076 while ((++index_ < max_+1) && !IsSet(bit_vector_, index_)) {
00077 }
00078 }
00079
00080 void operator++(int) {
00081 while ((++index_ < max_+1) && !IsSet(bit_vector_, index_)) {
00082 }
00083 }
00084
00085 void operator--() {
00086 }
00087
00088 void operator--(int) {
00089 }
00090
00091 bool operator==(const CkSequenceIteratorInternal<T>& rhs) const {
00092 return (this->bit_vector_ == ((BitVectorIterator *)&rhs)->bit_vector_ &&
00093 this->index_ == ((BitVectorIterator *)&rhs)->index_);
00094 }
00095
00096 bool operator!=(const CkSequenceIteratorInternal<T>& rhs) const {
00097 return (this->bit_vector_ != ((BitVectorIterator *)&rhs)->bit_vector_ ||
00098 this->index_ != ((BitVectorIterator *)&rhs)->index_);
00099 }
00100
00101 private:
00102 char* bit_vector_;
00103 int start_;
00104 int index_;
00105 int max_;
00106 };
00107
00108
00113 template <typename T>
00114 class RandomSequence : public CkSequenceInternal<T> {
00115
00116 public:
00117
00118 RandomSequence() {
00119 }
00120
00121 RandomSequence(char*& bit_vector, int start, int end) {
00122 min_ = start % 8;
00123 start_ = start - min_;
00124 max_ = min_ + (end - start);
00125 std::cout << "starting element " << start_ << " ending ele " << end << " max " << max_ << " min " << min_ << std::endl;
00126 bit_vector_ = (char *) malloc ((max_+1)/8 + 1);
00127 memcpy(bit_vector_, &bit_vector[(start/8)], (max_+1)/8 + 1);
00128 }
00129
00130 template <typename GenericIterator>
00131 RandomSequence(const GenericIterator& begin, const GenericIterator& end) {
00132 num_elements_ = 0;
00133 max_ = 0;
00134 if (begin == end) {
00135 return;
00136 }
00137 min_ = *begin;
00138 for (GenericIterator it = begin; it != end; ++it) {
00139 num_elements_++;
00140 if (max_ < *it) {
00141 max_ = *it;
00142 }
00143 if (*it < min_) {
00144 min_ = *it;
00145 }
00146 }
00147 max_;
00148 std::cout << "max " << max_ << std::endl;
00149 bit_vector_ = (char *) malloc ((max_+1)/8 + 1);
00150 memset(bit_vector_, 0, (max_+1)/8 + 1);
00151
00152 for (GenericIterator it = begin; it != end; ++it) {
00153 Set(bit_vector_, (*it));
00154 }
00155 std::cout << "Malloc bits " << ((max_+1)/8 + 1) << std::endl;
00156 }
00157
00158 ~RandomSequence() {
00159 }
00160
00161 void Insert(const T& element);
00162
00163 void Remove(const T& element);
00164
00165 int num_elements() const;
00166
00167 int mem_size() const;
00168
00169 T min() const {
00170 return start_;
00171 }
00172
00173 T max() const {
00174 return start_ + max_;
00175 }
00176
00177 Type type() const {
00178 return RANDOM;
00179 }
00180
00181 CkSequenceIteratorInternal<T>* begin() {
00182 return new BitVectorIterator<T>(bit_vector_, start_, min_, max_);
00183 }
00184
00185 CkSequenceIteratorInternal<T>* end() {
00186 return new BitVectorIterator<T>(bit_vector_, start_, max_+1, max_);
00187 }
00188
00189 void pup(PUP::er &p) {
00190 p|num_elements_;
00191 p|start_;
00192 p|min_;
00193 p|max_;
00194 if (p.isUnpacking()) {
00195 bit_vector_ = (char *) malloc ((max_+1)/8 + 1);
00196 }
00197 PUParray(p, bit_vector_, ((max_+1)/8 + 1));
00198 }
00199
00200 private:
00201 int num_elements_;
00202 T start_;
00203 T min_;
00204 T max_;
00205 char* bit_vector_;
00206 };
00207
00208 template <typename T>
00209 inline void RandomSequence<T>::Insert(const T& element) {
00210 int ele_ind = element - start_;
00211 if (ele_ind/8 > (max_+1)/8) {
00212 int diff = ((ele_ind + 1) / 8) - (( max_ + 1) / 8);
00213 bit_vector_ = (char *) realloc(bit_vector_, (ele_ind+1)/8 + 1);
00214 memset(&bit_vector_[((max_+1)/8) + 1], 0, diff);
00215 }
00216 Set(bit_vector_, ele_ind);
00217 if (ele_ind > max_) {
00218 max_ = ele_ind;
00219 }
00220 }
00221
00222 template <typename T>
00223 inline void RandomSequence<T>::Remove(const T& element) {
00224 }
00225
00226 template <typename T>
00227 inline int RandomSequence<T>::num_elements() const {
00228 return num_elements_;
00229 }
00230
00231 template <typename T>
00232 inline int RandomSequence<T>::mem_size() const {
00233
00234 return ((max_+1)/8 + 3);
00235 }
00236
00237
00238 #endif // RANDOM_SEQUENCE_H_