apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ian Holsman (x8608)" <i...@cnet.com>
Subject [Patch/Contrib] apr_hash_overlay (#2)
Date Tue, 17 Jul 2001 22:47:09 GMT
Ok..
the revised patch.(and test prog)

Index: include/apr_hash.h
===================================================================
RCS file: /home/cvspublic/apr/include/apr_hash.h,v
retrieving revision 1.25
diff -u -b -r1.25 apr_hash.h
--- include/apr_hash.h  2001/05/16 05:30:51 1.25
+++ include/apr_hash.h  2001/07/17 22:39:15
@@ -181,6 +181,18 @@
   */
  APR_DECLARE(int) apr_hash_count(apr_hash_t *ht);

+/**
+ * Merge two hash tables into one new hash table. The values of the 
'base' tabl
e
+ * override the values of the overlay if both have the same key.
+ * @param p The pool to use for the new hash table
+ * @param overlay The first table to put in the new hash table
+ * @param base The table to add at the end of the new hash table
+ * @return A new hash table containing all of the data from the two 
passed in
+ * @deffunc apr_hash_t *apr_hash_overlay(apr_pool_t *p, const 
apr_table_t *over
+lay, const apr_table_t *base);
+ */
+APR_DECLARE(apr_hash_t *) apr_hash_overlay(apr_pool_t *p,
+                       const apr_hash_t *overlay, const apr_hash_t *base);

  #ifdef __cplusplus
  }
Index: tables/apr_hash.c
===================================================================
RCS file: /home/cvspublic/apr/tables/apr_hash.c,v
retrieving revision 1.19
diff -u -b -r1.19 apr_hash.c
--- tables/apr_hash.c 2001/05/16 05:30:52 1.19
+++ tables/apr_hash.c 2001/07/17 22:39:18
@@ -325,3 +325,54 @@
  {
      return ht->count;
  }
+
+APR_DECLARE(apr_hash_t*) apr_hash_overlay(apr_pool_t*p, const 
apr_hash_t *overlay, const apr_hash_t*base)
+{
+    apr_hash_t *res;
+    apr_hash_index_t *hi;
+    apr_hash_entry_t *new_vals;
+    int i,j;
+
+#ifdef POOL_DEBUG
+    /* we don't copy keys and values, so it's necessary that
+     * overlay->a.pool and base->a.pool have a life span at least
+     * as long as p
+     */
+    if (!apr_pool_is_ancestor(overlay->a.pool, p)) {
+      fprintf(stderr,    "apr_hash_overlay: overlay's pool is not an 
ancestor of p\n");
+      abort();
+    }
+    if (!apr_pool_is_ancestor(base->a.pool, p)) {
+      fprintf(stderr,    "apr_hash_overlay: base's pool is not an 
ancestor of p\n");
+      abort();
+    }
+#endif
+
+
+    res = apr_palloc(p, sizeof(apr_hash_t));
+    res->pool = p;
+    res->count=base->count;
+    res->max=  (overlay->max > base->max)? overlay->max: base->max;
+    res->array = alloc_array(res, res->max);
+    new_vals = apr_palloc(p, sizeof( apr_hash_entry_t)*res->count);
+    j=0;
+    for (hi = apr_hash_first(base); hi; hi = apr_hash_next(hi)) {
+        i = hi->this->hash & res->max;
+
+        new_vals[j].klen = hi->this->klen;
+        new_vals[j].key = hi->this->key;
+        new_vals[j].val = hi->this->val;
+        new_vals[j].hash = hi->this->hash;
+        new_vals[j].next = res->array[i];
+        res->array[i] = &new_vals[j];
+        j++;
+    }
+    /* can't simply copy the stuff over, need to set each one so as to
+       increment the counts/array properly
+    */
+    for (hi = apr_hash_first(overlay); hi; hi = apr_hash_next(hi)) {
+        apr_hash_set(res,hi->this->key,hi->this->klen, hi->this->val);
+    }
+    return res;
+}

Index: test/Makefile.in
===================================================================
RCS file: /home/cvspublic/apr/test/Makefile.in,v
retrieving revision 1.59
diff -u -b -r1.59 Makefile.in
--- test/Makefile.in  2001/07/07 13:03:46 1.59
+++ test/Makefile.in  2001/07/17 22:39:18
@@ -24,6 +24,7 @@
          testmd5@EXEEXT@ \
          testpoll@EXEEXT@ \
          testmem@EXEEXT@ \
+        testhash@EXEEXT@ \
   occhild@EXEEXT@ \
          teststr@EXEEXT@

@@ -122,6 +123,9 @@

  testmem@EXEEXT@: testmem.lo $(LOCAL_LIBS)
   $(LINK) testmem.lo $(LOCAL_LIBS) $(ALL_LIBS)
+
+testhash@EXEEXT@: testhash.lo $(LOCAL_LIBS)
+ $(LINK) testhash.lo $(LOCAL_LIBS) $(ALL_LIBS)

  teststr@EXEEXT@: teststr.lo $(LOCAL_LIBS)
   $(LINK) teststr.lo $(LOCAL_LIBS) $(ALL_LIBS)

+
+

Mime
View raw message