This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update to version 1.16
[perl5.git] / hv.h
1 /*    hv.h
2  *
3  *    Copyright (c) 1991-1994, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 typedef struct he HE;
11
12 struct he {
13     HE          *hent_next;
14     char        *hent_key;
15     SV          *hent_val;
16     U32         hent_hash;
17     I32         hent_klen;
18 };
19
20 struct xpvhv {
21     char *      xhv_array;      /* pointer to malloced string */
22     STRLEN      xhv_fill;       /* how full xhv_array currently is */
23     STRLEN      xhv_max;        /* subscript of last element of xhv_array */
24     I32         xhv_keys;       /* how many elements in the array */
25     double      xnv_nv;         /* numeric value, if any */
26     MAGIC*      xmg_magic;      /* magic for scalar array */
27     HV*         xmg_stash;      /* class package */
28
29     I32         xhv_riter;      /* current root of iterator */
30     HE          *xhv_eiter;     /* current entry of iterator */
31     PMOP        *xhv_pmroot;    /* list of pm's for this package */
32     char        *xhv_name;      /* name, if a symbol table */
33 };
34
35 #define PERL_HASH(hash,str,len) \
36      STMT_START { \
37         register char *s_PeRlHaSh = str; \
38         register I32 i_PeRlHaSh = len; \
39         register U32 hash_PeRlHaSh = 0; \
40         while (i_PeRlHaSh--) \
41             hash_PeRlHaSh = hash_PeRlHaSh * 33 + *s_PeRlHaSh++; \
42         (hash) = hash_PeRlHaSh; \
43     } STMT_END
44
45
46 /* these hash entry flags ride on hent_klen */
47
48 #define HEf_LAZYDEL     -1      /* entry must be deleted during next iter step */
49 #define HEf_SVKEY       -2      /* hent_key is a SV* (only for magic/tied HVs) */
50
51
52 #define Nullhv Null(HV*)
53 #define HvARRAY(hv)     ((HE**)((XPVHV*)  SvANY(hv))->xhv_array)
54 #define HvFILL(hv)      ((XPVHV*)  SvANY(hv))->xhv_fill
55 #define HvMAX(hv)       ((XPVHV*)  SvANY(hv))->xhv_max
56 #define HvKEYS(hv)      ((XPVHV*)  SvANY(hv))->xhv_keys
57 #define HvRITER(hv)     ((XPVHV*)  SvANY(hv))->xhv_riter
58 #define HvEITER(hv)     ((XPVHV*)  SvANY(hv))->xhv_eiter
59 #define HvPMROOT(hv)    ((XPVHV*)  SvANY(hv))->xhv_pmroot
60 #define HvNAME(hv)      ((XPVHV*)  SvANY(hv))->xhv_name
61
62 #define HvSHAREKEYS(hv)         (SvFLAGS(hv) & SVphv_SHAREKEYS)
63 #define HvSHAREKEYS_on(hv)      (SvFLAGS(hv) |= SVphv_SHAREKEYS)
64 #define HvSHAREKEYS_off(hv)     (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
65
66 #ifdef OVERLOAD
67
68 /* Maybe amagical: */
69 /* #define HV_AMAGICmb(hv)      (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
70
71 #define HV_AMAGIC(hv)        (SvFLAGS(hv) &   SVpgv_AM)
72 #define HV_AMAGIC_on(hv)     (SvFLAGS(hv) |=  SVpgv_AM)
73 #define HV_AMAGIC_off(hv)    (SvFLAGS(hv) &= ~SVpgv_AM)
74
75 /*
76 #define HV_AMAGICbad(hv)     (SvFLAGS(hv) & SVpgv_badAM)
77 #define HV_badAMAGIC_on(hv)  (SvFLAGS(hv) |= SVpgv_badAM)
78 #define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
79 */
80
81 #endif /* OVERLOAD */
82
83 #define Nullhe Null(HE*)
84 #define HeNEXT(he)              (he)->hent_next
85 #define HeKEY(he)               (he)->hent_key
86 #define HeKLEN(he)              (he)->hent_klen
87 #define HeVAL(he)               (he)->hent_val
88 #define HeHASH(he)              (he)->hent_hash
89 #define HePV(he)                ((he)->hent_klen == HEf_SVKEY) ?                \
90                                 SvPV((SV*)((he)->hent_key),na) :                \
91                                 (he)->hent_key))
92 #define HeSVKEY(he)             (((he)->hent_key &&                             \
93                                   (he)->hent_klen == HEf_SVKEY) ?               \
94                                  (SV*)((he)->hent_key) : Nullsv)
95
96 #define HeSVKEY_force(he)       ((he)->hent_key ?                               \
97                                  (((he)->hent_klen == HEf_SVKEY) ?              \
98                                   (SV*)((he)->hent_key) :                       \
99                                   sv_2mortal(newSVpv((he)->hent_key,            \
100                                                      (he)->hent_klen))) :       \
101                                  &sv_undef)