rev 238 - in trunk: include/prothon src

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: bcollins
Date: 2004-03-31 14:43:45 -0500 (Wed, 31 Mar 2004)
New Revision: 238

Modified:
   trunk/include/prothon/prothon.h
   trunk/include/prothon/prothon_dll.h
   trunk/src/builtins.c
   trunk/src/object.c
Log:
Redo dictgen so that it returns a list with the key,value instead of just
the key.


Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-03-31 18:49:35 UTC (rev 237)
+++ trunk/include/prothon/prothon.h	2004-03-31 19:43:45 UTC (rev 238)
@@ -609,7 +609,14 @@
 // when read-locked) then the order will stay the same.  The order will match
 // the order of values given by dict_values() if they are both called while the
 // dict is read-locked.
-#define dict_keys(ist, dict_obj)   dict_keys_values(ist, dict_obj, PR_TRUE)
+#define dict_keys(ist, dict_obj)   dict_keys_values(ist, dict_obj, 1)
+
+// DICY_KEYS_VALUES: Implmentation for getting a list of keys, values or
+// key/values in a list from a dict. Possible args for key_flg:
+//
+// 0 = List of values
+// 1 = List of keys
+// 2 = List of keys and values (odd list items are keys, even are values)
 obj_p dict_keys_values(isp ist, obj_p dict_obj, int key_flg);
 
 // DICT_VALUES: Return a list object containing all values in the dictionary.
@@ -618,7 +625,7 @@
 // when read-locked) then the order will stay the same.  The order will match
 // the order of keys given by dict_keys() if they are both called while the
 // dict is read-locked.
-#define dict_values(ist, dict_obj) dict_keys_values(ist, dict_obj, FALSE)
+#define dict_values(ist, dict_obj) dict_keys_values(ist, dict_obj, 0)
 
 // HASH_MASK: Maximum value for any hash function to return.
 // Minimum value is 1.

Modified: trunk/include/prothon/prothon_dll.h
===================================================================
--- trunk/include/prothon/prothon_dll.h	2004-03-31 18:49:35 UTC (rev 237)
+++ trunk/include/prothon/prothon_dll.h	2004-03-31 19:43:45 UTC (rev 238)
@@ -114,8 +114,8 @@
 	void		  (*del_unlock)(obj_p obj);	
 	void	   (*set_immutable)(obj_p obj);		
 	void	   (*clr_immutable)(obj_p obj);
+	obj_p	   (*dict_keys_values)(isp ist, obj_p dict_obj, int key_flg);
 
-
 } pr_services_t;
 
 typedef void (* dll_initcall_t)(pr_services_t *);
@@ -244,7 +244,8 @@
 #define del_unlock			(*services->del_unlock)	
 #define set_immutable		(*services->set_immutable)	
 #define clr_immutable		(*services->clr_immutable)	
-#define get_pr_head_pool	(*services->get_pr_head_pool)	
+#define get_pr_head_pool	(*services->get_pr_head_pool)
+#define dict_keys_values	(*services->dict_keys_values)
 
 #endif // OBJECT_H
 #endif // PROTHON_DLL_H

Modified: trunk/src/builtins.c
===================================================================
--- trunk/src/builtins.c	2004-03-31 18:49:35 UTC (rev 237)
+++ trunk/src/builtins.c	2004-03-31 19:43:45 UTC (rev 238)
@@ -1283,7 +1283,7 @@
 	obj_p list_obj, gen_obj = new_object(DictGen_OBJ);
 
 	gen_obj->data_type = OBJ_TYPE_DATAPTR;
-	gen_obj->data.ptr = list_obj = clone_list_obj(ist, dict_keys(ist, self));
+	gen_obj->data.ptr = list_obj = dict_keys_values(ist, self, 2);
 
 	listlen(list_obj) = 0;
 	return gen_obj;
@@ -1298,14 +1298,24 @@
 
 DEF(DictGen, next, NULL) {
 	obj_p res, list_obj = self->data.ptr;
-	size_t lsiz = listsize(list_obj);
-	size_t llen = listlen(list_obj);
+	size_t lsiz, llen;
+
+	CHECK_TYPE_EXC(self, DictGen_OBJ, "DictGen");
+	CHECK_TYPE_EXC(list_obj, OBJ(SEQ_PROTO), "list");
+
+	lsiz = listsize(list_obj);
+	llen = listlen(list_obj);
+
 	if (llen == lsiz) {
 		raise_exception(ist, OBJ(STOP_ITERATION_EXC), NULL);
 		return NULL;
 	}
-	res = listitem(list_obj, llen);
-	listlen(list_obj)++;
+
+	res = new_list_obj(2);
+	list_append(ist, res, list_item(ist, list_obj, llen));
+	list_append(ist, res, list_item(ist, list_obj, llen + 1));
+	listlen(list_obj) += 2;
+
 	return res;
 }
 

Modified: trunk/src/object.c
===================================================================
--- trunk/src/object.c	2004-03-31 18:49:35 UTC (rev 237)
+++ trunk/src/object.c	2004-03-31 19:43:45 UTC (rev 238)
@@ -185,6 +185,7 @@
 	dll_services.set_immutable	  = set_immutable;
 	dll_services.clr_immutable	  = clr_immutable;
 	dll_services.get_pr_head_pool = get_pr_head_pool;
+	dll_services.dict_keys_values = dict_keys_values;
 
 	/* Load all of our builtin dll's */
 	dll_initcalls++;
@@ -1095,7 +1096,10 @@
 	for(i=0; i < size; i++)
 		if ((dp=dictptr(dict,i))->entry.hash > 0) {
 			assert (cnt++ < len);
-			list_append(ist, list_obj, (key_flg ? dp->entry.key : dp->entry.value));
+			if (key_flg == 1 || key_flg == 2)
+				list_append(ist, list_obj, dp->entry.key);
+			if (key_flg == 0 || key_flg == 2)
+				list_append(ist, list_obj, dp->entry.value);
 		}
 	read_unlock(ist, dict_obj);
 	return list_obj;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.