00001 #ifndef OrderedEdgelistDynLookup_HPP
00002 #define OrderedEdgelistDynLookup_HPP
00003
00004 #include "datastructures/trie/edgelist/OrderedEdgelist.hpp"
00005 #include <vector>
00006
00007
00008
00009 namespace Bodon
00010 {
00016 template <class VECTOR, unsigned int THRESHOLD>
00017 class OrderedEdgelistDynLookup : public OrderedEdgelist<VECTOR>
00018 {
00019 protected:
00020 typedef OrderedEdgelist<VECTOR> PARENT;
00021 public:
00022 OrderedEdgelistDynLookup() : OrderedEdgelist<VECTOR>(){}
00023
00024 void* find(item_t item) const;
00025 void*& findOrCreate(item_t label);
00026 bool lookup(item_t label, void*& subtrie) const;
00027 void lookupNocheck(item_t label, void*& subtrie) const;
00028 void lookupNoUppercheck(item_t label, void*& subtrie) const
00029 {
00030 lookupNocheck(label, subtrie);
00031 }
00032 void lower_bound(typename VECTOR::iterator& it, item_t label);
00033 };
00034
00035 template <class VECTOR, unsigned int THRESHOLD>
00036 inline void* OrderedEdgelistDynLookup<VECTOR, THRESHOLD>::
00037 find(item_t label) const
00038 {
00039 typename VECTOR::const_iterator it;
00040 if(VECTOR::size() < THRESHOLD)
00041 {
00042 it = VECTOR::begin();
00043 while(it != VECTOR::end())
00044 if( (*it).first < label)
00045 ++it;
00046 else if( (*it).first == label)
00047 return (*it).second;
00048 else
00049 return NULL;
00050 return NULL;
00051 }
00052 else
00053 {
00054 it = std::lower_bound( VECTOR::begin(), VECTOR::end(), label );
00055 if( it != VECTOR::end() && (*it).first == label)
00056 return (*it).second;
00057 else
00058 return NULL;
00059 }
00060
00061 } template <class VECTOR, unsigned int THRESHOLD>
00062 inline void*& OrderedEdgelistDynLookup<VECTOR, THRESHOLD>::findOrCreate(
00063 item_t label)
00064 {
00065 typename VECTOR::iterator it;
00066 if(VECTOR::size() < THRESHOLD)
00067 {
00068 for(it = VECTOR::begin(); it != VECTOR::end(); ++it)
00069 if(*it >= label)
00070 break;
00071 }
00072 else
00073 it = std::lower_bound( VECTOR::begin(), VECTOR::end(), label );
00074 if( it == VECTOR::end() || (*it).first != label )
00075 it = std::vector<Edge>::insert(it, Edge(label, NULL));
00076 return (*it).second;
00077 }
00078
00079 template <class VECTOR, unsigned int THRESHOLD>
00080 inline bool OrderedEdgelistDynLookup<VECTOR, THRESHOLD>::lookup(
00081 item_t label, void*& subtrie) const
00082 {
00083 if(VECTOR::back() < label)
00084 return false;
00085 else
00086 {
00087 subtrie = NULL;
00088 typename VECTOR::const_iterator it;
00089 if(VECTOR::size() < THRESHOLD)
00090 {
00091 for( it = VECTOR::begin(); it != VECTOR::end() &&
00092 (*it).first <= label; ++it)
00093 if((*it).first == label)
00094 {
00095 subtrie = (*it).second;
00096 break;
00097 }
00098 }
00099 else
00100 {
00101 it = std::lower_bound( VECTOR::begin(), VECTOR::end(), label );
00102 if( (*it).first == label )
00103 subtrie = (*it).second;
00104 }
00105 return true;
00106 }
00107 }
00108
00109 template <class VECTOR, unsigned int THRESHOLD>
00110 inline void OrderedEdgelistDynLookup<VECTOR, THRESHOLD>::lookupNocheck(
00111 item_t label, void*& subtrie) const
00112 {
00113 subtrie = NULL;
00114 typename VECTOR::const_iterator it;
00115 if(VECTOR::size() < THRESHOLD)
00116 {
00117 for(it = VECTOR::begin(); it != VECTOR::end() && (*it).first <= label; ++it)
00118 if((*it).first == label)
00119 {
00120 subtrie = (*it).second;
00121 break;
00122 }
00123 }
00124 else
00125 {
00126 it = std::lower_bound( VECTOR::begin(), VECTOR::end(),
00127 label );
00128 if( (*it).first == label )
00129 subtrie = (*it).second;
00130 }
00131 }
00132 template <class VECTOR, unsigned int THRESHOLD>
00133 inline void OrderedEdgelistDynLookup<VECTOR, THRESHOLD>::
00134 lower_bound(typename VECTOR::iterator& it, item_t label)
00135 {
00136 if( static_cast<unsigned int>(VECTOR::end() - it) < THRESHOLD)
00137 while(it != VECTOR::end() && (*it).first < label) ++it;
00138 else
00139 it = std::lower_bound(it, VECTOR::end(), label);
00140 }
00141 }
00142
00143 #endif