equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
68 } |
68 } |
69 |
69 |
70 _size = 16; // Size is a power of 2 |
70 _size = 16; // Size is a power of 2 |
71 _cnt = 0; // Dictionary is empty |
71 _cnt = 0; // Dictionary is empty |
72 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size); |
72 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size); |
73 memset(_bin,0,sizeof(bucket)*_size); |
73 memset((void*)_bin,0,sizeof(bucket)*_size); |
74 } |
74 } |
75 |
75 |
76 Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena, int size) |
76 Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena, int size) |
77 : _hash(inithash), _cmp(initcmp), _arena(arena) { |
77 : _hash(inithash), _cmp(initcmp), _arena(arena) { |
78 int i; |
78 int i; |
89 i=16; |
89 i=16; |
90 while( i < size ) i <<= 1; |
90 while( i < size ) i <<= 1; |
91 _size = i; // Size is a power of 2 |
91 _size = i; // Size is a power of 2 |
92 _cnt = 0; // Dictionary is empty |
92 _cnt = 0; // Dictionary is empty |
93 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size); |
93 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size); |
94 memset(_bin,0,sizeof(bucket)*_size); |
94 memset((void*)_bin,0,sizeof(bucket)*_size); |
95 } |
95 } |
96 |
96 |
97 //------------------------------~Dict------------------------------------------ |
97 //------------------------------~Dict------------------------------------------ |
98 // Delete an existing dictionary. |
98 // Delete an existing dictionary. |
99 Dict::~Dict() { |
99 Dict::~Dict() { |
125 // lo list depending on the value of the bit. |
125 // lo list depending on the value of the bit. |
126 void Dict::doubhash(void) { |
126 void Dict::doubhash(void) { |
127 uint oldsize = _size; |
127 uint oldsize = _size; |
128 _size <<= 1; // Double in size |
128 _size <<= 1; // Double in size |
129 _bin = (bucket*)_arena->Arealloc(_bin, sizeof(bucket) * oldsize, sizeof(bucket) * _size); |
129 _bin = (bucket*)_arena->Arealloc(_bin, sizeof(bucket) * oldsize, sizeof(bucket) * _size); |
130 memset(&_bin[oldsize], 0, oldsize * sizeof(bucket)); |
130 memset((void*)(&_bin[oldsize]), 0, oldsize * sizeof(bucket)); |
131 // Rehash things to spread into new table |
131 // Rehash things to spread into new table |
132 for (uint i = 0; i < oldsize; i++) { // For complete OLD table do |
132 for (uint i = 0; i < oldsize; i++) { // For complete OLD table do |
133 bucket *b = &_bin[i]; // Handy shortcut for _bin[i] |
133 bucket *b = &_bin[i]; // Handy shortcut for _bin[i] |
134 if (!b->_keyvals) continue; // Skip empties fast |
134 if (!b->_keyvals) continue; // Skip empties fast |
135 |
135 |
161 |
161 |
162 //------------------------------Dict----------------------------------------- |
162 //------------------------------Dict----------------------------------------- |
163 // Deep copy a dictionary. |
163 // Deep copy a dictionary. |
164 Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) { |
164 Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) { |
165 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size); |
165 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size); |
166 memcpy( _bin, d._bin, sizeof(bucket)*_size ); |
166 memcpy( (void*)_bin, (void*)d._bin, sizeof(bucket)*_size ); |
167 for( uint i=0; i<_size; i++ ) { |
167 for( uint i=0; i<_size; i++ ) { |
168 if( !_bin[i]._keyvals ) continue; |
168 if( !_bin[i]._keyvals ) continue; |
169 _bin[i]._keyvals=(void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2); |
169 _bin[i]._keyvals=(void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2); |
170 memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*)); |
170 memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*)); |
171 } |
171 } |
175 // Deep copy a dictionary. |
175 // Deep copy a dictionary. |
176 Dict &Dict::operator =( const Dict &d ) { |
176 Dict &Dict::operator =( const Dict &d ) { |
177 if( _size < d._size ) { // If must have more buckets |
177 if( _size < d._size ) { // If must have more buckets |
178 _arena = d._arena; |
178 _arena = d._arena; |
179 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size ); |
179 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size ); |
180 memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) ); |
180 memset( (void*)(&_bin[_size]), 0, (d._size-_size)*sizeof(bucket) ); |
181 _size = d._size; |
181 _size = d._size; |
182 } |
182 } |
183 uint i; |
183 uint i; |
184 for( i=0; i<_size; i++ ) // All buckets are empty |
184 for( i=0; i<_size; i++ ) // All buckets are empty |
185 _bin[i]._cnt = 0; // But leave bucket allocations alone |
185 _bin[i]._cnt = 0; // But leave bucket allocations alone |