00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef SGI_bucket_container_h_INCLUDED
00042 #define SGI_bucket_container_h_INCLUDED
00043
00044 #include <iterator.h>
00045 #include <vector>
00046 #include <algorithm>
00047
00048 namespace SGI {
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 template <class ForwardIterator, class IndexFunction, class Container>
00084 inline void
00085 counts_in_buckets_unguarded(ForwardIterator first, ForwardIterator last, IndexFunction i_fun, Container& c)
00086 {
00087 for (ForwardIterator p = first; p != last; ++p) {
00088 Container::size_type idx = i_fun(*p);
00089 ++c[idx];
00090 }
00091 }
00092
00093 template <class ForwardIterator, class IndexFunction, class Container>
00094 void counts_in_buckets(ForwardIterator first, ForwardIterator last, IndexFunction i_fun, Container& c)
00095 {
00096 fill(c.begin(), c.end(), 0);
00097 for (ForwardIterator p = first; p != last; ++p) {
00098 Container::size_type idx = i_fun(*p);
00099 Container::size_type needed = idx+1;
00100 if (needed > c.size())
00101 c.insert(c.end(), needed - c.size(), 0);
00102 ++c[idx];
00103 }
00104 }
00105
00106 template <class ForwardIterator, class IndexFunction, class Container, class Size_type>
00107 void counts_in_buckets(ForwardIterator first,
00108 ForwardIterator last,
00109 IndexFunction i_fun,
00110 Container& c,
00111 Size_type max_index)
00112 {
00113 fill(c.begin(), c.end(), 0);
00114 Container::size_type needed = max_index+1;
00115 if (needed > c.size())
00116 c.insert(c.end(), needed - c.size(), 0);
00117 counts_in_buckets_unguarded(first, last, i_fun, c);
00118 }
00119
00120
00121 template <class T, class IndexFunction>
00122 class bucket_vector {
00123 public:
00124 typedef T value_type;
00125 typedef IndexFunction index_function;
00126 typedef vector<T>::iterator iterator;
00127 typedef vector<T>::size_type size_type;
00128
00129 protected:
00130 typedef bucket_vector<T, IndexFunction> self;
00131 typedef vector<iterator>::iterator index_iterator;
00132 typedef vector<iterator>::iterator index_iterator;
00133
00134 public:
00135 class bucket_type {
00136 friend self;
00137 private:
00138 index_iterator p;
00139 bucket_type(index_iterator x) : p(x) {}
00140 public:
00141 typedef bucket_vector<T, IndexFunction>::iterator iterator;
00142 iterator begin() { return *p; }
00143 iterator end() { return *(p + 1); }
00144 size_type size() { return end() - begin(); }
00145 bool empty() { begin() == end(); }
00146 };
00147 IndexFunction i_fun;
00148
00149 protected:
00150 vector<T> data;
00151 vector<iterator> index;
00152
00153 template <class ForwardIterator>
00154 void fill_bucket(ForwardIterator first, ForwardIterator last, vector<size_type>& count)
00155 {
00156 count.insert(count.begin(), 0);
00157 partial_sum(count.begin(), count.end(), count.begin());
00158 size_type total_count = *(count.end()-1);
00159 if (total_count > data.size())
00160 data.insert(data.end(), total_count - data.size(), *first);
00161
00162 for (iterator p = first; p != last; ++p) {
00163 size_type idx = i_fun(*p);
00164 data[count[idx]] = *p;
00165 ++(count[idx]);
00166 }
00167 if (count.size() > index.size())
00168 index.insert(index.end(), count.size() - index.size(), 0);
00169
00170 index[0] = data.begin();
00171 for (size_type i = 0; i < count.size()-1; ++i) {
00172 index[i+1] = data.begin() + count[i];
00173 }
00174 }
00175 template <class ForwardIterator>
00176 void fill_bucket(ForwardIterator first, ForwardIterator last)
00177 {
00178 vector<size_type> count;
00179 counts_in_buckets(first, last, i_fun, count);
00180 fill_bucket(first, last, count);
00181 }
00182 template <class ForwardIterator>
00183 void fill_bucket(ForwardIterator first, ForwardIterator last, size_type max_index)
00184 {
00185 vector<size_type> count;
00186 counts_in_buckets(first, last, i_fun, count, max_index);
00187 fill_bucket(first, last, count);
00188 }
00189
00190 public:
00191 bucket_type bucket(size_type n) { return bucket_type(index.begin() + n); }
00192 size_type number_of_buckets() const { return index.size() - 1; }
00193
00194 iterator begin() { return data.begin(); }
00195 iterator end() { return data.end(); }
00196 size_type size() { return data.size(); }
00197
00198 template <class ForwardIterator>
00199 bucket_vector(ForwardIterator first, ForwardIterator last) { fill_bucket(first, last); }
00200
00201 template <class ForwardIterator>
00202 bucket_vector(ForwardIterator first,
00203 ForwardIterator last,
00204 size_type max_index) {
00205 fill_bucket(first, last, max_index);
00206 }
00207
00208 template <class ForwardIterator, class ForwardIterator2>
00209 bucket_vector(ForwardIterator first, ForwardIterator last,
00210 ForwardIterator2 first_count, ForwardIterator2 last_count) {
00211 vector<size_type> count(first_count, last_count);
00212 fill_bucket(first, last, count.size());
00213 }
00214 };
00215
00216 }
00217
00218 #endif
00219
00220
00221
00222