|
|
| version 1.30, 2001/04/04 10:50:36 | version 1.37, 2001/06/28 07:44:17 |
|---|---|
| Line 4 | Line 4 |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) |
| $Id$ | |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| /* | /* |
| The prime numbers used from zend_hash.c, | The prime numbers used from zend_hash.c, |
| Line 16 | Line 15 |
| For more information about Zend please visit http://www.zend.com/ | For more information about Zend please visit http://www.zend.com/ |
| */ | */ |
| #include "pa_config_includes.h" | |
| #include "pa_hash.h" | #include "pa_hash.h" |
| #include "pa_threads.h" | |
| void *Hash::Pair::operator new(size_t size, Pool& apool) { | void *Hash::Pair::operator new(size_t size, Pool& apool) { |
| return apool.malloc(size); | return apool.malloc(size); |
| Line 35 int Hash::allocates_count= | Line 31 int Hash::allocates_count= |
| sizeof(allocates)/sizeof(uint); | sizeof(allocates)/sizeof(uint); |
| void Hash::construct(Pool& apool, bool athread_safe) { | void Hash::construct(Pool& apool) { |
| fthread_safe=athread_safe; | |
| allocated=allocates[allocates_index=0]; | allocated=allocates[allocates_index=0]; |
| threshold=allocated*THRESHOLD_PERCENT/100; | threshold=allocated*THRESHOLD_PERCENT/100; |
| used=0; | used=0; |
| Line 51 void Hash::expand() { | Line 45 void Hash::expand() { |
| // allocated bigger refs array | // allocated bigger refs array |
| allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1; | allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1; |
| allocated=allocates[allocates_index]; | allocated=allocates[allocates_index]; |
| threshold=allocated*THRESHOLD_PERCENT/100; | |
| refs=static_cast<Pair **>(calloc(sizeof(Pair *)*allocated)); | refs=static_cast<Pair **>(calloc(sizeof(Pair *)*allocated)); |
| // rehash | // rehash |
| Line 82 uint Hash::generic_code(uint aresult, co | Line 77 uint Hash::generic_code(uint aresult, co |
| return result; | return result; |
| } | } |
| bool Hash::put(const Key& key, Val *value) { SYNCHRONIZED(fthread_safe); | bool Hash::put(const Key& key, Val *value) { |
| if(full()) | if(full()) |
| expand(); | expand(); |
| Line 103 bool Hash::put(const Key& key, Val *valu | Line 98 bool Hash::put(const Key& key, Val *valu |
| return false; | return false; |
| } | } |
| Hash::Val *Hash::get(const Key& key) const { SYNCHRONIZED(fthread_safe); | Hash::Val *Hash::get(const Key& key) const { |
| uint code=key.hash_code(); | uint code=key.hash_code(); |
| uint index=code%allocated; | uint index=code%allocated; |
| for(Pair *pair=refs[index]; pair; pair=pair->link) | for(Pair *pair=refs[index]; pair; pair=pair->link) |
| Line 113 Hash::Val *Hash::get(const Key& key) con | Line 108 Hash::Val *Hash::get(const Key& key) con |
| return 0; | return 0; |
| } | } |
| bool Hash::put_replace(const Key& key, Val *value) { SYNCHRONIZED(fthread_safe); | bool Hash::put_replace(const Key& key, Val *value) { |
| uint code=key.hash_code(); | uint code=key.hash_code(); |
| uint index=code%allocated; | uint index=code%allocated; |
| for(Pair *pair=refs[index]; pair; pair=pair->link) | for(Pair *pair=refs[index]; pair; pair=pair->link) |
| Line 127 bool Hash::put_replace(const Key& key, V | Line 122 bool Hash::put_replace(const Key& key, V |
| return false; | return false; |
| } | } |
| bool Hash::put_dont_replace(const Key& key, Val *value) { SYNCHRONIZED(fthread_safe); | bool Hash::put_dont_replace(const Key& key, Val *value) { |
| if(full()) | if(full()) |
| expand(); | expand(); |
| Line 147 bool Hash::put_dont_replace(const Key& k | Line 142 bool Hash::put_dont_replace(const Key& k |
| return false; | return false; |
| } | } |
| void Hash::merge_dont_replace(const Hash& src) { SYNCHRONIZED(fthread_safe); | void Hash::merge_dont_replace(const Hash& src) { |
| for(int i=0; i<src.allocated; i++) | for(int i=0; i<src.allocated; i++) |
| for(Pair *pair=src.refs[i]; pair; pair=pair->link) | for(Pair *pair=src.refs[i]; pair; pair=pair->link) |
| put_dont_replace(pair->key, pair->value); | put_dont_replace(pair->key, pair->value); |
| // MAY:optimize this.allocated==src.allocated case | // MAY:optimize this.allocated==src.allocated case |
| } | } |
| void Hash::for_each(For_each_func func, void *info) { | void Hash::for_each(For_each_func func, void *info) const { |
| Pair **ref=refs; | Pair **ref=refs; |
| for(int index=0; index<allocated; index++) | for(int index=0; index<allocated; index++) |
| for(Pair *pair=*ref++; pair; pair=pair->link) | for(Pair *pair=*ref++; pair; pair=pair->link) |
| Line 162 void Hash::for_each(For_each_func func, | Line 157 void Hash::for_each(For_each_func func, |
| (*func)(pair->key, pair->value, info); | (*func)(pair->key, pair->value, info); |
| } | } |
| void Hash::clear() { SYNCHRONIZED(fthread_safe); | Hash::Val* Hash::first_that(First_that_func func, void *info) const { |
| Pair **ref=refs; | |
| for(int index=0; index<allocated; index++) | |
| for(Pair *pair=*ref++; pair; pair=pair->link) | |
| if(pair->value) | |
| if((*func)(pair->key, pair->value, info)) | |
| return pair->value; | |
| return 0; | |
| } | |
| void Hash::clear() { | |
| memset(refs, 0, sizeof(*refs)*allocated); | memset(refs, 0, sizeof(*refs)*allocated); |
| used=0; | used=0; |
| } | } |