[Commit] nickle gram.y, 1.125, 1.126 hash.c, 1.1, 1.2 lex.l, 1.68, 1.69 nickle.h, 1.106, 1.107 pretty.c, 1.63, 1.64 type.c, 1.56, 1.57 value.h, 1.89, 1.90

Keith Packard commit at keithp.com
Mon Jul 21 22:32:38 PDT 2003


Committed by: keithp

Update of /local/src/CVS/nickle
In directory home.keithp.com:/tmp/cvs-serv9757

Modified Files:
	gram.y hash.c lex.l nickle.h pretty.c type.c value.h 
Log Message:
Eliminate bogus function layer in Hash API.
Switch hash initializes from colon to double arrow (=>)
Fix hash printing to produce re-parsable output
Make HashHash compute reasonable hash values
Add hash_key builtin


Index: gram.y
===================================================================
RCS file: /local/src/CVS/nickle/gram.y,v
retrieving revision 1.125
retrieving revision 1.126
diff -u -d -r1.125 -r1.126
--- gram.y	21 Jul 2003 22:22:10 -0000	1.125
+++ gram.y	22 Jul 2003 04:32:35 -0000	1.126
@@ -158,6 +158,7 @@
 %token <value>	    TEN_FLOAT OCTAL0_FLOAT OCTAL_FLOAT BINARY_FLOAT HEX_FLOAT
 %token <value>	    CHAR_CONST STRING_CONST POLY_CONST THREAD_CONST
 %token <value>	    VOIDVAL BOOLVAL
+%token		    DARROW
 
 %nonassoc 	POUND
 %right		COMMA
@@ -1452,8 +1453,8 @@
 		| hashelt
 		    { $$ = NewExprTree (COMMA, 0, $1); }
 		;
-hashelt		: hashvalue COLON hashvalue
-		    { $$ = NewExprTree (COLON, $1, $3); }
+hashelt		: hashvalue DARROW hashvalue
+		    { $$ = NewExprTree (DARROW, $1, $3); }
 		;
 hashvalue	: simpleexpr
 		| init

Index: hash.c
===================================================================
RCS file: /local/src/CVS/nickle/hash.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- hash.c	21 Jul 2003 22:22:10 -0000	1.1
+++ hash.c	22 Jul 2003 04:32:35 -0000	1.2
@@ -108,6 +108,7 @@
     HashValue	h;
     
     o = BoxElements (old);
+    ht->count = 0;
     for (h = old->nvalues / HashEltSize; h > 0; h--)
     {
 	if (HashEltCounted (o))
@@ -115,6 +116,7 @@
 	    /* XXX must rewrite references */
 	    n = Find (ht, HashEltHash(o), HashEltKey(o));
 	    HashEltCopy (n, o);
+	    ht->count++;
 	}
 	HashEltStep (o);
     }
@@ -138,96 +140,6 @@
     EXIT ();
 }
 
-static void
-Set (Value hv, Value key, Value hash, Value value)
-{
-    HashTablePtr    ht = &hv->hash;
-    Value	    *he;
-
-    if (ht->count == ht->hashSet->entries && 
-	ht->hashSet != &hashSets[NHASHSETS - 1])
-    {
-	Resize (ht, ht->hashSet + 1);
-    }
-    he = Find (ht, hash, key);
-    if (!HashEltCounted (he))
-	ht->count++;
-    HashEltHash (he) = hash;
-    HashEltKey (he) = key;
-    HashEltValue (he) = value;
-}
-
-static Value
-Get (Value hv, Value key, Value hash)
-{
-    HashTablePtr    ht = &hv->hash;
-    Value	    *he;
-
-    he = Find (ht, hash, key);
-    if (!HashEltValid (he))
-    {
-	RaiseStandardException (exception_uninitialized_value,
-				"uninitialized hash element", 0);
-	return (Void);
-    }
-    return HashEltValue (he);
-}
-
-static Value
-Refer (Value hv, Value key, Value hash)
-{
-    ENTER ();
-    HashTablePtr    ht = &hv->hash;
-    Value	    *he;
-
-    if (ht->count == ht->hashSet->entries && 
-	ht->hashSet != &hashSets[NHASHSETS - 1])
-    {
-	Resize (ht, ht->hashSet + 1);
-    }
-    he = Find (ht, hash, key);
-    if (!HashEltCounted (he))
-    {
-	ht->count++;
-	HashEltHash (he) = hash;
-	HashEltKey (he) = key;
-    }
-    RETURN (NewRef (ht->elts, 
-		    &HashEltValue(he) - BoxElements (ht->elts)));
-}
-
-static void
-Delete (Value hv, Value key, Value hash)
-{
-    HashTablePtr    ht = &hv->hash;
-    Value	    *he;
-
-    he = Find (ht, hash, key);
-    if (HashEltCounted (he))
-    {
-	/* mark this entry as deleted -- value Void, key NULL */
-	HashEltValue (he) = Void;
-	HashEltKey (he) = 0;
-	HashEltHash (he) = 0;
-	--ht->count;
-	if (ht->count < ht->hashSet->entries / 4 &&
-	    ht->hashSet != &hashSets[0])
-	{
-	    Resize (ht, ht->hashSet - 1);
-	}
-    }
-}
-
-static Value
-Test (Value hv, Value key, Value hash)
-{
-    HashTablePtr    ht = &hv->hash;
-    Value	    *he;
-
-    he = Find (ht, hash, key);
-    return HashEltValid (he) ? TrueVal : FalseVal;
-}
-
 int
 HashInit (void)
 {
@@ -268,30 +180,65 @@
     HashValue	h;
     Value	*e;
     Bool	first = True;
+    Bool	pretty = format == 'v' || format == 'g';
     
-    FilePuts (f, "{ ");
+    if (pretty)
+    {
+	FilePuts (f, "(");
+	FilePutBaseType (f, ht->type, False);
+	FilePuts (f, " ");
+	FilePuts (f, "[");
+	FilePutType (f, ht->keyType, False);
+	FilePuts (f, "]");
+	FilePutSubscriptType (f, ht->type, False);
+	FilePuts (f, ") {");
+    }
     e = BoxElements (ht->elts);
     for (h = ht->hashSet->size; h-- > 0; )
     {
 	if (HashEltValid (e))
 	{
-	    if (!first)
-		FilePuts (f, ", ");
-	    first = False;
-	    Print (f, HashEltKey (e), format, base, width, prec, fill);
-	    FilePuts (f, " : ");
+	    if (pretty)
+	    {
+		if (!first)
+		    FilePuts (f, ", ");
+		else
+		    FilePuts (f, " ");
+		Print (f, HashEltKey (e), format, base, width, prec, fill);
+		FilePuts (f, " => ");
+	    }
+	    else if (!first)
+		FilePuts (f, " ");
 	    Print (f, HashEltValue (e), format, base, width, prec, fill);
+	    first = False;
 	}
 	HashEltStep (e);
     }
-    FilePuts (f, " }");
+    if (pretty)
+    {
+	if (!first)
+	    FilePuts (f, " ");
+	FilePuts (f, "}");
+    }
     return True;
 }
 
 static HashValue
 HashHash (Value av)
 {
-    return 0;
+    HashTable	*ht = &av->hash;
+    HashValue	h;
+    Value	*e;
+    HashValue	hash = 0;
+    
+    e = BoxElements (ht->elts);
+    for (h = ht->hashSet->size; h-- > 0; )
+    {
+	if (HashEltValue (e))
+	    hash ^= ValueInt (HashEltHash (e));
+	HashEltStep (e);
+    }
+    return hash;
 }
 
 static void
@@ -346,31 +293,121 @@
 Value
 HashGet (Value hv, Value key)
 {
-    return Get (hv, key, ValueHash (key));
+    HashTablePtr    ht = &hv->hash;
+    Value	    hash = ValueHash (key);
+    Value	    *he;
+
+    he = Find (ht, hash, key);
+    if (!HashEltValid (he))
+    {
+	RaiseStandardException (exception_uninitialized_value,
+				"uninitialized hash element", 0);
+	return (Void);
+    }
+    return HashEltValue (he);
 }
 
 void
 HashSet (Value hv, Value key, Value value)
 {
-    Set (hv, key, ValueHash (key), value);
+    HashTablePtr    ht = &hv->hash;
+    Value	    hash = ValueHash (key);
+    Value	    *he;
+
+    if (ht->count == ht->hashSet->entries && 
+	ht->hashSet != &hashSets[NHASHSETS - 1])
+    {
+	Resize (ht, ht->hashSet + 1);
+    }
+    he = Find (ht, hash, key);
+    if (!HashEltCounted (he))
+	ht->count++;
+    HashEltHash (he) = hash;
+    HashEltKey (he) = key;
+    HashEltValue (he) = value;
 }
 
 Value
 HashRef (Value hv, Value key)
 {
-    return Refer (hv, key, ValueHash (key));
+    ENTER ();
+    HashTablePtr    ht = &hv->hash;
+    Value	    *he;
+    Value	    hash = ValueHash (key);
+
+    if (ht->count == ht->hashSet->entries && 
+	ht->hashSet != &hashSets[NHASHSETS - 1])
+    {
+	Resize (ht, ht->hashSet + 1);
+    }
+    he = Find (ht, hash, key);
+    if (!HashEltCounted (he))
+    {
+	ht->count++;
+	HashEltHash (he) = hash;
+	HashEltKey (he) = key;
+    }
+    RETURN (NewRef (ht->elts, 
+		    &HashEltValue(he) - BoxElements (ht->elts)));
 }
 
 Value
 HashTest (Value hv, Value key)
 {
-    return Test (hv, key, ValueHash (key));
+    HashTablePtr    ht = &hv->hash;
+    Value	    *he;
+    Value	    hash = ValueHash (key);
+
+    he = Find (ht, hash, key);
+    return HashEltValid (he) ? TrueVal : FalseVal;
 }
 
 void
 HashDelete (Value hv, Value key)
 {
-    Delete (hv, key, ValueHash (key));
+    HashTablePtr    ht = &hv->hash;
+    Value	    *he;
+    Value	    hash = ValueHash (key);
+
+    he = Find (ht, hash, key);
+    if (HashEltCounted (he))
+    {
+	/* mark this entry as deleted -- value Void, key NULL */
+	HashEltValue (he) = Void;
+	HashEltKey (he) = 0;
+	HashEltHash (he) = 0;
+	--ht->count;
+	if (ht->count < ht->hashSet->entries / 4 &&
+	    ht->hashSet != &hashSets[0])
+	{
+	    Resize (ht, ht->hashSet - 1);
+	}
+    }
+}
+
+Value
+HashKeys (Value hv)
+{
+    ENTER ();
+    HashTablePtr    ht = &hv->hash;
+    int		    dim = ht->count;
+    Value	    keys = NewArray (False, True, ht->keyType, 1, &dim);
+    HashValue	    h;
+    int		    i = 0;
+    Value	    *e = BoxElements (ht->elts);
+
+    for (h = ht->hashSet->size; h > 0; h--)
+    {
+	if (HashEltValid (e)) 
+	{
+	    BoxValueSet (keys->array.values, i, HashEltKey (e));
+	    i++;
+	}
+	HashEltStep (e);
+    }
+    if (i != dim)
+	ArrayResize (keys, 0, i);
+    RETURN (keys);
 }
 
 Value

Index: lex.l
===================================================================
RCS file: /local/src/CVS/nickle/lex.l,v
retrieving revision 1.68
retrieving revision 1.69
diff -u -d -r1.68 -r1.69
--- lex.l	29 May 2003 18:36:35 -0000	1.68
+++ lex.l	22 Jul 2003 04:32:35 -0000	1.69
@@ -347,6 +347,7 @@
 "..."		{ yylval.ints = DOTS; return DOTS; }
 "."		{ yylval.ints = DOT; return DOT; }
 "->"		{ yylval.ints = ARROW; return ARROW; }
+"=>"		{ yylval.ints = DARROW; return DARROW; }
 "<>"		{ yylval.value = Void; return VOIDVAL; }
 \n		{ 
 		    if (!ignorenl) { yylval.ints = NL; return NL; } 

Index: nickle.h
===================================================================
RCS file: /local/src/CVS/nickle/nickle.h,v
retrieving revision 1.106
retrieving revision 1.107
diff -u -d -r1.106 -r1.107
--- nickle.h	21 Jul 2003 22:22:10 -0000	1.106
+++ nickle.h	22 Jul 2003 04:32:35 -0000	1.107
@@ -1055,3 +1055,4 @@
 Value	do_hash_del (Value, Value);
 Value	do_hash_test (Value, Value);
 Value	do_hash_set (Value, Value, Value);
+Value	do_hash_keys (Value);

Index: pretty.c
===================================================================
RCS file: /local/src/CVS/nickle/pretty.c,v
retrieving revision 1.63
retrieving revision 1.64
diff -u -d -r1.63 -r1.64
--- pretty.c	21 Jul 2003 22:22:10 -0000	1.63
+++ pretty.c	22 Jul 2003 04:32:36 -0000	1.64
@@ -206,7 +206,7 @@
     while (e)
     {
 	PrettyExpr (f, e->tree.left->tree.left, -1, level, nest, pd);
-	FilePuts (f, " : ");
+	FilePuts (f, " => ");
 	PrettyExpr (f, e->tree.left->tree.right, -1, level, nest, pd);
 	e = e->tree.right;
 	if (e)

Index: type.c
===================================================================
RCS file: /local/src/CVS/nickle/type.c,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -d -r1.56 -r1.57
--- type.c	21 Jul 2003 22:22:10 -0000	1.56
+++ type.c	22 Jul 2003 04:32:36 -0000	1.57
@@ -385,9 +385,8 @@
 	    return TypeIsSupertype (super->array.type, sub->array.type);
 	return False;
     case type_hash:
-	/* contravariant */
 	return (TypeIsSupertype (super->hash.type, sub->hash.type) &&
-		TypeIsSupertype (sub->hash.keyType, super->hash.keyType));
+		TypeIsOrdered (super->hash.keyType, sub->hash.keyType));
     case type_struct:
     case type_union:
         super_st = super->structs.structs;

Index: value.h
===================================================================
RCS file: /local/src/CVS/nickle/value.h,v
retrieving revision 1.89
retrieving revision 1.90
diff -u -d -r1.89 -r1.90
--- value.h	21 Jul 2003 22:22:10 -0000	1.89
+++ value.h	22 Jul 2003 04:32:36 -0000	1.90
@@ -897,6 +897,7 @@
 Value	NewHash (Bool constant, TypePtr keyType, TypePtr valueType);
 Value	HashGet (Value hv, Value key);
 void	HashSet (Value hv, Value key, Value value);
+Value	HashKeys (Value hv);
 Value	HashRef (Value hv, Value key);
 Value	HashTest (Value hv, Value key);
 void	HashDelete (Value hv, Value key);




More information about the Commit mailing list