00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef SST_CORE_SPARSEVECTORMAP_H
00015 #define SST_CORE_SPARSEVECTORMAP_H
00016
00017 #include "sst/core/sst_types.h"
00018 #include <sst/core/serialization.h>
00019
00020 #include <vector>
00021
00022 namespace SST {
00023
00024
00025 template <typename keyT, typename classT = keyT>
00026 class SparseVectorMap {
00027 private:
00028 std::vector<classT> data;
00029 int binary_search_insert(keyT id) const
00030 {
00031
00032
00033
00034 int size = data.size();
00035
00036
00037 if ( size == 0 ) return 0;
00038 if ( id < data[0].key() ) return 0;
00039 if ( id > data[size-1].key() ) return size;
00040
00041 int bottom = 0;
00042 int top = size - 1;
00043 int middle;
00044
00045 while ( bottom <= top ) {
00046 middle = bottom + (top - bottom) / 2;
00047 if ( id == data[middle].key() ) return -1;
00048 if ( id < data[middle].key() ) {
00049
00050
00051
00052 if ( id > data[middle-1].key() ) return middle;
00053 else {
00054 top = middle - 1;
00055 }
00056 }
00057 else {
00058 bottom = middle + 1;
00059 }
00060 }
00061
00062 return -1;
00063 }
00064
00065 int binary_search_find(keyT id) const
00066 {
00067 int bottom = 0;
00068 int top = data.size() - 1;
00069 int middle;
00070
00071 if ( data.size() == 0 ) return -1;
00072 while (bottom <= top) {
00073 middle = bottom + ( top - bottom ) / 2;
00074 if ( id == data[middle].key() ) return middle;
00075 else if ( id < data[middle].key() ) top = middle - 1;
00076 else bottom = middle + 1;
00077 }
00078 return -1;
00079 }
00080
00081 friend class boost::serialization::access;
00082 template<class Archive>
00083 void
00084 serialize(Archive & ar, const unsigned int version )
00085 {
00086 ar & BOOST_SERIALIZATION_NVP(data);
00087 }
00088
00089 friend class ConfigGraph;
00090
00091 public:
00092 typedef typename std::vector<classT>::iterator iterator;
00093 typedef typename std::vector<classT>::const_iterator const_iterator;
00094
00095
00096
00097
00098 void push_back(const classT& val)
00099 {
00100
00101
00102 if ( data.size() == 0 ) {
00103 data.push_back(val);
00104 return;
00105 }
00106 if ( val.key() > data[data.size()-1].key() ) {
00107 data.push_back(val);
00108 return;
00109 }
00110
00111
00112 insert(val);
00113 }
00114
00115 void insert(const classT& val)
00116 {
00117 int index = binary_search_insert(val.key());
00118 if ( index == -1 ) return;
00119 iterator it = data.begin();
00120 it += index;
00121 data.insert(it, val);
00122 }
00123
00124 iterator begin() { return data.begin(); }
00125 iterator end() { return data.end(); }
00126
00127 const_iterator begin() const { return data.begin(); }
00128 const_iterator end() const { return data.end(); }
00129
00130 bool contains(keyT id) const
00131 {
00132 if ( binary_search_find(id) == -1 ) return false;
00133 return true;
00134 }
00135
00136 classT& operator[] (keyT id)
00137 {
00138 int index = binary_search_find(id);
00139 if ( index == -1 ) {
00140
00141 }
00142 return data[index];
00143 }
00144
00145 const classT& operator[] (keyT id) const
00146 {
00147 int index = binary_search_find(id);
00148 if ( index == -1 ) {
00149
00150 }
00151 return data[index];
00152 }
00153
00154 void clear() { data.clear(); }
00155 size_t size() { return data.size(); }
00156
00157 };
00158
00159 template <typename keyT>
00160 class SparseVectorMap<keyT,keyT> {
00161 private:
00162 std::vector<keyT> data;
00163 int binary_search_insert(keyT id) const
00164 {
00165
00166
00167
00168 int size = data.size();
00169
00170
00171 if ( size == 0 ) return 0;
00172 if ( id < data[0] ) return 0;
00173 if ( id > data[size-1] ) return size;
00174
00175 int bottom = 0;
00176 int top = size - 1;
00177 int middle;
00178
00179 while ( bottom <= top ) {
00180 middle = bottom + (top - bottom) / 2;
00181 if ( id == data[middle] ) return -1;
00182 if ( id < data[middle] ) {
00183
00184
00185
00186 if ( id > data[middle-1] ) return middle;
00187 else {
00188 top = middle - 1;
00189 }
00190 }
00191 else {
00192 bottom = middle + 1;
00193 }
00194 }
00195
00196 return -1;
00197 }
00198
00199 int binary_search_find(keyT id) const
00200 {
00201 int bottom = 0;
00202 int top = data.size() - 1;
00203 int middle;
00204
00205 if ( data.size() == 0 ) return -1;
00206 while (bottom <= top) {
00207 middle = bottom + ( top - bottom ) / 2;
00208 if ( id == data[middle] ) return middle;
00209 else if ( id < data[middle] ) top = middle - 1;
00210 else bottom = middle + 1;
00211 }
00212 return -1;
00213 }
00214
00215 friend class boost::serialization::access;
00216 template<class Archive>
00217 void
00218 serialize(Archive & ar, const unsigned int version )
00219 {
00220 ar & BOOST_SERIALIZATION_NVP(data);
00221 }
00222
00223 friend class ConfigGraph;
00224
00225 public:
00226 typedef typename std::vector<keyT>::iterator iterator;
00227 typedef typename std::vector<keyT>::const_iterator const_iterator;
00228
00229
00230
00231
00232 void push_back(const keyT& val)
00233 {
00234
00235
00236 if ( data.size() == 0 ) {
00237 data.push_back(val);
00238 return;
00239 }
00240 if ( val.key() > data[data.size()-1].key() ) {
00241 data.push_back(val);
00242 return;
00243 }
00244
00245
00246 insert(val);
00247 }
00248
00249 void insert(const keyT& val)
00250 {
00251 int index = binary_search_insert(val);
00252 if ( index == -1 ) return;
00253 iterator it = data.begin();
00254 it += index;
00255 data.insert(it, val);
00256 }
00257
00258 iterator begin() { return data.begin(); }
00259 iterator end() { return data.end(); }
00260
00261 const_iterator begin() const { return data.begin(); }
00262 const_iterator end() const { return data.end(); }
00263
00264 bool contains(keyT id)
00265 {
00266 if ( binary_search_find(id) == -1 ) return false;
00267 return true;
00268 }
00269
00270 keyT& operator[] (keyT id)
00271 {
00272 int index = binary_search_find(id);
00273 if ( index == -1 ) {
00274
00275 }
00276 return data[index];
00277 }
00278
00279 const keyT& operator[] (keyT id) const
00280 {
00281 int index = binary_search_find(id);
00282 if ( index == -1 ) {
00283
00284 }
00285 return data[index];
00286 }
00287
00288 void clear() { data.clear(); }
00289 size_t size() { return data.size(); }
00290
00291 };
00292
00293
00294
00295 }
00296
00297 #endif // SST_CORE_CONFIGGRAPH_H