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
00028 #include <coconut_config.h>
00029 #include <datamap.h>
00030
00031 namespace coco {
00032
00033
00034 bool datamap::sinsert(const std::string& __s, const basic_alltype& __h,
00035 bool remove)
00036 {
00037 _Base::iterator __b(this->lower_bound(__s));
00038
00039 if(__b != end() && (*__b).first == __s)
00040 {
00041 if(!remove) return false;
00042 (*__b).second = __h;
00043 }
00044 else
00045 insert(__b, std::make_pair(__s, __h));
00046 return true;
00047 }
00048
00049 std::string datamap::encode(const std::string& __s, int i) const
00050 {
00051 char buf[30];
00052 sprintf(buf, "%+020d", i);
00053 return __s + __DATAMAP_SEPARATOR + buf;
00054 }
00055
00056 bool datamap::update_index(const std::string& __s, int i, bool __insert)
00057 {
00058 _Base::iterator __b = find(__s + __DATAMAP_SEPARATOR + __DATAMAP_IDXSPEC);
00059 std::vector<int> _iv;
00060 bool update(false);
00061
00062 if(__b != end())
00063 _iv = __b->second.n();
00064 std::vector<int>::iterator _lb = std::lower_bound(_iv.begin(), _iv.end(), i);
00065 if(__insert)
00066 {
00067 if(_lb == _iv.end() || *_lb != i)
00068 {
00069 update = true;
00070 _iv.insert(_lb, i);
00071 }
00072 }
00073 else
00074 {
00075 if(_lb != _iv.end() && *_lb == i)
00076 {
00077 update = true;
00078 _iv.erase(_lb);
00079 }
00080 }
00081 if(update)
00082 {
00083 if(__b != end()) erase(__b);
00084 insert(std::make_pair(__s + __DATAMAP_SEPARATOR + __DATAMAP_IDXSPEC, _iv));
00085 }
00086 return update;
00087 }
00088
00089 bool datamap::check_index(const std::string& __s, int i) const
00090 {
00091 _Base::const_iterator __b = find(__s + __DATAMAP_SEPARATOR + __DATAMAP_IDXSPEC);
00092 bool ret(false);
00093
00094 if(__b == end())
00095 return ret;
00096 return std::binary_search(__b->second.n().begin(), __b->second.n().end(), i);
00097 }
00098
00099 void datamap::list(std::vector<std::string>& __v) const
00100 {
00101 _Base::const_iterator __e(end()), __c;
00102 for(__c = begin(); __c != __e; ++__c)
00103 __v.push_back((*__c).first);
00104 }
00105
00106 }