]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add function to get hash table's key/value pairs
authorStefan Berger <stefanb@linux.vnet.ibm.com>
Fri, 18 Nov 2011 16:58:17 +0000 (11:58 -0500)
committerStefan Berger <stefanb@us.ibm.com>
Fri, 18 Nov 2011 16:58:17 +0000 (11:58 -0500)
Add a function to the virHashTable for getting an array of the hash table's
key-value pairs and have the keys (optionally) sorted.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
src/libvirt_private.syms
src/util/hash.c
src/util/hash.h

index 33494d1cc7d42cc7eb359f22f26907e1dda992c6..fedac70605bec8ee430870ee856c5a2b3b67a52a 100644 (file)
@@ -559,6 +559,7 @@ virHashAddEntry;
 virHashCreate;
 virHashForEach;
 virHashFree;
+virHashGetItems;
 virHashLookup;
 virHashRemoveEntry;
 virHashRemoveSet;
@@ -566,6 +567,7 @@ virHashSearch;
 virHashSize;
 virHashSteal;
 virHashTableSize;
+virHashUpdateEntry;
 
 
 # hooks.h
index 42ccff74c50eca61cd21ee3e74e5c41fed9af3b9..665bcce1d57ddd4388a1835d1ec827412e1228b1 100644 (file)
@@ -618,3 +618,48 @@ void *virHashSearch(virHashTablePtr table,
 
     return NULL;
 }
+
+struct getKeysIter
+{
+    virHashKeyValuePair *sortArray;
+    unsigned arrayIdx;
+};
+
+static void virHashGetKeysIterator(void *payload,
+                                   const void *key, void *data)
+{
+    struct getKeysIter *iter = data;
+
+    iter->sortArray[iter->arrayIdx].key = key;
+    iter->sortArray[iter->arrayIdx].value = payload;
+
+    iter->arrayIdx++;
+}
+
+typedef int (*qsort_comp)(const void *, const void *);
+
+virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
+                                       virHashKeyComparator compar)
+{
+    int numElems = virHashSize(table);
+    struct getKeysIter iter = {
+        .arrayIdx = 0,
+        .sortArray = NULL,
+    };
+
+    if (numElems < 0)
+        return NULL;
+
+    if (VIR_ALLOC_N(iter.sortArray, numElems + 1)) {
+        virReportOOMError();
+        return NULL;
+    }
+
+    virHashForEach(table, virHashGetKeysIterator, &iter);
+
+    if (compar)
+        qsort(&iter.sortArray[0], numElems, sizeof(iter.sortArray[0]),
+              (qsort_comp)compar);
+
+    return iter.sortArray;
+}
index 7583abc90d34b0b5b1b9a61b1e967367f383895b..1ba12b91d33906989010c0af037159030ff7f6a4 100644 (file)
@@ -130,6 +130,28 @@ void *virHashLookup(virHashTablePtr table, const void *name);
  */
 void *virHashSteal(virHashTablePtr table, const void *name);
 
+/*
+ * Get the hash table's key/value pairs and have them optionally sorted.
+ * The returned array contains virHashSize() elements. Additionally,
+ * an empty element has been added to the end of the array (with key == NULL)
+ * to indicate the end of the array.
+ * The key/value pairs are only valid as long as the underlying hash
+ * table is not modified, i.e., no keys are removed or inserted, and
+ * the hash table is not deleted.
+ * The caller must only free the returned array using VIR_FREE().
+ * The caller must make copies of all returned keys and values if they are
+ * to be used somewhere else.
+ */
+typedef struct _virHashKeyValuePair virHashKeyValuePair;
+typedef virHashKeyValuePair *virHashKeyValuePairPtr;
+struct _virHashKeyValuePair {
+    const void *key;
+    const void *value;
+};
+typedef int (*virHashKeyComparator)(const virHashKeyValuePairPtr,
+                                    const virHashKeyValuePairPtr);
+virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
+                                       virHashKeyComparator compar);
 
 /*
  * Iterators