|
|
| version 1.79, 2009/08/08 13:30:21 | version 1.87, 2015/04/06 22:27:26 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: hash class decl. | Parser: hash class decl. |
| Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 17 | Line 17 |
| #ifndef PA_HASH_H | #ifndef PA_HASH_H |
| #define PA_HASH_H | #define PA_HASH_H |
| static const char * const IDENT_HASH_H="$Date$"; | #define IDENT_PA_HASH_H "$Id$" |
| #include "pa_memory.h" | #include "pa_memory.h" |
| #include "pa_types.h" | #include "pa_types.h" |
| Line 120 public: | Line 120 public: |
| HASH() { | HASH() { |
| allocated=Hash_allocates[allocates_index=0]; | allocated=Hash_allocates[allocates_index=0]; |
| threshold=allocated*THRESHOLD_PERCENT/100; | |
| fpairs_count=fused_refs=0; | fpairs_count=fused_refs=0; |
| refs=new(UseGC) Pair*[allocated]; | refs=new Pair*[allocated]; |
| #ifdef HASH_ORDER | #ifdef HASH_ORDER |
| first=0; | first=0; |
| last=&first; | last=&first; |
| Line 132 public: | Line 131 public: |
| HASH(const HASH& source) { | HASH(const HASH& source) { |
| allocates_index=source.allocates_index; | allocates_index=source.allocates_index; |
| allocated=source.allocated; | allocated=source.allocated; |
| threshold=source.threshold; | |
| fused_refs=source.fused_refs; | fused_refs=source.fused_refs; |
| fpairs_count=source.fpairs_count; | fpairs_count=source.fpairs_count; |
| refs=new(UseGC) Pair*[allocated]; | refs=new Pair*[allocated]; |
| // clone & rehash | |
| #ifdef HASH_ORDER | #ifdef HASH_ORDER |
| first=0; | first=0; |
| last=&first; | last=&first; |
| #endif | for(Pair *pair=source.first; pair; pair=pair->next) |
| // clone & rehash | { |
| Pair **old_ref=source.refs; | uint index=pair->code%allocated; |
| for(int index=0; index<allocated; index++) | Pair **ref=&refs[index]; |
| for(Pair *pair=*old_ref++; pair; ) { | HASH_NEW_PAIR(pair->code, pair->key, pair->value); |
| Pair *next=pair->link; | } |
| #else | |
| Pair **ref=&refs[index]; | for(int i=0; i<source.allocated; i++) |
| for(Pair *pair=source.refs[i]; pair; pair=pair->link) | |
| { | |
| Pair **ref=&refs[i]; | |
| HASH_NEW_PAIR(pair->code, pair->key, pair->value); | HASH_NEW_PAIR(pair->code, pair->key, pair->value); |
| pair=next; | |
| } | } |
| #endif | |
| } | } |
| #ifdef USE_DESTRUCTORS | #ifdef USE_DESTRUCTORS |
| Line 242 public: | Line 243 public: |
| return V(0); | return V(0); |
| } | } |
| #ifdef HASH_ORDER | |
| String::Body first_key() const { | |
| return (first) ? String::Body(first->key, first->code) : String::Body(); | |
| } | |
| V first_value() const { | |
| return (first) ? first->value : V(0); | |
| } | |
| String::Body last_key() const { | |
| if (fpairs_count) { | |
| Pair* pair = (Pair*)((char *)last - offsetof(Pair, next)); | |
| return String::Body(pair->key, pair->code); | |
| } else { | |
| return String::Body(); | |
| } | |
| } | |
| V last_value() const { | |
| return (fpairs_count) ? ((Pair *)((char *)last - offsetof(Pair, next)))->value : V(0); | |
| } | |
| #endif | |
| /// put a [value] under the [key] if that [key] existed @returns existed or not | /// put a [value] under the [key] if that [key] existed @returns existed or not |
| bool put_replaced(K key, V value) { | bool put_replaced(K key, V value) { |
| if(!value) { | if(!value) { |
| Line 333 public: | Line 357 public: |
| protected: | protected: |
| /// expand when these %% of allocated exausted | |
| enum { | |
| THRESHOLD_PERCENT=75 | |
| }; | |
| /// the index of [allocated] in [Hash_allocates] | /// the index of [allocated] in [Hash_allocates] |
| int allocates_index; | int allocates_index; |
| /// number of allocated pairs | /// number of allocated pairs |
| int allocated; | int allocated; |
| /// helper: expanding when fused_refs == threshold | |
| int threshold; | |
| /// used pairs | /// used pairs |
| int fused_refs; | int fused_refs; |
| Line 376 protected: | Line 392 protected: |
| Pair **last; | Pair **last; |
| #endif | #endif |
| /// filled to threshold: needs expanding | /// filled to threshold (THRESHOLD_PERCENT=75), needs expanding |
| bool is_full() { return fused_refs==threshold; } | bool is_full() { return fused_refs + allocated/4 >= allocated; } |
| /// allocate larger buffer & rehash | /// allocate larger buffer & rehash |
| void expand() { | void expand() { |
| int old_allocated=allocated; | int old_allocated=allocated; |
| Pair **old_refs=refs; | Pair **old_refs=refs; |
| allocates_index=allocates_index+1<HASH_ALLOCATES_COUNT?allocates_index+1:HASH_ALLOCATES_COUNT-1; | if (allocates_index<HASH_ALLOCATES_COUNT-1) allocates_index++; |
| // allocated bigger refs array | // allocated bigger refs array |
| allocated=Hash_allocates[allocates_index]; | allocated=Hash_allocates[allocates_index]; |
| threshold=allocated*THRESHOLD_PERCENT/100; | refs=new Pair*[allocated]; |
| refs=new(UseGC) Pair*[allocated]; | |
| // rehash | // rehash |
| Pair **old_ref=old_refs; | Pair **old_ref=old_refs; |
| Line 586 public: | Line 601 public: |
| return pair->value; | return pair->value; |
| return V(0); | return V(0); |
| } | } |
| /// simple hash iterator | |
| class Iterator { | |
| const HASH_STRING<V>& fhash; | |
| Pair *fcurrent; | |
| public: | |
| Iterator(const HASH_STRING<V>& ahash): fhash(ahash) { | |
| fcurrent=fhash.first; | |
| } | |
| operator bool () { | |
| return fcurrent != 0; | |
| } | |
| void next() { | |
| fcurrent=fcurrent->next; | |
| } | |
| String::Body key(){ | |
| return String::Body(fcurrent->key, fcurrent->code); | |
| } | |
| V value(){ | |
| return fcurrent->value; | |
| } | |
| }; | |
| }; | }; |
| #else //HASH_CODE_CACHING | #else //HASH_CODE_CACHING |
| Line 595 template<typename V> class HASH_STRING: | Line 636 template<typename V> class HASH_STRING: |
| #ifndef HASH_ORDER | #ifndef HASH_ORDER |
| /// Auto-object used to temporarily substituting/removing string hash values | /// Auto-object used to temporarily substituting/removing string hash values |
| template <typename K, typename V> | template <typename H, typename V> |
| class Temp_hash_value { | class Temp_hash_value { |
| HashString<V> &fhash; | H *fhash; |
| K fname; | String::Body fname; |
| V saved_value; | V saved_value; |
| public: | public: |
| Temp_hash_value(HashString<V>& ahash, K aname, V avalue) : | Temp_hash_value(H *ahash, String::Body aname, V avalue) : fhash(ahash), fname(aname) { |
| fhash(ahash), | if(fhash){ |
| fname(aname), | saved_value=fhash->get(aname); |
| saved_value(ahash.get(aname)) { | fhash->put(aname, avalue); |
| fhash.put(aname, avalue); | } |
| } | } |
| ~Temp_hash_value() { | ~Temp_hash_value() { |
| fhash.put(fname, saved_value); | if(fhash) |
| fhash->put(fname, saved_value); | |
| } | } |
| }; | }; |
| #endif | #endif |