[Opensrf-commits] r1806 - in trunk: include/opensrf src/libopensrf (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Oct 5 10:48:26 EDT 2009


Author: scottmk
Date: 2009-10-05 10:48:24 -0400 (Mon, 05 Oct 2009)
New Revision: 1806

Modified:
   trunk/include/opensrf/osrf_hash.h
   trunk/src/libopensrf/osrf_hash.c
Log:
Created a new function osrfHashExtract().  It extracts an item with a
given key from an osrfHash, without destroying it, leaving the rest
of the osrfHash intact. 

M    include/opensrf/osrf_hash.h
M    src/libopensrf/osrf_hash.c


Modified: trunk/include/opensrf/osrf_hash.h
===================================================================
--- trunk/include/opensrf/osrf_hash.h	2009-10-05 02:01:39 UTC (rev 1805)
+++ trunk/include/opensrf/osrf_hash.h	2009-10-05 14:48:24 UTC (rev 1806)
@@ -35,6 +35,8 @@
 
 void* osrfHashRemove( osrfHash* hash, const char* key, ... );
 
+void* osrfHashExtract( osrfHash* hash, const char* key, ... );
+
 void* osrfHashGet( osrfHash* hash, const char* key );
 
 void* osrfHashGetFmt( osrfHash* hash, const char* key, ... );

Modified: trunk/src/libopensrf/osrf_hash.c
===================================================================
--- trunk/src/libopensrf/osrf_hash.c	2009-10-05 02:01:39 UTC (rev 1805)
+++ trunk/src/libopensrf/osrf_hash.c	2009-10-05 14:48:24 UTC (rev 1806)
@@ -300,11 +300,6 @@
 	return NULL;
 }
 
-/* Delete the entry for a specified key.  If the entry exists,
-   and there is no callback function to destroy the associated
-   item, return a pointer to the formerly associated item.
-   Otherwise return NULL.
-*/
 /**
 	@brief Remove the item for a specified key from an osrfHash.
 	@param hash Pointer to the osrfHash from which the item is to be removed.
@@ -367,7 +362,52 @@
 	return item;
 }
 
+/**
+	@brief Extract the item for a specified key from an osrfHash.
+	@param hash Pointer to the osrfHash from which the item is to be extracted.
+	@param key A printf-style format string to be expanded into the key for the item.
+		Subsequent parameters, if any, will be formatted and inserted into the expanded key.
+	@return Pointer to the extracted item, if any (see discussion).
+	
+	osrfHashRemove removes a specified entry without destroying it, and returns a pointer
+	to it.  If either of its first two parameters is NULL, or if no entry is present for the specified key, it returns NULL.
 
+	This function is identical to osrfHashRemove() except that it does not destroy the
+	specified item.
+*/
+void* osrfHashExtract( osrfHash* hash, const char* key, ... ) {
+	if(!(hash && key )) return NULL;
+
+	VA_LIST_TO_STRING(key);
+
+	osrfHashNode* node = find_item( hash, VA_BUF, NULL );
+	if( !node ) return NULL;
+
+	hash->size--;
+
+	void* item = node->item;  // to be returned
+
+	// Mark the node as logically deleted
+	free(node->key);
+	node->key = NULL;
+	node->item = NULL;
+
+	// Make the node unreachable from the rest of the linked list.
+	// We leave the next and prev pointers in place so that an
+	// iterator parked here can find its way to an adjacent node.
+	if( node->prev )
+		node->prev->next = node->next;
+	else
+		hash->first_key = node->next;
+
+	if( node->next )
+		node->next->prev = node->prev;
+	else
+		hash->last_key = node->prev;
+
+	return item;
+}
+
 /**
 	@brief Fetch the item stored in an osrfHash for a given key.
 	@param hash Pointer to the osrfHash from which to fetch the item.



More information about the opensrf-commits mailing list