1 /*
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.
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
144
145 for (j = 0; j < b->_cnt; ) { // Rehash all keys in this bucket
146 void *key = b->_keyvals[j + j];
147 if ((_hash(key) & (_size-1)) != i) { // Moving to hi bucket?
148 nb->_keyvals[nbcnt + nbcnt] = key;
149 nb->_keyvals[nbcnt + nbcnt + 1] = b->_keyvals[j + j + 1];
150 nb->_cnt = nbcnt = nbcnt + 1;
151 b->_cnt--; // Remove key/value from lo bucket
152 b->_keyvals[j + j] = b->_keyvals[b->_cnt + b->_cnt];
153 b->_keyvals[j + j + 1] = b->_keyvals[b->_cnt + b->_cnt + 1];
154 // Don't increment j, hash compacted element also.
155 } else {
156 j++; // Iterate.
157 }
158 } // End of for all key-value pairs in bucket
159 } // End of for all buckets
160 }
161
162 //------------------------------Dict-----------------------------------------
163 // Deep copy a dictionary.
164 Dict::Dict( const Dict &d ) : _arena(d._arena), _size(d._size), _cnt(d._cnt), _hash(d._hash), _cmp(d._cmp) {
165 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
166 memcpy( (void*)_bin, (void*)d._bin, sizeof(bucket)*_size );
167 for( uint i=0; i<_size; i++ ) {
168 if( !_bin[i]._keyvals ) continue;
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*));
171 }
172 }
173
174 //------------------------------Dict-----------------------------------------
175 // Deep copy a dictionary.
176 Dict &Dict::operator =( const Dict &d ) {
177 if( _size < d._size ) { // If must have more buckets
178 _arena = d._arena;
179 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
180 memset( (void*)(&_bin[_size]), 0, (d._size-_size)*sizeof(bucket) );
181 _size = d._size;
182 }
183 uint i;
184 for( i=0; i<_size; i++ ) // All buckets are empty
|
1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
144
145 for (j = 0; j < b->_cnt; ) { // Rehash all keys in this bucket
146 void *key = b->_keyvals[j + j];
147 if ((_hash(key) & (_size-1)) != i) { // Moving to hi bucket?
148 nb->_keyvals[nbcnt + nbcnt] = key;
149 nb->_keyvals[nbcnt + nbcnt + 1] = b->_keyvals[j + j + 1];
150 nb->_cnt = nbcnt = nbcnt + 1;
151 b->_cnt--; // Remove key/value from lo bucket
152 b->_keyvals[j + j] = b->_keyvals[b->_cnt + b->_cnt];
153 b->_keyvals[j + j + 1] = b->_keyvals[b->_cnt + b->_cnt + 1];
154 // Don't increment j, hash compacted element also.
155 } else {
156 j++; // Iterate.
157 }
158 } // End of for all key-value pairs in bucket
159 } // End of for all buckets
160 }
161
162 //------------------------------Dict-----------------------------------------
163 // Deep copy a dictionary.
164 Dict::Dict( const Dict &d ) : ResourceObj(d), _arena(d._arena), _size(d._size), _cnt(d._cnt), _hash(d._hash), _cmp(d._cmp) {
165 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
166 memcpy( (void*)_bin, (void*)d._bin, sizeof(bucket)*_size );
167 for( uint i=0; i<_size; i++ ) {
168 if( !_bin[i]._keyvals ) continue;
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*));
171 }
172 }
173
174 //------------------------------Dict-----------------------------------------
175 // Deep copy a dictionary.
176 Dict &Dict::operator =( const Dict &d ) {
177 if( _size < d._size ) { // If must have more buckets
178 _arena = d._arena;
179 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
180 memset( (void*)(&_bin[_size]), 0, (d._size-_size)*sizeof(bucket) );
181 _size = d._size;
182 }
183 uint i;
184 for( i=0; i<_size; i++ ) // All buckets are empty
|