Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
JiauZhang committed Mar 25, 2024
1 parent d599fc4 commit 1c32bb6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitpicker.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
"lxdialog/textbox.c",
"lxdialog/util.c",
"lxdialog/yesno.c",
"array_size.h",
"confdata.c",
"expr.c",
"expr.h",
"hashtable.h",
"internal.h",
"lexer.l",
"list.h",
"list_types.h",
"lkc_proto.h",
"lkc.h",
"mconf.c",
Expand Down
11 changes: 11 additions & 0 deletions array_size.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef ARRAY_SIZE_H
#define ARRAY_SIZE_H

/**
* ARRAY_SIZE - get the number of elements in array @arr
* @arr: array to be sized
*/
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

#endif /* ARRAY_SIZE_H */
48 changes: 48 additions & 0 deletions hashtable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef HASHTABLE_H
#define HASHTABLE_H

#include "array_size.h"
#include "list.h"

#define HASH_SIZE(name) (ARRAY_SIZE(name))

#define HASHTABLE_DECLARE(name, size) struct hlist_head name[size]

#define HASHTABLE_DEFINE(name, size) \
HASHTABLE_DECLARE(name, size) = \
{ [0 ... ((size) - 1)] = HLIST_HEAD_INIT }

#define hash_head(table, key) (&(table)[(key) % HASH_SIZE(table)])

/**
* hash_add - add an object to a hashtable
* @table: hashtable to add to
* @node: the &struct hlist_node of the object to be added
* @key: the key of the object to be added
*/
#define hash_add(table, node, key) \
hlist_add_head(node, hash_head(table, key))

/**
* hash_for_each - iterate over a hashtable
* @table: hashtable to iterate
* @obj: the type * to use as a loop cursor for each entry
* @member: the name of the hlist_node within the struct
*/
#define hash_for_each(table, obj, member) \
for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++) \
hlist_for_each_entry(obj, &table[_bkt], member)

/**
* hash_for_each_possible - iterate over all possible objects hashing to the
* same bucket
* @table: hashtable to iterate
* @obj: the type * to use as a loop cursor for each entry
* @member: the name of the hlist_node within the struct
* @key: the key of the objects to iterate over
*/
#define hash_for_each_possible(table, obj, member, key) \
hlist_for_each_entry(obj, hash_head(table, key), member)

#endif /* HASHTABLE_H */
17 changes: 17 additions & 0 deletions lxdialog/list_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef LIST_TYPES_H
#define LIST_TYPES_H

struct list_head {
struct list_head *next, *prev;
};

struct hlist_head {
struct hlist_node *first;
};

struct hlist_node {
struct hlist_node *next, **pprev;
};

#endif /* LIST_TYPES_H */

0 comments on commit 1c32bb6

Please sign in to comment.