Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

linmap.cpp

Go to the documentation of this file.
00001 
00009 #ifndef LINMAP_CPP_050402
00010 #define LINMAP_CPP_050402
00011 
00012 #include <inttypes.h>
00013 #include "common/allocators.hpp"
00014 
00015 namespace bracz {
00016 
00029 template<class key, class value> class linmap {
00030 public:
00031   typedef key key_type;
00032   typedef value data_type;
00033   typedef std::pair<key_type, data_type> value_type;
00034 
00035   //  **** I have no idea if and why this is required
00036   typedef std::allocator<linmap<key_type,value_type> > allocator;
00037 
00038   linmap() {
00039     clear();
00040   }
00041 
00042   void clear() {
00043     data=NULL;
00044     size=0;
00045     logsize=-1;
00046   }
00047 
00048   data_type & DINLINE  operator[](key_type k) {
00049     unsigned int pos=0;
00050     while((pos<size)&&(data[pos].first!=k)) {
00051       pos++;
00052     }
00053     if (pos>=size) {
00054       //insert
00055       size_t dlen=size?myallocator.BLOCKLEN(logsize):0;
00056       if (pos>=dlen) {
00057         //realloc
00058         value_type *nd=myallocator.allocate(logsize+1);
00059         if (size) {
00060           //sort(data,data+size);
00061           memcpy(nd,data,size*sizeof(value_type));
00062           myallocator.free(data,logsize);
00063         }
00064         logsize++;
00065         data=nd;
00066       }
00067       data[pos].first=k;
00068       data[pos].second=data_type();
00069       size++;
00070     }
00071     //swap with the previous
00072     /*if (pos) {
00073       swap(data[pos-1],data[pos]);
00074       pos--;
00075       }*/
00076     return data[pos].second;
00077   }
00078 
00079   template<class CmpFn> void sort(CmpFn fn=CmpFn()) {
00080     if (size>0) {
00081       std::sort(data,data+size,fn);
00082     }
00083   }
00084 
00085   class iterator {
00086   private:
00087     size_t pos;
00088     linmap<key_type,data_type> *parent;
00089   public:
00090     value_type& DINLINE operator*() const {
00091       return parent->data[pos];
00092     }
00093     
00094     value_type* DINLINE operator->() const {
00095       return parent->data+pos;
00096     }
00097 
00098     void DINLINE operator++ ()  {
00099       pos++;
00100       //return *this;
00101     }
00103     bool DINLINE  operator==(const iterator &o) const {
00104       return pos==o.pos;
00105     }
00106 
00107     bool DINLINE operator!=(const iterator &o) const {
00108       return pos!=o.pos;
00109     }
00110     
00111     inline iterator(linmap<key_type,data_type> *p,size_t s){
00112       parent=p;
00113       pos=s;
00114     }
00115     inline iterator() {
00116       parent=NULL;
00117       pos=0;
00118     }
00119   };
00120   friend class iterator;
00121 
00122   iterator DINLINE begin()  {
00123     return iterator(this,0);
00124   }
00125 
00126   iterator DINLINE end()  {
00127     return iterator(this,size);
00128   }
00129 
00130 private:
00132   uint32_t size;
00133   
00135   int logsize;
00136 
00138   value_type *data;
00139 
00141   typedef arrayalloc<value_type,1,100000> myallocator_t;
00142 
00143   static myallocator_t myallocator;
00144 
00145 };
00146 
00147 template<class key, class value> TYPENAME linmap<key,value>::myallocator_t linmap<key,value>::myallocator;
00148 
00149 
00150 } //namespace bracz
00151 
00152 #endif //LINMAP_CPP_050402

Generated on Sun Sep 17 17:50:39 2006 for FIM environment by  doxygen 1.4.4