apr-bugs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject [Bug 54550] New: Feature request: insert an entry into hash-table, if key is not already present
Date Mon, 11 Feb 2013 22:25:01 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=54550

            Bug ID: 54550
           Summary: Feature request: insert an entry into hash-table, if
                    key is not already present
           Product: APR
           Version: 1.4.6
          Hardware: All
                OS: All
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P2
         Component: APR
          Assignee: bugs@apr.apache.org
          Reporter: mi+apache@aldan.algebra.com
    Classification: Unclassified

A fairly common use-case involves checking, if a particular key is already
present in a hash-table and inserting it, if not:

   struct mystruct *value = apr_hash_get(table, key, klen);

   if (value == NULL) {
       value = create_new_value();
       apr_hash_table_set(table, key, klen, value);
   } else {
       verify_value(value);
       ...
   }

This means, the hash-table search is performed twice -- first by the
apr_hash_get, and then -- for the same key -- by the apr_hash_set. This is
suboptimal... There should be a function, that would insert a new entry (with
the value being NULL) under the specified key -- and allow the caller to set
the value. For example something like this:

   struct myststruct **pvalue;

   if (apr_hash_insert(table, key, klen, &pvalue)) {
       *pvalue = create_new_value();
   } else {
       verify_value(*pvalue);
       ...
   }

in the above apr_hash_insert the last argument will be filled with the address
of the value-pointer, that was either newly-inserted by the call (if the key
was a new one), or existed already (if the key was known). This would be
achieved with only a single hash-table search -- something find_entry() (the
static function behind both apr_hash_set and apr_hash_get) can already do.

The actual coding of this feature is not difficult. If there are no objections
in principle, I could come up with a patch. Please, advise. Thanks!

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org


Mime
View raw message