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
00026
00027
00028 #ifndef _terimber_varmap_h_
00029 #define _terimber_varmap_h_
00030
00031 #include "smart/varvalue.h"
00032 #include "smart/varfactory.h"
00033 #include "base/map.h"
00034 #include "base/list.h"
00035 #include "base/string.h"
00036 #include "smart/byterep.h"
00037
00038 BEGIN_TERIMBER_NAMESPACE
00039 #pragma pack(4)
00040
00046 class main_map_key
00047 {
00049 main_map_key& operator=(const main_map_key& x);
00050
00051 public:
00053 main_map_key(const main_map_key& x);
00054
00058 main_map_key( vt_types type,
00059 const var_value& val,
00060 variant_factory* factory
00061 );
00062
00065 main_map_key( const char* val,
00066 variant_factory* factory
00067 );
00068
00071 main_map_key( size_t fuzzyident,
00072 const char* val,
00073 variant_factory* factory
00074 );
00075
00076
00078 inline
00079 bool
00080 operator<(const main_map_key& x) const;
00082 inline
00083 bool
00084 partial_match(const main_map_key& x) const;
00085
00086 public:
00087 var_key _var_res;
00088 };
00089
00092 class main_map_key_compare
00093 {
00094 public:
00096 main_map_key_compare(vt_types type,
00097 bool partial,
00098 bool fuzzy
00099 ) :
00100 _type(type),
00101 _partial(partial),
00102 _fuzzy(fuzzy)
00103 {
00104 }
00106 inline
00107 bool
00108 operator()(const main_map_key& x, const main_map_key& y) const
00109 {
00110 if (_partial)
00111 return x < y;
00112 else if (_fuzzy)
00113 return x._var_res._ngram._fuzzy_key < y._var_res._ngram._fuzzy_key;
00114 else
00115 return operator_less(_type, x._var_res._val, y._var_res._val);
00116 }
00117 public:
00118 vt_types _type;
00119 bool _partial;
00120 bool _fuzzy;
00121 };
00122
00125 template < class T
00126 , class C
00127
00128 >
00129 class varmap
00130 {
00133 typedef node_allocator< TYPENAME base_map< T, bool >::_node > mainmap_object_allocator_t;
00136 typedef _map< T, bool, mainmap_object_allocator_t > mainmap_object_t;
00137
00138 public:
00142 typedef map< main_map_key, mainmap_object_t, main_map_key_compare > mainmap_t;
00145 typedef TYPENAME mainmap_t::const_iterator mainmap_citer_t;
00148 typedef TYPENAME mainmap_t::iterator mainmap_iter_t;
00151 typedef TYPENAME mainmap_t::pairib_t mainmap_pairib_t;
00152
00153 public:
00155 varmap( vt_types type,
00156 bool partial,
00157 bool fuzzy,
00158 variant_factory& factory
00159 );
00161 ~varmap();
00162
00164 mainmap_iter_t
00165 add_resource( const main_map_key& res,
00166 const T& x
00167 );
00169 mainmap_iter_t
00170 end();
00171
00174 bool
00175 find_exact_resource(const main_map_key& res,
00176 C& x,
00177 const T& min_filter,
00178 const T& max_filter
00179 ) const;
00180
00183 bool
00184 find_less_resource(const main_map_key& res,
00185 C& x,
00186 const T& min_filter,
00187 const T& max_filter,
00188 bool boundary_include
00189 ) const;
00192 bool
00193 find_greater_resource(const main_map_key& res,
00194 C& x,
00195 const T& min_filter,
00196 const T& max_filter,
00197 bool boundary_include
00198 ) const;
00199
00202 bool
00203 intersect_exact_resource(const main_map_key& res,
00204 C& x,
00205 const T& min_filter,
00206 const T& max_filter
00207 ) const;
00208
00211 bool
00212 intersect_less_resource(const main_map_key& res,
00213 C& x,
00214 const T& min_filter,
00215 const T& max_filter,
00216 bool boundary_include
00217 ) const;
00220 bool
00221 intersect_greater_resource(const main_map_key& res,
00222 C& x,
00223 const T& min_filter,
00224 const T& max_filter,
00225 bool boundary_include
00226 ) const;
00227
00229 bool
00230 remove_resource(mainmap_iter_t iter,
00231 const T& x
00232 );
00233
00236 size_t
00237 remove_all_resource(const T& x
00238 );
00239
00241 void
00242 fetch_all( C& x
00243 ) const;
00244
00246 void
00247 clear();
00248
00250 bool
00251 find_partial_resource(const main_map_key& res,
00252 bool deep,
00253 C& x,
00254 const T& min_filter,
00255 const T& max_filter
00256 ) const;
00258 bool
00259 intersect_partial_resource(const main_map_key& res,
00260 bool deep,
00261 C& x,
00262 const T& min_filter,
00263 const T& max_filter
00264 ) const;
00266 bool
00267 find_fuzzy_resource(const _list< size_t >& fuzzy_container,
00268 C& x,
00269 const T& min_filter,
00270 const T& max_filter
00271 ) const;
00273 bool
00274 intersect_fuzzy_resource(const _list< size_t >& fuzzy_container,
00275 C& x,
00276 const T& min_filter,
00277 const T& max_filter
00278 ) const;
00280 static
00281 void
00282 get_common_items(mainmap_citer_t c,
00283 const C& r,
00284 const T& min_filter,
00285 const T& max_filter,
00286 size_t& entries
00287 );
00289 static
00290 bool
00291 add_unique_items(mainmap_citer_t c,
00292 C& r,
00293 const T& min_filter,
00294 const T& max_filter
00295 );
00297 static
00298 void
00299 remove_uncommon_items(mainmap_citer_t c,
00300 C& r,
00301 const T& min_filter,
00302 const T& max_filter
00303 );
00305 static
00306 void
00307 mark_common_items(mainmap_citer_t c,
00308 C& r,
00309 const T& min_filter,
00310 const T& max_filter
00311 );
00312
00315 size_t
00316 get_main_object_allocator_pages() const;
00318 size_t
00319 get_main_object_allocator_capacity() const;
00321 template < class F >
00322 void
00323 retrive_stats( F& r
00324 )
00325 {
00326 for (mainmap_citer_t miter = _mainmap.begin(); miter != _mainmap.end(); ++miter)
00327 {
00328 r(miter);
00329 }
00330 }
00331
00332 protected:
00333 vt_types _type;
00334 main_map_key_compare _compare;
00335 mainmap_t _mainmap;
00336 mainmap_object_allocator_t _main_object_allocator;
00337 variant_factory& _factory;
00338 bool _partial;
00339 bool _fuzzy;
00340
00341 private:
00344 class offset_map_key
00345 {
00347 offset_map_key& operator=(const offset_map_key& x);
00348 public:
00350 offset_map_key(mainmap_iter_t iter,
00351 size_t offset
00352 ) :
00353 _iter(iter),
00354 _offset(offset),
00355 _pointer(0)
00356 {
00357 }
00359 offset_map_key(const char* x,
00360 size_t offset
00361 ):
00362 _pointer(x), _offset(offset)
00363 {
00364 }
00366 offset_map_key(const offset_map_key& x):
00367 _iter(x._iter), _offset(x._offset), _pointer(x._pointer)
00368 {
00369 }
00370
00372 inline
00373 bool
00374 operator<(const offset_map_key& x) const
00375 {
00376 if (x._pointer)
00377 return _offset != x._offset ? _offset < x._offset :
00378 str_template::strnocasecmp(_iter.key()._var_res._key._res + _iter.key()._var_res._key._offsets[1 + _offset], x._pointer, os_minus_one) < 0;
00379 else if (_pointer)
00380 return _offset != x._offset ? _offset < x._offset :
00381 str_template::strnocasecmp(_pointer, x._iter.key()._var_res._key._res + x._iter.key()._var_res._key._offsets[1 + x._offset], os_minus_one) < 0;
00382 else
00383 return _offset != x._offset ? _offset < x._offset :
00384 str_template::strnocasecmp(_iter.key()._var_res._key._res + _iter.key()._var_res._key._offsets[1 + _offset],
00385 x._iter.key()._var_res._key._res + x._iter.key()._var_res._key._offsets[1 + x._offset], os_minus_one) < 0;
00386 }
00388 inline
00389 bool
00390 partial_match(const main_map_key& x) const
00391 {
00392 return !str_template::strnocasecmp((const char*)_iter.key()._var_res._key._res + _iter.key()._var_res._key._offsets[1 + _offset],
00393 x._var_res._key._res ? x._var_res._key._res : "",
00394 x._var_res._key._res ? strlen(x._var_res._key._res) : 0);
00395 }
00396
00397 public:
00398
00399 size_t _offset;
00400 mainmap_iter_t _iter;
00401 const char* _pointer;
00402 };
00403
00406 typedef map< offset_map_key, bool, less< offset_map_key >, true > offsetmap_t;
00409 typedef typename offsetmap_t::const_iterator offsetmap_citer_t;
00412 typedef typename offsetmap_t::iterator offsetmap_iter_t;
00413 private:
00414 offsetmap_t _offsetmap;
00415 };
00416
00417
00418 #pragma pack()
00419 END_TERIMBER_NAMESPACE
00420
00421 #endif