5c41309f006c197fb0b8ac3343262a8348cc943d
[p5sagit/p5-mst-13.2.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 (for use only in magic/tied HVs) */
47 #define HEf_SVKEY       -2      /* hent_key is a SV* */
48
49
50 #define Nullhv Null(HV*)
51 #define HvARRAY(hv)     ((HE**)((XPVHV*)  SvANY(hv))->xhv_array)
52 #define HvFILL(hv)      ((XPVHV*)  SvANY(hv))->xhv_fill
53 #define HvMAX(hv)       ((XPVHV*)  SvANY(hv))->xhv_max
54 #define HvKEYS(hv)      ((XPVHV*)  SvANY(hv))->xhv_keys
55 #define HvRITER(hv)     ((XPVHV*)  SvANY(hv))->xhv_riter
56 #define HvEITER(hv)     ((XPVHV*)  SvANY(hv))->xhv_eiter
57 #define HvPMROOT(hv)    ((XPVHV*)  SvANY(hv))->xhv_pmroot
58 #define HvNAME(hv)      ((XPVHV*)  SvANY(hv))->xhv_name
59
60 #define HvSHAREKEYS(hv)         (SvFLAGS(hv) & SVphv_SHAREKEYS)
61 #define HvSHAREKEYS_on(hv)      (SvFLAGS(hv) |= SVphv_SHAREKEYS)
62 #define HvSHAREKEYS_off(hv)     (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
63
64 #define HvLAZYDEL(hv)           (SvFLAGS(hv) & SVphv_LAZYDEL)
65 #define HvLAZYDEL_on(hv)        (SvFLAGS(hv) |= SVphv_LAZYDEL)
66 #define HvLAZYDEL_off(hv)       (SvFLAGS(hv) &= ~SVphv_LAZYDEL)
67
68 #ifdef OVERLOAD
69
70 /* Maybe amagical: */
71 /* #define HV_AMAGICmb(hv)      (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
72
73 #define HV_AMAGIC(hv)        (SvFLAGS(hv) &   SVpgv_AM)
74 #define HV_AMAGIC_on(hv)     (SvFLAGS(hv) |=  SVpgv_AM)
75 #define HV_AMAGIC_off(hv)    (SvFLAGS(hv) &= ~SVpgv_AM)
76
77 /*
78 #define HV_AMAGICbad(hv)     (SvFLAGS(hv) & SVpgv_badAM)
79 #define HV_badAMAGIC_on(hv)  (SvFLAGS(hv) |= SVpgv_badAM)
80 #define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
81 */
82
83 #endif /* OVERLOAD */
84
85 #define Nullhe Null(HE*)
86 #define HeNEXT(he)              (he)->hent_next
87 #define HeKEY(he)               (he)->hent_key
88 #define HeKLEN(he)              (he)->hent_klen
89 #define HeVAL(he)               (he)->hent_val
90 #define HeHASH(he)              (he)->hent_hash
91 #define HePV(he)                ((he)->hent_klen == HEf_SVKEY) ?                \
92                                 SvPV((SV*)((he)->hent_key),na) :                \
93                                 (he)->hent_key))
94 #define HeSVKEY(he)             (((he)->hent_key &&                             \
95                                   (he)->hent_klen == HEf_SVKEY) ?               \
96                                  (SV*)((he)->hent_key) : Nullsv)
97
98 #define HeSVKEY_force(he)       ((he)->hent_key ?                               \
99                                  (((he)->hent_klen == HEf_SVKEY) ?              \
100                                   (SV*)((he)->hent_key) :                       \
101                                   sv_2mortal(newSVpv((he)->hent_key,            \
102                                                      (he)->hent_klen))) :       \
103                                  &sv_undef)