home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / IDICT.C < prev    next >
C/C++ Source or Header  |  1992-05-18  |  21KB  |  645 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* idict.c */
  21. /* Dictionaries for Ghostscript */
  22. #include "ghost.h"
  23. #include "alloc.h"
  24. #include "errors.h"
  25. #include "name.h"
  26. #include "packed.h"
  27. #include "save.h"            /* for value cache in names */
  28. #include "store.h"
  29. #include "iutil.h"            /* for array_get and obj_eq */
  30. #include "dict.h"            /* interface definition */
  31. #include "dstack.h"            /* ditto */
  32.  
  33. /*
  34.  * A dictionary is a structure of three elements (refs):
  35.  *
  36.  *    count - a t_integer whose value says how many entries are
  37.  *    occupied (N), and whose size says how many elements the client
  38.  *    thinks the dictionary can hold (C).  C may be less than M (see below).
  39.  *
  40.  *    keys - a t_shortarray or t_array of M+1 elements, containing
  41.  *    the keys.
  42.  *
  43.  *    values - a t_array of M+1 elements, containing the values.
  44.  *
  45.  * C < M is possible because on 32-bit systems, we round up M so that
  46.  * M is a power of 2; this allows us to use masking rather than division
  47.  * for computing the initial hash probe.  However, C is always the
  48.  * maxlength specified by the client, so clients get a consistent story.
  49.  */
  50. #define dict_round_size (!arch_ints_are_short)
  51. #if dict_round_size
  52. #  define hash_mod(hash, size) ((hash) & ((size) - 1))
  53. #else
  54. #  define hash_mod(hash, size) ((hash) % (size))
  55. #endif
  56. /*
  57.  * The first entry is always marked deleted, to reduce the cost of the
  58.  * wrap-around check.
  59.  *
  60.  * In the packed form:
  61.  *    unused entries contain packed_key_empty;
  62.  *    deleted entries contain packed_key_deleted.
  63.  * In the unpacked form:
  64.  *    unused entries contain a literal null;
  65.  *    deleted entries contain an executable null.
  66.  *
  67.  * Note that if the keys slot in the dictionary is new,
  68.  * all the key slots are new (more recent than the last save).
  69.  * We use this fact to avoid saving stores into packed keys
  70.  * for newly created dictionaries.
  71.  */
  72. #define dict_is_packed(dct) r_has_type(&(dct)->keys, t_shortarray)
  73. #define packed_key_empty (pt_tag(pt_integer) + 0)
  74. #define packed_key_deleted (pt_tag(pt_integer) + 1)
  75. #define packed_key_impossible pt_tag(pt_full_ref)    /* never matches */
  76. #define packed_name_key(nidx)\
  77.   ((nidx) <= packed_max_name_index ? pt_tag(pt_literal_name) + (nidx) :\
  78.    packed_key_impossible)
  79. /*
  80.  * Using a special mark for deleted entries causes lookup time to degrade
  81.  * as entries are inserted and deleted.  This is not a problem, because
  82.  * entries are almost never deleted.
  83.  */
  84. #define d_maxlength(dct) r_size(&(dct)->count)
  85. #define d_set_maxlength(dct,siz) r_set_size(&(dct)->count,siz)
  86. #define nslots(dct) r_size(&(dct)->values)
  87. #define npairs(dct) (nslots(dct) - 1)
  88. #define d_length(dct) ((uint)((dct)->count.value.intval))
  89.  
  90. /* Define the size of the largest valid dictionary. */
  91. /* This is limited by the size field of the keys and values refs, */
  92. /* and by the enumeration interface, which requires the size to */
  93. /* fit in an int. */
  94. const uint dict_max_size = max_ushort / 2 - 2;
  95.  
  96. /* Define whether dictionaries expand automatically when full. */
  97. int dict_auto_expand = 1;
  98.  
  99. /* Define the hashing function for names. */
  100. /* We don't have to scramble the index, because */
  101. /* indices are assigned in a scattered order (see name_ref in iname.c). */
  102. #define dict_name_index_hash(nidx) (nidx)
  103.  
  104. /* Define whether dictionaries are packed by default. */
  105. #define default_pack 1
  106.  
  107. /* Forward references */
  108. private int dict_create_contents(P3(uint size, dict *pdict, int pack));
  109.  
  110. /* Create a dictionary. */
  111. int
  112. dict_create(uint size, ref *pref)
  113. {    dict *pdict =
  114.       (dict *)alloc_refs(sizeof(dict) / sizeof(ref), "dict_create");
  115.     int code;
  116.     if ( pdict == 0 ) return e_VMerror;
  117.     code = dict_create_contents(size, pdict, default_pack);
  118.     if ( code < 0 ) return code;
  119.     make_tav_new(pref, t_dictionary, a_all, pdict, pdict);
  120.     return 0;
  121. }
  122. private int
  123. dict_create_unpacked_keys(uint asize, dict *pdict)
  124. {    ref *kp = alloc_refs(asize, "dict_create(keys)");
  125.     ref *zp;
  126.     register uint i;
  127.     if ( kp == 0 ) return e_VMerror;
  128.     make_tasv_new(&pdict->keys, t_array, a_all, asize,
  129.               refs, kp);
  130.     for ( zp = kp, i = asize; i; zp++, i-- )
  131.         make_null_new(zp);
  132.     r_set_attrs(kp, a_executable);    /* wraparound entry */
  133.     return 0;
  134. }
  135. private int
  136. dict_create_contents(uint size, dict *pdict, int pack)
  137. {    uint csize = (size == 0 ? 1 : size);    /* client-specified size */
  138.     uint asize = csize;
  139.     ref *vp;
  140.     register uint i;
  141.     ref *zp;
  142. #if dict_round_size
  143.     /* Round up the actual allocated size to the next higher */
  144.     /* power of 2, so we can use & instead of %. */
  145.     while ( asize & (asize - 1) ) asize = (asize | (asize >> 1)) + 1;
  146. #endif
  147.     asize++;        /* allow room for wraparound entry */
  148.     vp = alloc_refs(asize, "dict_create(values)");
  149.     if ( vp == 0 ) return e_VMerror;
  150.     make_tasv_new(&pdict->values, t_array, a_all, asize, refs, vp);
  151.     for ( zp = vp, i = asize; i; zp++, i-- )
  152.         make_null_new(zp);
  153.     if ( pack )
  154.        {    uint ksize = (asize + packed_per_ref - 1) / packed_per_ref;
  155.         ref_packed *pkp = (ref_packed *)alloc_refs(ksize, "dict_create(packed keys)");
  156.         ref_packed *pzp;
  157.         make_tasv_new(&pdict->keys, t_shortarray, a_all, asize,
  158.                   packed, pkp);
  159.         for ( pzp = pkp, i = 0; i < asize || i % packed_per_ref; pzp++, i++ )
  160.             *pzp = packed_key_empty;
  161.         *pkp = packed_key_deleted;    /* wraparound entry */
  162.        }
  163.     else                /* not packed */
  164.        {    int code = dict_create_unpacked_keys(asize, pdict);
  165.         if ( code < 0 ) return code;
  166.        }
  167.     make_tv_new(&pdict->count, t_integer, intval, 0);
  168.     d_set_maxlength(pdict, csize);
  169.     return 0;
  170. }
  171.  
  172. /*
  173.  * Define a macro for searching a packed dictionary.  Free variables:
  174.  *    ref_packed kpack - holds the packed key.
  175.  *    uint hash - holds the hash of the name.
  176.  *    dict *pdict - points to the dictionary.
  177.  *    uint size - holds npairs(pdict).
  178.  * Note that the macro is *not* enclosed in {}, so that we can access
  179.  * the values of kbot and kp after leaving the loop.
  180.  *
  181.  * We break the macro into two to avoid overflowing some preprocessors.
  182.  */
  183. #define packed_search_1(del,pre,post,miss)\
  184.    const ref_packed *kbot = pdict->keys.value.packed;\
  185.    register const ref_packed *kp;\
  186.    for ( kp = kbot + hash_mod(hash, size) + 2; ; )\
  187.     { if ( *--kp == kpack )\
  188.        { pre (pdict->values.value.refs + (kp - kbot));\
  189.      post;\
  190.        }\
  191.       else if ( !packed_ref_is_name(kp) )\
  192.        { /* Empty, deleted, or wraparound. Figure out which. */\
  193.      if ( *kp == packed_key_empty ) miss;\
  194.      if ( kp == kbot ) break;    /* wrap */\
  195.      else { del; }\
  196.        }\
  197.     }
  198. #define packed_search_2(del,pre,post,miss)\
  199.    for ( kp += size + 1; ; )\
  200.     { if ( *--kp == kpack )\
  201.        { pre (pdict->values.value.refs + (kp - kbot));\
  202.      post;\
  203.        }\
  204.       else if ( !packed_ref_is_name(kp) )\
  205.        { /* Empty, deleted, or wraparound. Figure out which. */\
  206.      if ( *kp == packed_key_empty ) miss;\
  207.      if ( kp == kbot ) break;    /* wrap */\
  208.      else { del; }\
  209.        }\
  210.     }
  211.  
  212. /*
  213.  * Look up in a stack of dictionaries.  Store a pointer to the value slot
  214.  * where found, or to the (value) slot for inserting.
  215.  * Return 1 if found, 0 if not and there is room for a new entry in
  216.  * the top dictionary on the stack, or e_dictfull if the top dictionary
  217.  * is full and the key is missing.
  218.  * Note that pdbot <= pdtop, and the search starts at pdtop.
  219.  */
  220. int
  221. dict_lookup(const ref *pdbot, const ref *pdtop, const ref *pkey,
  222.   ref **ppvalue /* result is stored here */)
  223. {    const ref *pdref = pdtop;
  224.     uint nidx;
  225.     ref_packed kpack;
  226.     uint hash;
  227.     int ktype;
  228.     int full = 1;            /* gets set to 0 or e_dictfull */
  229.     /* Compute hash.  The only types we bother with are strings, */
  230.     /* names, and (unlikely, but worth checking for) integers. */
  231.     switch ( r_type(pkey) )
  232.        {
  233.     case t_name:
  234.         nidx = name_index(pkey);
  235. nh:        hash = dict_name_index_hash(nidx);
  236.         kpack = packed_name_key(nidx);
  237.         ktype = t_name;
  238.         break;
  239.     case t_string:            /* convert to a name first */
  240.        {    ref nref;
  241.         int code = name_ref(pkey->value.bytes,
  242.                     r_size(pkey), &nref, 1);
  243.         if ( code < 0 ) return code;
  244.         nidx = name_index(&nref);
  245.        }    goto nh;
  246.     case t_integer:
  247.         hash = (uint)pkey->value.intval * 30503;
  248.         kpack = packed_key_impossible;
  249.         ktype = -1;
  250.         break;
  251.     default:
  252.         hash = r_btype(pkey) * 99;    /* yech */
  253.         kpack = packed_key_impossible;
  254.         ktype = -1;
  255.        }
  256.     do
  257.        {    dict *pdict = pdref->value.pdict;
  258.         uint size = npairs(pdict);
  259.         register int etype;
  260.         /* Search the dictionary */
  261.         if ( dict_is_packed(pdict) )
  262.            {    const ref_packed *pslot = 0;
  263.             packed_search_1(if ( pslot == 0 ) pslot = kp,
  264.                     *ppvalue =, return 1, goto miss);
  265.             packed_search_2(if ( pslot == 0 ) pslot = kp,
  266.                     *ppvalue =, return 1, goto miss);
  267.             /* Double wraparound. */
  268.             /* Set full = e_dictfull if first dict and */
  269.             /* dict is full (pslot == 0). */
  270.             if ( full > 0 )    /* first dictionary */
  271.                {    if ( pslot == 0 )
  272.                   full = e_dictfull;
  273.                 else
  274.                 { *ppvalue = pdict->values.value.refs +
  275.                     (pslot - kbot),
  276.                   full = 0;
  277.                 }
  278.                }
  279.             goto next_dict;
  280. miss:            /* Key is missing, not double wrap. */
  281.             if ( full > 0 )    /* first dictionary */
  282.                {    if ( pslot == 0 ) pslot = kp;
  283.                 *ppvalue = pdict->values.value.refs +
  284.                   (pslot - kbot),
  285.                 full = 0;
  286.                }
  287.            }
  288.         else
  289.            {    ref *kbot = pdict->keys.value.refs;
  290.             register ref *kp;
  291.             ref *pslot = 0;
  292.             int wrap = 0;
  293.             for ( kp = kbot + hash_mod(hash, size) + 2; ; )
  294.                {    --kp;
  295.                 if ( (etype = r_type(kp)) == ktype )
  296.                    {    /* Fast comparison if both keys are names */
  297.                     if ( name_index(kp) == nidx )
  298.                        {    *ppvalue = pdict->values.value.refs + (kp - kbot);
  299.                         return 1;
  300.                        }
  301.                    }
  302.                 else if ( etype == t_null )
  303.                    {    /* Empty, deleted, or wraparound. */
  304.                     /* Figure out which. */
  305.                     if ( kp == kbot )    /* wrap */
  306.                        {    if ( wrap++ )    /* wrapped twice */
  307.                            {    if ( full > 0 )
  308.                                {    if ( pslot != 0 )
  309.                                     break;
  310.                                 full = e_dictfull;
  311.                                }
  312.                             goto next_dict;
  313.                            }
  314.                         kp += size + 1;
  315.                        }
  316.                     else if ( r_has_attr(kp, a_executable) )
  317.                        {    /* Deleted entry, save the slot. */
  318.                         if ( pslot == 0 ) pslot = kp;
  319.                        }
  320.                     else    /* key not found */
  321.                         break;
  322.                    }
  323.                 else
  324.                    {    if ( obj_eq(kp, pkey) )
  325.                        {    *ppvalue = pdict->values.value.refs + (kp - kbot);
  326.                         return 1;
  327.                        }
  328.                    }
  329.                }
  330.             if ( full > 0 )
  331.                {    *ppvalue = pdict->values.value.refs +
  332.                   ((pslot != 0 ? pslot : kp) - kbot);
  333.                 full = 0;
  334.                }
  335.            }
  336. next_dict: ;
  337.        }
  338.     while ( --pdref >= pdbot );
  339.     return full;
  340. }
  341.  
  342. /*
  343.  * Look up a name on the dictionary stack.
  344.  * Return the pointer to the value if found, 0 if not.
  345.  * This is just an optimization of dict_lookup with a different interface.
  346.  */
  347. ref *
  348. dict_find_name_by_index(uint nidx)
  349. {    ds_ptr pdref = dsp;
  350. /* Since we know the hash function is the identity function, */
  351. /* there's no point in allocating a separate variable for it. */
  352. #define hash dict_name_index_hash(nidx)
  353.     ref_packed kpack = packed_name_key(nidx);
  354.     do
  355.        {    dict *pdict = pdref->value.pdict;
  356.         uint size = npairs(pdict);
  357.         if ( dict_is_packed(pdict) )
  358.            {    packed_search_1(0, return, 0, goto miss);
  359.             packed_search_2(0, return, 0, break);
  360. miss:            ;
  361.            }
  362.         else
  363.            {    ref *kbot = pdict->keys.value.refs;
  364.             register ref *kp;
  365.             int wrap = 0;
  366.             /* Search the dictionary */
  367.             for ( kp = kbot + hash_mod(hash, size) + 2; ; )
  368.                {    --kp;
  369.                 if ( r_has_type(kp, t_name) )
  370.                    {    if ( name_index(kp) == nidx )
  371.                       return pdict->values.value.refs +
  372.                         (kp - kbot);
  373.                    }
  374.                 else if ( r_has_type(kp, t_null) )
  375.                    {    /* Empty, deleted, or wraparound. */
  376.                     /* Figure out which. */
  377.                     if ( !r_has_attr(kp, a_executable) )
  378.                         break;
  379.                     if ( kp == kbot )    /* wrap */
  380.                        {    if ( wrap++ )
  381.                             break;    /* 2 wraps */
  382.                         kp += size + 1;
  383.                        }
  384.                    }
  385.                }
  386.            }
  387.        }
  388.     while ( --pdref >= dstack );
  389.     return (ref *)0;
  390. #undef hash
  391. }
  392.  
  393. /*
  394.  * Enter a key-value pair in a dictionary.
  395.  * The caller is responsible for ensuring key is not a null.
  396.  * Return 0, e_dictfull, or e_VMerror if the key was a string
  397.  * and a VMerror occurred when converting it to a name.
  398.  */
  399. int
  400. dict_put(ref *pdref /* t_dictionary */, const ref *pkey, const ref *pvalue)
  401. {    ref *pvslot;
  402. top:    if ( dict_find(pdref, pkey, &pvslot) <= 0 )    /* not found */
  403.        {    /* Check for overflow */
  404.         dict *pdict = pdref->value.pdict;
  405.         ref kname;
  406.         uint index = pvslot - pdict->values.value.refs;
  407.         if ( d_length(pdict) == d_maxlength(pdict) )
  408.            {    int code;
  409.             ulong new_size;
  410.             if ( !dict_auto_expand )
  411.                 return e_dictfull;
  412.             /* We might have maxlength < npairs, if */
  413.             /* dict_round_size is true. */
  414.             new_size = (ulong)npairs(pdict) * 3 / 2 + 2;
  415.             if ( new_size > dict_max_size )
  416.                {    if ( d_maxlength(pdict) == dict_max_size )
  417.                     return e_dictfull;
  418.                 new_size = dict_max_size;
  419.                }
  420.             if ( new_size > npairs(pdict) )
  421.             {    code = dict_resize(pdref, (uint)new_size);
  422.                 if ( code < 0 ) return code;
  423.             }
  424.             else
  425.             {    /* maxlength < npairs, we can grow in place */
  426.                 ref_save(&pdict->count, "dict_put(size)");
  427.                 d_set_maxlength(pdict, npairs(pdict));
  428.             }
  429.             goto top;    /* keep things simple */
  430.            }
  431.         /* If the key is a string, convert it to a name. */
  432.         if ( r_has_type(pkey, t_string) )
  433.            {    int code = name_from_string(pkey, &kname);
  434.             if ( code < 0 ) return code;
  435.             pkey = &kname;
  436.            }
  437.         if ( dict_is_packed(pdict) )
  438.            {    ref_packed *kp;
  439.             if ( !r_has_type(pkey, t_name) ||
  440.                  name_index(pkey) > packed_max_name_index
  441.                )
  442.                {    /* Change to unpacked representation. */
  443.                 /* We can't just use dict_resize, */
  444.                 /* because the values slots mustn't move. */
  445.                 uint count = nslots(pdict);
  446.                 const ref_packed *old_keys =
  447.                     pdict->keys.value.packed;
  448.                 int code;
  449.                 const ref_packed *okp;
  450.                 ref *nkp;
  451.                 if ( alloc_save_new_mask )
  452.                     alloc_save_change(&pdict->keys, "dict_unpack(keys)");
  453.                 code = dict_create_unpacked_keys(count, pdict);
  454.                 if ( code < 0 ) return code;
  455.                 for ( okp = old_keys, nkp = pdict->keys.value.refs; count--; okp++, nkp++ )
  456.                   if ( packed_ref_is_name(okp) )
  457.                     packed_get(okp, nkp);
  458.                 alloc_free_refs((ref *)old_keys,
  459.                         (count + packed_per_ref - 1) / packed_per_ref,
  460.                         "dict_unpack(old keys)");
  461.                 return dict_put(pdref, pkey, pvalue);
  462.                }
  463.             kp = (ref_packed *)(pdict->keys.value.packed + index);
  464.             if ( alloc_save_new_mask &&
  465.                  !r_has_attr(&pdict->keys, l_new)
  466.                )
  467.                {    /* See initial comment for why it is safe */
  468.                 /* not to save the change if the keys */
  469.                 /* array itself is new. */
  470.                 alloc_save_change(pdict->keys.value.refs + (index / packed_per_ref), "dict_put(key)");
  471.                }
  472.             *kp = pt_tag(pt_literal_name) + name_index(pkey);
  473.            }
  474.         else
  475.            {    ref *kp = pdict->keys.value.refs + index;
  476.             if_debug2('d', "[d]%lx fill key %lx\n",
  477.                   (ulong)pdict, (ulong)kp);
  478.             ref_assign_old(kp, pkey, "dict_put(key)");    /* set key of pair */
  479.            }
  480.         ref_save(&pdict->count, "dict_put(count)");
  481.         pdict->count.value.intval++;
  482.         /* If the key is a name, update its 1-element cache. */
  483.         if ( r_has_type(pkey, t_name) )
  484.            {    name *pname = pkey->value.pname;
  485.             if ( pname->pvalue == pv_no_defn &&
  486.                 (pdict == systemdict.value.pdict ||
  487.                  pdict == userdict.value.pdict) &&
  488.                 /* Only set the cache if we aren't inside */
  489.                 /* a save.  This way, we never have to */
  490.                 /* undo setting the cache. */
  491.                 alloc_save_level() == 0
  492.                )
  493.                {    /* Set the cache */
  494.                 pname->pvalue = pvslot;
  495.                }
  496.             else    /* The cache is worthless */
  497.                 pname->pvalue = pv_other;
  498.            }
  499.        }
  500.     if_debug6('d', "[d]in %lx put %lx: %lx %lx -> %lx %lx\n",
  501.           (ulong)pdref->value.pdict, (ulong)pvslot,
  502.           ((ulong *)pvslot)[0], ((ulong *)pvslot)[1],
  503.           ((ulong *)pvalue)[0], ((ulong *)pvalue)[1]);
  504.     ref_assign_old(pvslot, pvalue, "dict_put(value)");
  505.     return 0;
  506. }
  507.  
  508. /* Remove an element from a dictionary. */
  509. int
  510. dict_undef(ref *pdref, const ref *pkey)
  511. {    ref *pvslot;
  512.     dict *pdict;
  513.     uint index;
  514.     if ( dict_find(pdref, pkey, &pvslot) <= 0 )
  515.         return e_undefined;
  516.     /* Remove the entry from the dictionary. */
  517.     pdict = pdref->value.pdict;
  518.     index = pvslot - pdict->values.value.refs;
  519.     if ( dict_is_packed(pdict) )
  520.        {    ref_packed *pkp =
  521.            (ref_packed *)(pdict->keys.value.packed + index);
  522.         /* Since packed arrays don't have room for a saved bit, */
  523.         /* always save the entire ref containing this key. */
  524.         /* This wastes a little space, but undef is rare. */
  525.         /* See the initial comment for why it is safe not to save */
  526.         /* the change if the keys array itself is new. */
  527.         if ( alloc_save_new_mask && !r_has_attr(&pdict->keys, l_new) )
  528.             alloc_save_change(pdict->keys.value.refs + (index / packed_per_ref), "dict_undef(key)");
  529.         /* Accumulating deleted entries slows down lookup. */
  530.         /* Detect the easy case where we can use an empty entry */
  531.         /* rather than a deleted one, namely, when the next entry */
  532.         /* in the probe order is empty. */
  533.         if ( pkp[-1] == packed_key_empty )
  534.             *pkp = packed_key_empty;
  535.         else
  536.             *pkp = packed_key_deleted;
  537.        }
  538.     else                /* not packed */
  539.        {    ref *kp = pdict->keys.value.refs + index;
  540.         make_null_old(kp, "dict_undef(key)");
  541.         /* Accumulating deleted entries slows down lookup. */
  542.         /* Detect the easy case where we can use an empty entry */
  543.         /* rather than a deleted one, namely, when the next entry */
  544.         /* in the probe order is empty. */
  545.         if ( !r_has_type(kp - 1, t_null) ||    /* full entry */
  546.              r_has_attr(kp - 1, a_executable)    /* deleted or wraparound */
  547.             )
  548.             r_set_attrs(kp, a_executable);    /* mark as deleted */
  549.        }
  550.     ref_save(&pdict->count, "dict_undef(count)");
  551.     pdict->count.value.intval--;
  552.     /* If the key is a name, update its 1-element cache. */
  553.     if ( r_has_type(pkey, t_name) )
  554.        {    name *pname = pkey->value.pname;
  555.         if ( pv_valid(pname->pvalue) &&
  556.             (pdict == systemdict.value.pdict ||
  557.              pdict == userdict.value.pdict) )
  558.            {    /* Clear the cache */
  559.             pname->pvalue = pv_no_defn;
  560.            }
  561.        }
  562.     make_null_old(pvslot, "dict_undef(value)");
  563.     return 0;
  564. }
  565.  
  566. /* Return the number of elements in a dictionary. */
  567. uint
  568. dict_length(const ref *pdref /* t_dictionary */)
  569. {    return d_length(pdref->value.pdict);
  570. }
  571.  
  572. /* Return the capacity of a dictionary. */
  573. uint
  574. dict_maxlength(const ref *pdref /* t_dictionary */)
  575. {    return d_maxlength(pdref->value.pdict);
  576. }
  577.  
  578. /* Copy one dictionary into another. */
  579. int
  580. dict_copy(const ref *pdrfrom /* t_dictionary */, ref *pdrto /* t_dictionary */)
  581. {    int index = dict_first(pdrfrom);
  582.     ref elt[2];
  583.     int code;
  584.     while ( (index = dict_next(pdrfrom, index, elt)) >= 0 )
  585.       if ( (code = dict_put(pdrto, &elt[0], &elt[1])) < 0 )
  586.         return code;
  587.     return 0;
  588. }
  589.  
  590. /* Resize a dictionary. */
  591. int
  592. dict_resize(ref *pdrfrom, uint new_size)
  593. {    dict *pdict = pdrfrom->value.pdict;
  594.     uint count = nslots(pdict);
  595.     dict dnew;
  596.     ref drto;
  597.     int code;
  598.     if ( new_size < d_length(pdict) )
  599.        {    if ( !dict_auto_expand )
  600.             return e_dictfull;
  601.         new_size = d_length(pdict);
  602.        }
  603.     if ( (code = dict_create_contents(new_size, &dnew, dict_is_packed(pdict))) < 0 )
  604.         return code;
  605.     make_tav_new(&drto, t_dictionary, a_all, pdict, &dnew);
  606.     dict_copy(pdrfrom, &drto);    /* can't fail */
  607.     /* Free the old dictionary */
  608.     alloc_free_refs(pdict->values.value.refs, count,
  609.             "dict_resize(old values)");
  610.     alloc_free_refs(pdict->keys.value.refs,
  611.             (dict_is_packed(pdict) ?
  612.              (count + packed_per_ref - 1) / packed_per_ref :
  613.              count),
  614.             "dict_resize(old keys)");
  615.     ref_assign_old(&pdict->keys, &dnew.keys, "dict_resize(keys)");
  616.     ref_assign_old(&pdict->values, &dnew.values, "dict_resize(values)");
  617.     ref_save(&pdict->count, "dict_resize(size)");
  618.     d_set_maxlength(pdict, new_size);
  619.     return 0;
  620. }
  621.  
  622. /* Prepare to enumerate a dictionary. */
  623. int
  624. dict_first(const ref *pdref)
  625. {    return (int)nslots(pdref->value.pdict);
  626. }
  627.  
  628. /* Enumerate the next element of a dictionary. */
  629. int
  630. dict_next(const ref *pdref, int index, ref *eltp /* ref eltp[2] */)
  631. {    dict *pdict = pdref->value.pdict;
  632.     ref *vp = pdict->values.value.refs + index;
  633.     while ( vp--, --index >= 0 )
  634.        {    array_get(&pdict->keys, (long)index, eltp);
  635.         /* Make sure this is a valid entry. */
  636.         if ( r_has_type(eltp, t_name) ||
  637.              (!dict_is_packed(pdict) && !r_has_type(eltp, t_null))
  638.            )
  639.            {    eltp[1] = *vp;
  640.             return index;
  641.            }
  642.        }
  643.     return -1;            /* no more elements */
  644. }
  645.