1 #define PERL_NO_GET_CONTEXT
7 MODULE = Hash::Util PACKAGE = Hash::Util
10 _clear_placeholders(hashref)
16 hv = MUTABLE_HV(hashref);
17 hv_clear_placeholders(hv);
20 all_keys(hash,keys,placeholder)
30 av_clear(placeholder);
32 (void)hv_iterinit(hash);
33 while((he = hv_iternext_flags(hash, HV_ITERNEXT_WANTPLACEHOLDERS))!= NULL) {
35 av_push(HeVAL(he) == &PL_sv_placeholder ? placeholder : keys,
44 Hash::Util::legal_ref_keys = 1
49 (void)hv_iterinit(hash);
50 while((he = hv_iternext_flags(hash, HV_ITERNEXT_WANTPLACEHOLDERS))!= NULL) {
52 if (ix || HeVAL(he) == &PL_sv_placeholder) {
58 hv_store(hash, key, val)
66 if (!hv_store_ent(hash, key, val, 0)) {
78 mXPUSHs(newSVpvn((char *)PERL_HASH_SEED,PERL_HASH_SEED_BYTES));
83 hash_value(string,...)
90 char *pv= SvPV(string,len);
92 PERL_HASH(uv, pv, len);
95 U8 *seedbuf= (U8 *)SvPV(ST(1),seedlen);
96 if ( seedlen < PERL_HASH_SEED_BYTES ) {
98 Perl_croak(aTHX_ "seed len must be at least %d long only got %"
99 UVuf " bytes", PERL_HASH_SEED_BYTES, (UV)seedlen);
102 PERL_HASH_WITH_SEED(seedbuf, uv, pv, len);
108 hash_traversal_mask(rhv, ...)
112 #ifdef PERL_HASH_RANDOMIZE_KEYS
113 if (SvROK(rhv) && SvTYPE(SvRV(rhv))==SVt_PVHV && !SvMAGICAL(SvRV(rhv))) {
114 HV *hv = (HV *)SvRV(rhv);
116 hv_rand_set(hv, SvUV(ST(1)));
119 XSRETURN_UV(HvRAND_get(hv));
125 Perl_croak(aTHX_ "Perl has not been compiled with support for randomized hash key traversal");
136 Takes a non-magical hash ref as an argument and returns a list of
137 statistics about the hash. The number and keys and the size of the
138 array will always be reported as the first two values. If the array is
139 actually allocated (they are lazily allocated), then additionally
140 will return a list of counts of bucket lengths. In other words in
142 ($keys, $buckets, $used, @length_count)= hash::bucket_info(\%hash);
144 $length_count[0] is the number of empty buckets, and $length_count[1]
145 is the number of buckets with only one key in it, $buckets - $length_count[0]
146 gives the number of used buckets, and @length_count-1 is the maximum
149 If the argument is not a hash ref, or if it is magical, then returns
150 nothing (the empty list).
153 const HV * hv = NULL;
154 if (SvROK(rhv) && SvTYPE(SvRV(rhv))==SVt_PVHV && !SvMAGICAL(SvRV(rhv))) {
155 hv = (const HV *) SvRV(rhv);
156 } else if (!SvOK(rhv)) {
160 U32 max_bucket_index= HvMAX(hv);
161 U32 total_keys= HvUSEDKEYS(hv);
162 HE **bucket_array= HvARRAY(hv);
164 mXPUSHi(max_bucket_index+1);
165 mXPUSHi(0); /* for the number of used buckets */
166 #define BUCKET_INFO_ITEMS_ON_STACK 3
168 XSRETURN(BUCKET_INFO_ITEMS_ON_STACK);
170 /* we use chain_length to index the stack - we eliminate an add
171 * by initializing things with the number of items already on the stack.
172 * If we have 2 items then ST(2+0) (the third stack item) will be the counter
173 * for empty chains, ST(2+1) will be for chains with one element, etc.
175 I32 max_chain_length= BUCKET_INFO_ITEMS_ON_STACK - 1; /* so we do not have to do an extra push for the 0 index */
178 for ( bucket_index= 0; bucket_index <= max_bucket_index; bucket_index++ ) {
179 I32 chain_length= BUCKET_INFO_ITEMS_ON_STACK;
180 for (he= bucket_array[bucket_index]; he; he= HeNEXT(he) ) {
183 while ( max_chain_length < chain_length ) {
187 SvIVX( ST( chain_length ) )++;
189 /* now set the number of used buckets */
190 SvIVX( ST( BUCKET_INFO_ITEMS_ON_STACK - 1 ) ) = max_bucket_index - SvIVX( ST( BUCKET_INFO_ITEMS_ON_STACK ) ) + 1;
191 XSRETURN( max_chain_length + 1 ); /* max_chain_length is the index of the last item on the stack, so we add 1 */
193 #undef BUCKET_INFO_ITEMS_ON_STACK
203 /* Returns an array of arrays representing key/bucket mappings.
204 * Each element of the array contains either an integer or a reference
205 * to an array of keys. A plain integer represents K empty buckets. An
206 * array ref represents a single bucket, with each element being a key in
207 * the hash. (Note this treats a placeholder as a normal key.)
209 * This allows one to "see" the keyorder. Note the "insert first" nature
210 * of the hash store, combined with regular remappings means that relative
211 * order of keys changes each remap.
213 const HV * hv = NULL;
214 if (SvROK(rhv) && SvTYPE(SvRV(rhv))==SVt_PVHV && !SvMAGICAL(SvRV(rhv))) {
215 hv = (const HV *) SvRV(rhv);
216 } else if (!SvOK(rhv)) {
220 HE **he_ptr= HvARRAY(hv);
229 Perl_croak(aTHX_ "hash::bucket_array only works on 'normal' hashes");
233 mXPUSHs(newRV_noinc((SV*)info_av));
234 for ( i= 0; i <= max; i++ ) {
236 for (he= he_ptr[i]; he; he= HeNEXT(he) ) {
244 av_push(info_av, newSViv(empty_count));
247 av_push(info_av, (SV *)newRV_noinc((SV *)key_av));
249 if (HeKLEN(he) == HEf_SVKEY) {
253 mode= SvUTF8(sv) ? 1 : 0;
257 mode= HeKUTF8(he) ? 1 : 0;
259 key_sv= newSVpvn(str,len);
260 av_push(key_av,key_sv);
269 av_push(info_av, newSViv(empty_count));
286 if ( SvTYPE(rhv)==SVt_PVHV ) {
287 #if PERL_VERSION < 25
288 SV *ret= Perl_hv_scalar(aTHX_ (HV*)rhv);
290 SV *ret= Perl_hv_bucket_ratio(aTHX_ (HV*)rhv);
307 if ( SvTYPE(rhv)==SVt_PVHV ) {
308 XSRETURN_UV(HvMAX((HV*)rhv)+1);
322 if ( SvTYPE(rhv)==SVt_PVHV ) {
323 XSRETURN_UV(HvFILL((HV*)rhv));