1 #define PERL_NO_GET_CONTEXT
7 MODULE = Hash::Util PACKAGE = Hash::Util
10 all_keys(hash,keys,placeholder)
20 av_clear(placeholder);
22 (void)hv_iterinit(hash);
23 while((he = hv_iternext_flags(hash, HV_ITERNEXT_WANTPLACEHOLDERS))!= NULL) {
25 av_push(HeVAL(he) == &PL_sv_placeholder ? placeholder : keys,
34 Hash::Util::legal_ref_keys = 1
39 (void)hv_iterinit(hash);
40 while((he = hv_iternext_flags(hash, HV_ITERNEXT_WANTPLACEHOLDERS))!= NULL) {
42 if (ix || HeVAL(he) == &PL_sv_placeholder) {
48 hv_store(hash, key, val)
56 if (!hv_store_ent(hash, key, val, 0)) {
68 mXPUSHs(newSVpvn((char *)PERL_HASH_SEED,PERL_HASH_SEED_BYTES));
92 Takes a non-magical hash ref as an argument and returns a list of
93 statistics about the hash. The number and keys and the size of the
94 array will always be reported as the first two values. If the array is
95 actually allocated (they are lazily allocated), then additionally
96 will return a list of counts of bucket lengths. In other words in
98 ($keys, $buckets, $used, @length_count)= hash::bucket_info(\%hash);
100 $length_count[0] is the number of empty buckets, and $length_count[1]
101 is the number of buckets with only one key in it, $buckets - $length_count[0]
102 gives the number of used buckets, and @length_count-1 is the maximum
105 If the argument is not a hash ref, or if it is magical, then returns
106 nothing (the empty list).
109 if (SvROK(rhv) && SvTYPE(SvRV(rhv))==SVt_PVHV && !SvMAGICAL(SvRV(rhv))) {
110 const HV * const hv = (const HV *) SvRV(rhv);
111 U32 max_bucket_index= HvMAX(hv);
112 U32 total_keys= HvUSEDKEYS(hv);
113 HE **bucket_array= HvARRAY(hv);
115 mXPUSHi(max_bucket_index+1);
116 mXPUSHi(0); /* for the number of used buckets */
117 #define BUCKET_INFO_ITEMS_ON_STACK 3
119 XSRETURN(BUCKET_INFO_ITEMS_ON_STACK);
121 /* we use chain_length to index the stack - we eliminate an add
122 * by initializing things with the number of items already on the stack.
123 * If we have 2 items then ST(2+0) (the third stack item) will be the counter
124 * for empty chains, ST(2+1) will be for chains with one element, etc.
126 I32 max_chain_length= BUCKET_INFO_ITEMS_ON_STACK - 1; /* so we do not have to do an extra push for the 0 index */
129 for ( bucket_index= 0; bucket_index <= max_bucket_index; bucket_index++ ) {
130 I32 chain_length= BUCKET_INFO_ITEMS_ON_STACK;
131 for (he= bucket_array[bucket_index]; he; he= HeNEXT(he) ) {
134 while ( max_chain_length < chain_length ) {
138 SvIVX( ST( chain_length ) )++;
140 /* now set the number of used buckets */
141 SvIVX( ST( BUCKET_INFO_ITEMS_ON_STACK - 1 ) ) = max_bucket_index - SvIVX( ST( BUCKET_INFO_ITEMS_ON_STACK ) ) + 1;
142 XSRETURN( max_chain_length + 1 ); /* max_chain_length is the index of the last item on the stack, so we add 1 */
144 #undef BUCKET_INFO_ITEMS_ON_STACK
154 /* Returns an array of arrays representing key/bucket mappings.
155 * Each element of the array contains either an integer or a reference
156 * to an array of keys. A plain integer represents K empty buckets. An
157 * array ref represents a single bucket, with each element being a key in
158 * the hash. (Note this treats a placeholder as a normal key.)
160 * This allows one to "see" the keyorder. Note the "insert first" nature
161 * of the hash store, combined with regular remappings means that relative
162 * order of keys changes each remap.
164 if (SvROK(rhv) && SvTYPE(SvRV(rhv))==SVt_PVHV && !SvMAGICAL(SvRV(rhv))) {
165 const HV * const hv = (const HV *) SvRV(rhv);
166 HE **he_ptr= HvARRAY(hv);
175 Perl_croak(aTHX_ "hash::bucket_array only works on 'normal' hashes");
179 mXPUSHs(newRV_noinc((SV*)info_av));
180 for ( i= 0; i <= max; i++ ) {
182 for (he= he_ptr[i]; he; he= HeNEXT(he) ) {
190 av_push(info_av, newSViv(empty_count));
193 av_push(info_av, (SV *)newRV_noinc((SV *)key_av));
195 if (HeKLEN(he) == HEf_SVKEY) {
199 mode= SvUTF8(sv) ? 1 : 0;
203 mode= HeKUTF8(he) ? 1 : 0;
205 key_sv= newSVpvn(str,len);
206 av_push(key_av,key_sv);
215 av_push(info_av, newSViv(empty_count));