DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T i

⟦2de11502b⟧ TextFile

    Length: 7493 (0x1d45)
    Types: TextFile
    Names: »idict.c«

Derivation

└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89
    └─⟦ff23ba0e6⟧ »./ghostscript-1.3.tar.Z« 
        └─⟦a24a58cd3⟧ 
            └─⟦this⟧ »idict.c« 

TextFile

/* Copyright (C) 1989 Aladdin Enterprises.  All rights reserved.
   Distributed by Free Software Foundation, Inc.

This file is part of Ghostscript.

Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing.  Refer
to the Ghostscript General Public License for full details.

Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License.  A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies.  */

/* idict.c */
/* Dictionaries for GhostScript */
#include <stdio.h>			/* for printf */
#include "ghost.h"
#include "alloc.h"
#include "errors.h"
#include "name.h"
#include "store.h"
#include "dict.h"	/* interface definition */

/* Import the dictionary stack */
extern ref dstack[];

/* Import systemdict and userdict for the name value cache. */
#define systemdict (dstack[0])
#define userdict (dstack[1])

/* Import null. */
extern ref null;

/* A dictionary is a structure of two elements (refs). */
/* The first element is a t_integer whose value says how many */
/* entries are occupied.  The second element is a t_array */
/* of 2N elements, containing alternating keys and values. */
/* Unused entries have null as the key. */
struct dict_s {
	ref count;			/* t_integer */
	ref contents;			/* t_array */
};
struct pair_s {
	ref key;
	ref value;
};
typedef struct pair_s pair;
#define pairs(dct) (pair *)((dct)->contents.value.refs)
#define npairs(dct) ((dct)->contents.size >> 1)
#define numpairs(pdref) (int)npairs(pdref->value.pdict)

/* Define the size of the largest valid dictionary. */
/* This is limited by the size field of the contents ref. */
uint dict_max_size = (ushort)(-1) / 2;

/* Create a dictionary */
int
dict_create(uint size, ref *pref)
{	uint asize = (size == 0 ? 1 : size);
	dict *pdict = (dict *)alloc(sizeof(dict), "dict_create");
	pair *pp;
	ushort null_ta = null.type_attrs;
	if ( pdict == 0 ) return e_VMerror;
	pp = (pair *)alloc(asize * sizeof(pair), "dict_create(pairs)");
	if ( pp == 0 )
	   {	alloc_free((char *)pdict, sizeof(dict), "dict_create");
		return e_VMerror;
	   }
	n_store_int(&pdict->count, 0);
	n_store_tasv(&pdict->contents, t_array, 0, asize * 2,
		     refs, (ref *)pp);
	n_store_tav(pref, t_dictionary, a_all, pdict, pdict);
	pp = pairs(pdict);
	while ( asize-- )
		pp->key.type_attrs = pp->value.type_attrs = null_ta, pp++;
	return 0;
   }

/* Look up in a dictionary.  Store a pointer to the value slot */
/* where found, or to the (value) slot for inserting. */
/* Return 1 if found, 0 if not and there is room for a new entry, */
/* or e_dictfull if the dictionary is full and the key is missing. */
/* The caller is responsible for ensuring key is not a null. */
int
dict_find(ref *pdref, ref *pkey, ref **ppvalue /* result is stored here */)
{	dict *pdict = pdref->value.pdict;
	uint size = npairs(pdict);
	pair *ppbot = pairs(pdict);
	uint hash;
	pair *pp;			/* probe pointer */
	ref *ppkey;
	byte ktype;
	int wrap = 0;
	/* Compute hash.  The only types we bother with are strings */
	/* and names. */
	switch ( r_type(pkey) )
	   {
	case t_name:
		hash = (uint)pkey->value.pname;
		ktype = t_name; break;
	case t_string:
		hash = string_hash(pkey->value.bytes, pkey->size);
		ktype = t_null; break;
	default:
		hash = r_btype(pkey) * 99;	/* yech */
		ktype = t_null;
	   }
	/* Search the dictionary */
#ifdef DEBUG
if ( gs_debug['d'] )
	   {	extern void debug_print_ref(P1(ref *));
		printf("[d]");
		debug_print_ref(pdref);
		printf(":");
		debug_print_ref(pkey);
		printf("->");
	   }
#endif
	pp = ppbot + (hash % size);
	/* Do a fast search if the key is a name */
	while ( (ppkey = &pp->key), r_type(ppkey) != t_null )
	   {	if ( (r_type(ppkey) == ktype ?
			ppkey->value.pname == pkey->value.pname :
			obj_eq(ppkey, pkey)) )
		   {	*ppvalue = &pp->value;
#ifdef DEBUG
if ( gs_debug['d'] )
			   {	extern void debug_print_ref(P1(ref *));
				debug_print_ref(&pp->value);
				printf("; ");
			   }
#endif
			return 1;
		   }
		if ( pp == ppbot )
		   {	if ( wrap++ )	/* wrapped twice */
				return e_dictfull;
			pp += size;
		   }
		pp--;
	   }
	*ppvalue = &pp->value;
#ifdef DEBUG
if ( gs_debug['d'] )
		printf("0(%lx); ", (ulong)&pp->value);
#endif
	return 0;
}

/* Enter a key-value pair in a dictionary. */
/* Return 0 or e_dictfull. */
int
dict_put(ref *pdref /* t_dictionary */, ref *pkey, ref *pvalue)
{	ref *pvslot;
	if ( dict_find(pdref, pkey, &pvslot) <= 0 )	/* not found */
	   {	/* Check for overflow */
		dict *pdict = pdref->value.pdict;
		if ( pdict->count.value.intval == npairs(pdict) )
			return e_dictfull;
		pdict->count.value.intval++;
		pvslot[-1] = *pkey;	/* i.e. key of pair */
		/* If the key is a name, update its 1-element cache. */
		if ( r_type(pkey) == t_name )
		   {	name *pname = pkey->value.pname;
			if ( pname->pvalue == pv_no_defn &&
				(pdict == systemdict.value.pdict ||
				 pdict == userdict.value.pdict) )
			   {	/* Initialize the cache */
				pname->pvalue = pvslot;
			   }
			else	/* The cache is worthless */
				pname->pvalue = pv_other;
		   }
	   }
	store_i(pvslot, pvalue);
	return 0;
}

/* Look up a key on the dictionary stack. */
/* Return a pointer to its value, or 0 if not found. */
ref *
dict_search(ref *pkey, ref *dsptr)
{	ref *pdref = dsptr;
	ref *pvslot;
	while ( dict_find(pdref, pkey, &pvslot) <= 0 )
	   {	if ( --pdref < dstack ) return 0;
	   }
	return pvslot;
}

/* Return the number of elements in a dictionary. */
uint
dict_length(ref *pdref /* t_dictionary */)
{	return (uint)(pdref->value.pdict->count.value.intval);
}

/* Return the capacity of a dictionary. */
uint
dict_maxlength(ref *pdref /* t_dictionary */)
{	return npairs(pdref->value.pdict);
}

/* Copy one dictionary into another. */
int
dict_copy(ref *pdrfrom /* t_dictionary */, ref *pdrto /* t_dictionary */)
{	dict *pdict = pdrfrom->value.pdict;
	int count = npairs(pdict);
	pair *pp = pairs(pdict);
	int code;
	while ( count-- )
	   {	if ( r_type(&pp->key) != t_null )
			if ( (code = dict_put(pdrto, &pp->key, &pp->value)) != 0 )
				return code;
		pp++;
	   }
	return 0;
}

/* Resize a dictionary */
int
dict_resize(ref *pdrfrom, uint new_size)
{	dict *pdict = pdrfrom->value.pdict;
	ref drto;
	int code;
	if ( (code = dict_create(new_size, &drto)) < 0 ) return code;
	dict_copy(pdrfrom, &drto);	/* can't fail */
	/* Free the old dictionary */
	alloc_free((char *)pdict->contents.value.refs,
		  dict_maxlength(pdrfrom) * sizeof(pair), "dict_resize(old)");
	*pdict = *drto.value.pdict;
	/* Free the new dictionary header */
	alloc_free((char *)drto.value.pdict, sizeof(dict), "dict_resize(new)");
	return 0;
}

/* Prepare to enumerate a dictionary. */
int
dict_first(ref *pdref)
{	return numpairs(pdref);
}

/* Enumerate the next element of a dictionary. */
int
dict_next(ref *pdref, int index, ref *eltp /* ref eltp[2] */)
{	pair *pp = pairs(pdref->value.pdict) + index;
	while ( pp--, --index >= 0 )
	   {	if ( r_type(&pp->key) != t_null )
		   {	eltp[0] = pp->key;
			eltp[1] = pp->value;
			return index;
		   }
	   }
	return -1;			/* no more elements */
}