You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by mt...@apache.org on 2008/07/15 08:29:30 UTC

svn commit: r676807 - in /apr/apr/trunk: CHANGES include/apr_hash.h tables/apr_hash.c

Author: mturk
Date: Mon Jul 14 23:29:29 2008
New Revision: 676807

URL: http://svn.apache.org/viewvc?rev=676807&view=rev
Log:
Intruduce apr_hash_do function

Modified:
    apr/apr/trunk/CHANGES
    apr/apr/trunk/include/apr_hash.h
    apr/apr/trunk/tables/apr_hash.c

Modified: apr/apr/trunk/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/trunk/CHANGES?rev=676807&r1=676806&r2=676807&view=diff
==============================================================================
--- apr/apr/trunk/CHANGES [utf-8] (original)
+++ apr/apr/trunk/CHANGES [utf-8] Mon Jul 14 23:29:29 2008
@@ -1,5 +1,8 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 1.4.0
+  *) Intruduce apr_hash_do for iterating over a hash table.
+     [Mladen Turk]
+
   *) Rename apr_pool_create_core to apr_pool_create_unmanaged and
      deprecate the old API name. It better reflects the scope and usage
      of this function. [Mladen Turk]

Modified: apr/apr/trunk/include/apr_hash.h
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr_hash.h?rev=676807&r1=676806&r2=676807&view=diff
==============================================================================
--- apr/apr/trunk/include/apr_hash.h (original)
+++ apr/apr/trunk/include/apr_hash.h Mon Jul 14 23:29:29 2008
@@ -219,6 +219,36 @@
                                          const void *data);
 
 /**
+ * Declaration prototype for the iterator callback function of apr_hash_do().
+ *
+ * @param rec The data passed as the first argument to apr_hash_[v]do()
+ * @param key The key from this iteration of the hash table
+ * @param klen The key length from this iteration of the hash table
+ * @param value The value from this iteration of the hash table
+ * @remark Iteration continues while this callback function returns non-zero.
+ * To export the callback function for apr_hash_do() it must be declared 
+ * in the _NONSTD convention.
+ */
+typedef int (apr_hash_do_callback_fn_t)(void *rec, const void *key,
+                                                   apr_ssize_t klen,
+                                                   const void *value);
+
+/** 
+ * Iterate over a hash table running the provided function once for every
+ * element in the hash table. The @param comp function will be invoked for
+ * every element in the hash table.
+ *
+ * @param comp The function to run
+ * @param rec The data to pass as the first argument to the function
+ * @param ht The hash table to iterate over
+ * @return FALSE if one of the comp() iterations returned zero; TRUE if all
+ *            iterations returned non-zero
+ * @see apr_hash_do_callback_fn_t
+ */
+APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
+                             void *rec, const apr_hash_t *ht);
+
+/**
  * Get a pointer to the pool which the hash table was created in
  */
 APR_POOL_DECLARE_ACCESSOR(hash);

Modified: apr/apr/trunk/tables/apr_hash.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/tables/apr_hash.c?rev=676807&r1=676806&r2=676807&view=diff
==============================================================================
--- apr/apr/trunk/tables/apr_hash.c (original)
+++ apr/apr/trunk/tables/apr_hash.c Mon Jul 14 23:29:29 2008
@@ -474,4 +474,37 @@
     return res;
 }
 
+/* This is basically the following...
+ * for every element in hash table {
+ *    comp elemeny.key, element.value
+ * }
+ *
+ * Like with apr_table_do, the comp callback is called for each and every
+ * element of the hash table.
+ */
+APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
+                             void *rec, const apr_hash_t *ht)
+{
+    apr_hash_index_t  hix;
+    apr_hash_index_t *hi;
+    int rv, dorv  = 1;
+
+    hix.ht    = (apr_hash_t *)ht;
+    hix.index = 0;
+    hix.this  = NULL;
+    hix.next  = NULL;
+
+    if ((hi = apr_hash_next(&hix))) {
+        /* Scan the entire table */
+        do {
+            rv = (*comp)(rec, hi->this->key, hi->this->klen, hi->this->val);
+        } while ((hi = apr_hash_next(hi)));
+
+        if (rv == 0) {
+            dorv = 0;
+        }
+    }   
+    return dorv;
+}
+
 APR_POOL_IMPLEMENT_ACCESSOR(hash)



Re: svn commit: r676807 - in /apr/apr/trunk: CHANGES include/apr_hash.h tables/apr_hash.c

Posted by Mladen Turk <mt...@apache.org>.
Davi Arnaut wrote:
> 
> Iteration does not stop if one of the comp() iterations returned zero...
> 
> 
>          ^ Spurious space here
> 

Fixed. Thanks for keeping an extra eye :)

Regards
-- 
^(TM)

Re: svn commit: r676807 - in /apr/apr/trunk: CHANGES include/apr_hash.h tables/apr_hash.c

Posted by Mladen Turk <mt...@apache.org>.
Davi Arnaut wrote:
> 
> Iteration does not stop if one of the comp() iterations returned zero...
> 
> 
>          ^ Spurious space here
> 

Fixed. Thanks for keeping an extra eye :)

Regards
-- 
^(TM)

Re: svn commit: r676807 - in /apr/apr/trunk: CHANGES include/apr_hash.h tables/apr_hash.c

Posted by Davi Arnaut <da...@apache.org>.
mturk@apache.org wrote:
> Author: mturk
> Date: Mon Jul 14 23:29:29 2008
> New Revision: 676807
>

<snip>

>
>   /**
> + * Declaration prototype for the iterator callback function of apr_hash_do().
> + *
> + * @param rec The data passed as the first argument to apr_hash_[v]do()
> + * @param key The key from this iteration of the hash table
> + * @param klen The key length from this iteration of the hash table
> + * @param value The value from this iteration of the hash table
> + * @remark Iteration continues while this callback function returns non-zero.
> + * To export the callback function for apr_hash_do() it must be declared
> + * in the _NONSTD convention.
> + */
> +typedef int (apr_hash_do_callback_fn_t)(void *rec, const void *key,
> +                                                   apr_ssize_t klen,
> +                                                   const void *value);
> +
> +/**
> + * Iterate over a hash table running the provided function once for every
> + * element in the hash table. The @param comp function will be invoked for
> + * every element in the hash table.
> + *
> + * @param comp The function to run
> + * @param rec The data to pass as the first argument to the function
> + * @param ht The hash table to iterate over
> + * @return FALSE if one of the comp() iterations returned zero; TRUE if all
> + *            iterations returned non-zero
> + * @see apr_hash_do_callback_fn_t
> + */
> +APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
> +                             void *rec, const apr_hash_t *ht);
> +
> +/**
>    * Get a pointer to the pool which the hash table was created in
>    */
>   APR_POOL_DECLARE_ACCESSOR(hash);
>
> Modified: apr/apr/trunk/tables/apr_hash.c
> URL: http://svn.apache.org/viewvc/apr/apr/trunk/tables/apr_hash.c?rev=676807&r1=676806&r2=676807&view=diff
> ==============================================================================
> --- apr/apr/trunk/tables/apr_hash.c (original)
> +++ apr/apr/trunk/tables/apr_hash.c Mon Jul 14 23:29:29 2008
> @@ -474,4 +474,37 @@
>       return res;
>   }
>
> +/* This is basically the following...
> + * for every element in hash table {
> + *    comp elemeny.key, element.value
> + * }
> + *
> + * Like with apr_table_do, the comp callback is called for each and every
> + * element of the hash table.
> + */
> +APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
> +                             void *rec, const apr_hash_t *ht)
> +{
> +    apr_hash_index_t  hix;
> +    apr_hash_index_t *hi;
> +    int rv, dorv  = 1;
> +
> +    hix.ht    = (apr_hash_t *)ht;
> +    hix.index = 0;
> +    hix.this  = NULL;
> +    hix.next  = NULL;
> +
> +    if ((hi = apr_hash_next(&hix))) {
> +        /* Scan the entire table */
> +        do {
> +            rv = (*comp)(rec, hi->this->key, hi->this->klen, hi->this->val);
> +        } while ((hi = apr_hash_next(hi)));

Iteration does not stop if one of the comp() iterations returned zero...

> +
> +        if (rv == 0) {
> +            dorv = 0;
> +        }
> +    }

          ^ Spurious space here

> +    return dorv;
> +}
> +

Regards,

-- Davi Arnaut

Re: svn commit: r676807 - in /apr/apr/trunk: CHANGES include/apr_hash.h tables/apr_hash.c

Posted by Ruediger Pluem <rp...@apache.org>.

On 07/15/2008 08:29 AM, mturk@apache.org wrote:
> Author: mturk
> Date: Mon Jul 14 23:29:29 2008
> New Revision: 676807
> 
> URL: http://svn.apache.org/viewvc?rev=676807&view=rev
> Log:
> Intruduce apr_hash_do function
> 
> Modified:
>     apr/apr/trunk/CHANGES
>     apr/apr/trunk/include/apr_hash.h
>     apr/apr/trunk/tables/apr_hash.c
> 

> Modified: apr/apr/trunk/tables/apr_hash.c
> URL: http://svn.apache.org/viewvc/apr/apr/trunk/tables/apr_hash.c?rev=676807&r1=676806&r2=676807&view=diff
> ==============================================================================
> --- apr/apr/trunk/tables/apr_hash.c (original)
> +++ apr/apr/trunk/tables/apr_hash.c Mon Jul 14 23:29:29 2008
> @@ -474,4 +474,37 @@
>      return res;
>  }
>  
> +/* This is basically the following...
> + * for every element in hash table {
> + *    comp elemeny.key, element.value
> + * }
> + *
> + * Like with apr_table_do, the comp callback is called for each and every
> + * element of the hash table.
> + */
> +APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
> +                             void *rec, const apr_hash_t *ht)
> +{
> +    apr_hash_index_t  hix;
> +    apr_hash_index_t *hi;
> +    int rv, dorv  = 1;
> +
> +    hix.ht    = (apr_hash_t *)ht;
> +    hix.index = 0;
> +    hix.this  = NULL;
> +    hix.next  = NULL;
> +
> +    if ((hi = apr_hash_next(&hix))) {
> +        /* Scan the entire table */
> +        do {
> +            rv = (*comp)(rec, hi->this->key, hi->this->klen, hi->this->val);
> +        } while ((hi = apr_hash_next(hi)));

Shouldn't we leave the while loop if rv == 0?
If not the API promise might not be true, because if the last element returns
non zero the return value will be 1 although not all return values were non zero.

> +
> +        if (rv == 0) {
> +            dorv = 0;
> +        }
> +    }   
> +    return dorv;
> +}
> +

Regards

RĂ¼diger

Re: svn commit: r676807 - in /apr/apr/trunk: CHANGES include/apr_hash.h tables/apr_hash.c

Posted by Davi Arnaut <da...@apache.org>.
mturk@apache.org wrote:
> Author: mturk
> Date: Mon Jul 14 23:29:29 2008
> New Revision: 676807
>

<snip>

>
>   /**
> + * Declaration prototype for the iterator callback function of apr_hash_do().
> + *
> + * @param rec The data passed as the first argument to apr_hash_[v]do()
> + * @param key The key from this iteration of the hash table
> + * @param klen The key length from this iteration of the hash table
> + * @param value The value from this iteration of the hash table
> + * @remark Iteration continues while this callback function returns non-zero.
> + * To export the callback function for apr_hash_do() it must be declared
> + * in the _NONSTD convention.
> + */
> +typedef int (apr_hash_do_callback_fn_t)(void *rec, const void *key,
> +                                                   apr_ssize_t klen,
> +                                                   const void *value);
> +
> +/**
> + * Iterate over a hash table running the provided function once for every
> + * element in the hash table. The @param comp function will be invoked for
> + * every element in the hash table.
> + *
> + * @param comp The function to run
> + * @param rec The data to pass as the first argument to the function
> + * @param ht The hash table to iterate over
> + * @return FALSE if one of the comp() iterations returned zero; TRUE if all
> + *            iterations returned non-zero
> + * @see apr_hash_do_callback_fn_t
> + */
> +APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
> +                             void *rec, const apr_hash_t *ht);
> +
> +/**
>    * Get a pointer to the pool which the hash table was created in
>    */
>   APR_POOL_DECLARE_ACCESSOR(hash);
>
> Modified: apr/apr/trunk/tables/apr_hash.c
> URL: http://svn.apache.org/viewvc/apr/apr/trunk/tables/apr_hash.c?rev=676807&r1=676806&r2=676807&view=diff
> ==============================================================================
> --- apr/apr/trunk/tables/apr_hash.c (original)
> +++ apr/apr/trunk/tables/apr_hash.c Mon Jul 14 23:29:29 2008
> @@ -474,4 +474,37 @@
>       return res;
>   }
>
> +/* This is basically the following...
> + * for every element in hash table {
> + *    comp elemeny.key, element.value
> + * }
> + *
> + * Like with apr_table_do, the comp callback is called for each and every
> + * element of the hash table.
> + */
> +APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
> +                             void *rec, const apr_hash_t *ht)
> +{
> +    apr_hash_index_t  hix;
> +    apr_hash_index_t *hi;
> +    int rv, dorv  = 1;
> +
> +    hix.ht    = (apr_hash_t *)ht;
> +    hix.index = 0;
> +    hix.this  = NULL;
> +    hix.next  = NULL;
> +
> +    if ((hi = apr_hash_next(&hix))) {
> +        /* Scan the entire table */
> +        do {
> +            rv = (*comp)(rec, hi->this->key, hi->this->klen, hi->this->val);
> +        } while ((hi = apr_hash_next(hi)));

Iteration does not stop if one of the comp() iterations returned zero...

> +
> +        if (rv == 0) {
> +            dorv = 0;
> +        }
> +    }

          ^ Spurious space here

> +    return dorv;
> +}
> +

Regards,

-- Davi Arnaut