--- parser3/src/include/pa_hash.h 2016/07/04 17:26:23 1.95 +++ parser3/src/include/pa_hash.h 2025/05/26 00:52:15 1.107 @@ -1,23 +1,15 @@ /** @file Parser: hash class decl. - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) -*/ - -/* - The prime numbers used from zend_hash.c, - the part of Zend scripting engine library, - Copyrighted (C) 1999-2000 Zend Technologies Ltd. - http://www.zend.com/license/0_92.txt - For more information about Zend please visit http://www.zend.com/ + Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_HASH_H #define PA_HASH_H -#define IDENT_PA_HASH_H "$Id: pa_hash.h,v 1.95 2016/07/04 17:26:23 moko Exp $" +#define IDENT_PA_HASH_H "$Id: pa_hash.h,v 1.107 2025/05/26 00:52:15 moko Exp $" #include "pa_memory.h" #include "pa_types.h" @@ -25,15 +17,12 @@ const int HASH_ALLOCATES_COUNT=29; -/** Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster - - paf: HPUX ld could not handle static member: unsatisfied symbols -*/ +// nearest primes to 5 * 2^n static uint Hash_allocates[HASH_ALLOCATES_COUNT]={ - 5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, - 16229, 32531, 65407, 130987, 262237, 524521, 1048793, - 2097397, 4194103, 8388857, 16777447, 33554201, 67108961, - 134217487, 268435697, 536870683, 1073741621, 2147483399}; + 5, 11, 19, 41, 79, 163, 317, 641, 1279, 2557, 5119, + 10243, 20479, 40961, 81919, 163841, 327673, 655357, + 1310719, 2621447, 5242883, 10485767, 20971529, 41943049, + 83886091, 167772161, 335544323, 671088637, 1342177283}; /// useful generic hash function inline void generic_hash_code(uint& result, char c) { @@ -115,6 +104,9 @@ inline uint hash_code(int self) { #endif +#define HASH_PUT_CSTR(h, cstr, v) \ + { static const String::Body body_##__LINE__(cstr); (h).put(body_##__LINE__, (v)); } + template class HASH: public PA_Object { protected: class Pair; @@ -125,7 +117,7 @@ public: HASH() { allocated=Hash_allocates[allocates_index=0]; fpairs_count=fused_refs=0; - refs=new Pair*[allocated]; + refs=new(PointerGC) Pair*[allocated]; HASH_ORDER_CLEAR(); } @@ -134,7 +126,7 @@ public: allocated=source.allocated; fused_refs=source.fused_refs; fpairs_count=source.fpairs_count; - refs=new Pair*[allocated]; + refs=new(PointerGC) Pair*[allocated]; // clone & rehash #ifdef HASH_ORDER HASH_ORDER_CLEAR(); @@ -163,7 +155,7 @@ public: delete pair; pair=next; } - delete[] refs; + operator delete[](refs); } #endif @@ -255,9 +247,12 @@ public: return (first) ? first->value : V(0); } + inline Pair* last_pair() const { + return (fpairs_count) ? (Pair*)((char *)last - offsetof(Pair, next)) : NULL; + } + String::Body last_key() const { - if (fpairs_count) { - Pair* pair = (Pair*)((char *)last - offsetof(Pair, next)); + if(Pair* pair = last_pair()) { #ifdef HASH_CODE_CACHING return String::Body(pair->key, pair->code); #else @@ -269,7 +264,9 @@ public: } V last_value() const { - return (fpairs_count) ? ((Pair *)((char *)last - offsetof(Pair, next)))->value : V(0); + if(Pair* pair = last_pair()) + return pair->value; + return NULL; } void order_clear() { @@ -282,7 +279,6 @@ public: *last=pair; last=&(pair->next); } - #endif //HASH_ORDER /// put a [value] under the [key] if that [key] existed @returns existed or not @@ -419,7 +415,7 @@ protected: if (allocates_indexallocated; + uint index=code % this->allocated; Pair **ref=&this->refs[index]; for(Pair *pair=*ref; pair; pair=pair->link) if(pair->code==code && CORD_cmp(pair->key,key)==0) { @@ -487,11 +483,15 @@ public: return false; } + bool put(const char *str, V value) { + return put(String::Body(str), value); + } + /// remove the [key] @returns existed or not bool remove(K str) { CORD key=str.get_cord(); uint code=str.get_hash_code(); - uint index=code%this->allocated; + uint index=code % this->allocated; for(Pair **ref=&this->refs[index]; *ref; ref=&(*ref)->link){ Pair *pair=*ref; if(pair->code==code && CORD_cmp(pair->key,key)==0) { @@ -517,7 +517,7 @@ public: bool contains(K str){ CORD key=str.get_cord(); uint code=str.get_hash_code(); - uint index=code%this->allocated; + uint index=code % this->allocated; for(Pair *pair=this->refs[index]; pair; pair=pair->link){ if(pair->code==code && CORD_cmp(pair->key,key)==0) return true; @@ -530,7 +530,7 @@ public: V get(K str) const { CORD key=str.get_cord(); uint code=str.get_hash_code(); - uint index=code%this->allocated; + uint index=code % this->allocated; for(Pair *pair=this->refs[index]; pair; pair=pair->link) if(pair->code==code && CORD_cmp(pair->key,key)==0) return pair->value; @@ -546,12 +546,12 @@ public: } else { key=0; } - uint index=code%this->allocated; + uint index=code % this->allocated; for(Pair *pair=this->refs[index]; pair; pair=pair->link) if(pair->code==code && CORD_cmp(pair->key,(CORD)key)==0) return pair->value; - return V(0); + return V((const char *)0); } /// put a [value] under the [key] if that [key] existed @returns existed or not @@ -563,7 +563,7 @@ public: CORD key=str.get_cord(); uint code=str.get_hash_code(); - uint index=code%this->allocated; + uint index=code % this->allocated; for(Pair *pair=this->refs[index]; pair; pair=pair->link) if(pair->code==code && CORD_cmp(pair->key,key)==0) { // found a pair with the same key, replacing @@ -586,7 +586,7 @@ public: CORD key=str.get_cord(); uint code=str.get_hash_code(); - uint index=code%this->allocated; + uint index=code % this->allocated; Pair **ref=&this->refs[index]; for(Pair *pair=*ref; pair; pair=pair->link) if(pair->code==code && CORD_cmp(pair->key,key)==0) { @@ -602,6 +602,44 @@ public: return false; } + /// rename $.from[] to $.to[] + void rename(K from, K to) { + CORD key_from=from.get_cord(); + uint code_from=from.get_hash_code(); + uint index_from=code_from % this->allocated; + + CORD key_to=to.get_cord(); + uint code_to=to.get_hash_code(); + uint index_to=code_to % this->allocated; + + if(code_from == code_to && CORD_cmp(key_from, key_to)==0) + return; + + for(Pair **ref=&this->refs[index_from]; *ref; ref=&(*ref)->link){ + Pair *pair=*ref; + if(pair->code==code_from && CORD_cmp(pair->key,key_from)==0) { + // found a pair with the required key + + // remove it from the from key first + Pair *next=pair->link; + *ref=next; + + // to simplify code + remove(to); + + // change pair key + pair->code=code_to; + (CORD &)(pair->key)=key_to; + + // link to the to key, hash order left intact + if(!(pair->link=this->refs[index_to])) // root was unused? + this->fused_refs++; // we've used it and record the fact + this->refs[index_to]=pair; + return; + } + } + } + /// put all 'src' values if NO with same key existed void merge_dont_replace(const HASH_STRING& src) { #ifdef HASH_ORDER @@ -675,7 +713,7 @@ public: #endif operator bool () { - return fcurrent != 0; + return fcurrent != NULL; } String::Body key(){ @@ -695,6 +733,42 @@ public: } }; +#ifdef HASH_ORDER + /// simple reverse hash iterator + class ReverseIterator { + const HASH_STRING& fhash; + Pair *fcurrent; + public: + ReverseIterator(const HASH_STRING& ahash): fhash(ahash) { + fcurrent=fhash.last_pair(); + } + + void prev() { + fcurrent=(fcurrent->prev == &fhash.first) ? NULL : (Pair*)((char *)fcurrent->prev - offsetof(Pair, next)); + } + + operator bool () { + return fcurrent != NULL; + } + + String::Body key(){ +#ifdef HASH_CODE_CACHING + return String::Body(fcurrent->key, fcurrent->code); +#else + return fcurrent->key; +#endif + } + + V value(){ + return fcurrent->value; + } + + Pair *pair(){ + return fcurrent; + } + }; +#endif + }; #ifndef HASH_ORDER