Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Zbigniew Jędrzejewski-Szmek
polkit
Commits
a32ca44f
Commit
a32ca44f
authored
Nov 05, 2007
by
David Zeuthen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement polkit_hash_foreach
parent
ef211df9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
36 deletions
+121
-36
polkit/polkit-hash.c
polkit/polkit-hash.c
+90
-28
polkit/polkit-hash.h
polkit/polkit-hash.h
+31
-8
No files found.
polkit/polkit-hash.c
View file @
a32ca44f
...
...
@@ -78,8 +78,8 @@ struct _PolKitHash
* polkit_hash_new:
* @hash_func: The hash function to use
* @key_equal_func: The function used to determine key equality
* @key_destroy_func: Function for freeing keys
* @value_destroy_func: Function for freeing values
* @key_destroy_func: Function for freeing keys
or #NULL
* @value_destroy_func: Function for freeing values
or #NULL
*
* Creates a new Hash Table.
*
...
...
@@ -202,6 +202,9 @@ polkit_hash_insert (PolKitHash *hash,
PolKitHashNode
**
nodep
;
PolKitHashNode
*
node
;
g_return_val_if_fail
(
hash
!=
NULL
,
FALSE
);
g_return_val_if_fail
(
key
!=
NULL
,
FALSE
);
ret
=
FALSE
;
bucket
=
hash
->
hash_func
(
key
)
%
hash
->
num_top_nodes
;
...
...
@@ -261,6 +264,13 @@ polkit_hash_lookup (PolKitHash *hash, void *key, polkit_bool_t *found)
void
*
value
;
PolKitHashNode
*
node
;
value
=
NULL
;
if
(
found
!=
NULL
)
*
found
=
FALSE
;
g_return_val_if_fail
(
hash
!=
NULL
,
NULL
);
g_return_val_if_fail
(
key
!=
NULL
,
NULL
);
bucket
=
hash
->
hash_func
(
key
)
%
hash
->
num_top_nodes
;
node
=
hash
->
top_nodes
[
bucket
];
...
...
@@ -277,96 +287,141 @@ polkit_hash_lookup (PolKitHash *hash, void *key, polkit_bool_t *found)
}
}
value
=
NULL
;
if
(
found
!=
NULL
)
*
found
=
FALSE
;
out:
return
value
;
}
/**
* p_direct_hash:
* @key: the key
* polkit_hash_foreach:
* @hash: the hash table
* @cb: callback function
* @user_data: user data
*
*
Converts a pointer to
a hash
value.
*
Iterate over all elements in
a hash
table
*
* Returns:
a hash value corresponding to the key
* Returns:
#TRUE only if the callback short-circuited the iteration
*
* Since: 0.7
*/
polkit_
uint32
_t
p
_direct_hash
(
const
void
*
key
)
polkit_
bool
_t
p
olkit_hash_foreach
(
PolKitHash
*
hash
,
PolKitHashForeachFunc
cb
,
void
*
user_data
)
{
/* TODO: reimplement */
return
g_direct_hash
(
key
);
int
n
;
g_return_val_if_fail
(
hash
!=
NULL
,
FALSE
);
g_return_val_if_fail
(
cb
!=
NULL
,
FALSE
);
for
(
n
=
0
;
n
<
hash
->
num_top_nodes
;
n
++
)
{
PolKitHashNode
*
node
;
for
(
node
=
hash
->
top_nodes
[
n
];
node
!=
NULL
;
node
=
node
->
next
)
{
if
(
cb
(
hash
,
node
->
key
,
node
->
value
,
user_data
))
return
TRUE
;
}
}
return
FALSE
;
}
/**
* p
_str_hash
:
* p
olkit_hash_direct_hash_func
:
* @key: the key
*
* Converts a
string
to a hash value.
* Converts a
pointer
to a hash value.
*
* Returns: a hash value corresponding to the key
*
* Since: 0.7
*/
polkit_uint32_t
p
_str_hash
(
const
void
*
key
)
polkit_uint32_t
p
olkit_hash_direct_hash_func
(
const
void
*
key
)
{
/* TODO: reimplement */
return
g_
str
_hash
(
key
);
return
g_
direct
_hash
(
key
);
}
/**
* p_direct_equal:
* p
olkit_hash
_direct_equal
_func
:
* @v1: first value
* @v2: second value
*
* Compares to pointers and return #TRUE if they are equal (same address).
* Compares t
w
o pointers and return #TRUE if they are equal (same address).
*
* Returns: #TRUE only if the values are equal
*
* Since: 0.7
*/
polkit_bool_t
p_direct_equal
(
const
void
*
v1
,
const
void
*
v2
)
p
olkit_hash
_direct_equal
_func
(
const
void
*
v1
,
const
void
*
v2
)
{
/* TODO: reimplement */
return
g_direct_equal
(
v1
,
v2
);
}
/**
* p_str_equal:
* polkit_hash_str_hash_func:
* @key: the key
*
* Converts a string to a hash value.
*
* Returns: a hash value corresponding to the key
*
* Since: 0.7
*/
polkit_uint32_t
polkit_hash_str_hash_func
(
const
void
*
key
)
{
/* TODO: reimplement */
return
g_str_hash
(
key
);
}
/**
* polkit_hash_str_equal_func:
* @v1: first value
* @v2: second value
*
* Compares to strings and return #TRUE if they are equal.
* Compares t
w
o strings and return #TRUE if they are equal.
*
* Returns: #TRUE only if the values are equal
*
* Since: 0.7
*/
polkit_bool_t
p_str_equal
(
const
void
*
v1
,
const
void
*
v2
)
p
olkit_hash
_str_equal
_func
(
const
void
*
v1
,
const
void
*
v2
)
{
/* TODO: reimplement */
return
g_str_equal
(
v1
,
v2
);
}
#ifdef POLKIT_BUILD_TESTS
static
polkit_bool_t
_it1
(
PolKitHash
*
hash
,
void
*
key
,
void
*
value
,
void
*
user_data
)
{
int
*
count
=
(
int
*
)
user_data
;
*
count
+=
1
;
return
FALSE
;
}
static
polkit_bool_t
_it2
(
PolKitHash
*
hash
,
void
*
key
,
void
*
value
,
void
*
user_data
)
{
int
*
count
=
(
int
*
)
user_data
;
*
count
+=
1
;
return
TRUE
;
}
static
polkit_bool_t
_run_test
(
void
)
{
int
count
;
PolKitHash
*
h
;
polkit_bool_t
found
;
/* string hash tables */
if
((
h
=
polkit_hash_new
(
p
_str_hash
,
p
_str_equal
,
p_free
,
p_free
))
!=
NULL
)
{
if
((
h
=
polkit_hash_new
(
p
olkit_hash_str_hash_func
,
polkit_hash
_str_equal
_func
,
p_free
,
p_free
))
!=
NULL
)
{
int
n
;
char
*
key
;
char
*
value
;
...
...
@@ -432,6 +487,13 @@ _run_test (void)
g_assert
(
found
&&
value
!=
NULL
&&
strcmp
(
value
,
"val1-replaced"
)
==
0
);
}
}
count
=
0
;
g_assert
(
polkit_hash_foreach
(
h
,
_it1
,
&
count
)
==
FALSE
);
g_assert
(
count
==
((
sizeof
(
test_data
)
/
sizeof
(
char
*
)
-
1
)
/
2
));
count
=
0
;
g_assert
(
polkit_hash_foreach
(
h
,
_it2
,
&
count
)
==
TRUE
);
g_assert
(
count
==
1
);
polkit_hash_ref
(
h
);
polkit_hash_unref
(
h
);
...
...
@@ -441,7 +503,7 @@ _run_test (void)
}
/* direct hash tables */
if
((
h
=
polkit_hash_new
(
p_direct_hash
,
p
_direct_equal
,
NULL
,
NULL
))
!=
NULL
)
{
if
((
h
=
polkit_hash_new
(
p
olkit_hash
_direct_hash
_func
,
polkit_hash
_direct_equal
_func
,
NULL
,
NULL
))
!=
NULL
)
{
if
(
polkit_hash_insert
(
h
,
h
,
h
))
{
g_assert
((
polkit_hash_lookup
(
h
,
h
,
&
found
)
==
h
)
&&
found
);
if
(
polkit_hash_insert
(
h
,
h
,
NULL
))
{
...
...
polkit/polkit-hash.h
View file @
a32ca44f
...
...
@@ -42,9 +42,9 @@ typedef struct _PolKitHash PolKitHash;
* @key: a key
*
* The function is passed a key and should return a hash value. The
* functions p
_direct_hash(), p_in
t_hash() and
p_str_hash() provide
*
hash functions which can be used when the key is a pointer, an
*
integer,
and char* respectively.
* functions p
olkit_hash_direc
t_hash
_func
() and
*
polkit_hash_str_hash_func() provide hash functions which can be
*
used when the key is a pointer
and
an
char* respectively.
*
* Returns: the hash value corresponding to the key
*
...
...
@@ -57,7 +57,10 @@ typedef polkit_uint32_t (*PolKitHashFunc) (const void *key);
* @key1: first key
* @key2: second key
*
* Determines if two keys are equal.
* Determines if two keys are equal. The functions
* polkit_hash_direct_equal_func() and polkit_hash_str_equal_func()
* provide equality functions which can be used when the key is a
* pointer and an char* respectively.
*
* Returns: #TRUE iff the keys are equal
*
...
...
@@ -77,6 +80,24 @@ typedef polkit_bool_t (*PolKitEqualFunc) (const void *key1, const void *key2);
*/
typedef
void
(
*
PolKitFreeFunc
)
(
void
*
p
);
/**
* PolKitHashForeachFunc:
* @hash: the hash table
* @key: key
* @value: value
* @user_data: user data passed to polkit_hash_foreach()
*
* Type signature for callback function used in polkit_hash_foreach().
*
* Returns: Return #TRUE to short-circuit, e.g. stop the iteration.
*
* Since: 0.7
*/
typedef
polkit_bool_t
(
*
PolKitHashForeachFunc
)
(
PolKitHash
*
hash
,
void
*
key
,
void
*
value
,
void
*
user_data
);
PolKitHash
*
polkit_hash_new
(
PolKitHashFunc
hash_func
,
PolKitEqualFunc
key_equal_func
,
...
...
@@ -90,12 +111,14 @@ polkit_bool_t polkit_hash_insert (PolKitHash *hash, void *key, void *value);
void
*
polkit_hash_lookup
(
PolKitHash
*
hash
,
void
*
key
,
polkit_bool_t
*
found
);
polkit_
uint32_t
p_direct_hash
(
const
void
*
key
);
polkit_uint32_t
p_str_hash
(
const
void
*
key
);
polkit_
bool_t
polkit_hash_foreach
(
PolKitHash
*
hash
,
PolKitHashForeachFunc
cb
,
void
*
user_data
);
polkit_uint32_t
polkit_hash_direct_hash_func
(
const
void
*
key
);
polkit_bool_t
polkit_hash_direct_equal_func
(
const
void
*
v1
,
const
void
*
v2
);
polkit_
bool_t
p_direct_equal
(
const
void
*
v1
,
const
void
*
v2
);
polkit_bool_t
p
_str_equal
(
const
void
*
v1
,
const
void
*
v2
);
polkit_
uint32_t
polkit_hash_str_hash_func
(
const
void
*
key
);
polkit_bool_t
polkit_hash
_str_equal
_func
(
const
void
*
v1
,
const
void
*
v2
);
POLKIT_END_DECLS
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment