Skip to content

Commit 23a8f76

Browse files
committed
opal: add the OPAL_HASH_TABLE_FOREACH macro
this is a convenience macro similar to the OPAL_LIST_FOREACH macro, that can be used to iterate on all the key/value pairs of an opal_hash_table_t
1 parent 014f917 commit 23a8f76

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

opal/class/opal_hash_table.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Copyright (c) 2014-2015 Mellanox Technologies, Inc.
1515
* All rights reserved.
1616
* Copyright (c) 2014 Intel, Inc. All rights reserved.
17-
* Copyright (c) 2014 Research Organization for Information Science
17+
* Copyright (c) 2014-2016 Research Organization for Information Science
1818
* and Technology (RIST). All rights reserved.
1919
* $COPYRIGHT$
2020
*
@@ -463,6 +463,28 @@ OPAL_DECLSPEC int opal_proc_table_get_next_key(opal_proc_table_t *pt, opal_proce
463463
void **value, void *in_node1, void **out_node1,
464464
void *in_node2, void **out_node2);
465465

466+
/**
467+
* Loop over a hash table.
468+
*
469+
* @param[in] key Key for each item
470+
* @param[in] type Type of key (ui32|ui64|ptr)
471+
* @param[in] value Storage for each item
472+
* @param[in] ht Hash table to iterate over
473+
*
474+
* This macro provides a simple way to loop over the items in an opal_hash_table_t. It
475+
* is not safe to call opal_hash_table_remove* from within the loop.
476+
*
477+
* Example Usage:
478+
*
479+
* uint64_t key;
480+
* void * value;
481+
* OPAL_HASH_TABLE_FOREACH(key, uint64, value, ht) {
482+
* do_something(key, value);
483+
* }
484+
*/
485+
#define OPAL_HASH_TABLE_FOREACH(key, type, value, ht) \
486+
for (void *_nptr=NULL; \
487+
OPAL_SUCCESS == opal_hash_table_get_next_key_##type(ht, &key, (void **)&value, _nptr, &_nptr);)
466488

467489
END_C_DECLS
468490

0 commit comments

Comments
 (0)