00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef DSR_PDB_SMALL_MAP_H
00023 #define DSR_PDB_SMALL_MAP_H
00024
00025 #include <vector>
00026 #include <algorithm>
00027
00028 namespace dsrpdb {
00029 template <class Key, class Data>
00030 class small_map {
00031 public:
00032 typedef std::pair<Key, Data> value_type;
00033 typedef Key key_type;
00034 typedef Data data_type;
00035 typedef std::vector<value_type> container;
00036 typedef typename container::iterator iterator;
00037 typedef typename container::const_iterator const_iterator;
00038
00039
00040 small_map(std::size_t sz=0){c_.reserve(sz);}
00041
00042 iterator find(key_type k) {
00043 for (iterator it= c_.begin(); it != c_.end(); ++it){
00044 if (it->first==k) return it;
00045 }
00046 return end();
00047 }
00048 const_iterator find(key_type k) const {
00049 for (const_iterator it= c_.begin(); it != c_.end(); ++it){
00050 if (it->first==k) return it;
00051 }
00052 return end();
00053 }
00054
00055 iterator begin() {
00056 return c_.begin();
00057 }
00058
00059 iterator end() {
00060 return c_.end();
00061 }
00062
00063 const_iterator begin() const {
00064 return c_.begin();
00065 }
00066
00067 const_iterator end() const {
00068 return c_.end();
00069 }
00070
00071 data_type& operator[](key_type k){
00072 iterator it= find(k);
00073 if (it != end()) return it->second;
00074 else {
00075 c_.push_back(value_type(k,data_type()));
00076 return c_.back().second;
00077 }
00078 }
00079
00080 void insert(const value_type &v) {
00081 c_.push_back(v);
00082 }
00083
00084 std::size_t size() const {
00085 return c_.size();
00086 }
00087
00088 protected:
00089 container c_;
00090 };
00091 }
00092 #endif