Files | Defines | Functions

Cp_hashlist

Files

file  hashlist.c
 

Implementation for cp_hashlist Collection with linked elements and hash key and with cp_hashlist_iterator.


Defines

#define CP_HASHLIST_MULTIPLE_VALUES   1

Functions

int cp_hashlist_txlock (cp_hashlist *list, int type)
int cp_hashlist_txunlock (cp_hashlist *list)
static cp_hashlist_entry * cp_hashlist_create_entry (cp_hashlist *list, int mode, void *key, void *value)
static void cp_hashlist_entry_delete (cp_hashlist_entry *entry)
static void cp_hashlist_entry_release_by_option (cp_hashlist *list, cp_hashlist_entry *entry, int mode)
static cp_hashlist_entry * cp_hashlist_insert_internal (cp_hashlist *list, cp_hashlist_entry *entry)
 insert an entry
static cp_hashlist_entry * cp_hashlist_remove_internal (cp_hashlist *list, void *key)
 remove first entry matching key
static cp_hashlist_entry * cp_hashlist_remove_entry_internal (cp_hashlist *list, cp_hashlist_entry *entry)
 remove specified entry
static void * cp_hashlist_get_internal (cp_hashlist *list, void *key)
cp_hashlistcp_hashlist_create_by_option (int mode, unsigned long size_hint, cp_hashfunction hash_fn, cp_compare_fn compare_fn, cp_copy_fn copy_key, cp_destructor_fn free_key, cp_copy_fn copy_value, cp_destructor_fn free_value)
 Constructor for copy mode.
cp_hashlistcp_hashlist_create_by_mode (int mode, unsigned long size_hint, cp_hashfunction hash_fn, cp_compare_fn compare_fn)
 constructor with parameters.
static void cp_hashlist_destroy_internal (cp_hashlist *list, int mode, cp_destructor_fn dk, cp_destructor_fn dv)
void cp_hashlist_destroy (cp_hashlist *list)
 Destroy the list with the mode stored in the list.
void cp_hashlist_destroy_deep (cp_hashlist *list)
 Destroy the list with the mode stored in the list plus COLLECTION_MODE_DEEP.
void cp_hashlist_destroy_by_option (cp_hashlist *list, int mode)
 Destroy the object with the specified mode (override default).
void cp_hashlist_destroy_custom (cp_hashlist *list, cp_destructor_fn dk, cp_destructor_fn dv)
 This function does exactly what you would think it does.
int cp_hashlist_callback (cp_hashlist *list, int(*cb)(void *key, void *value, void *id), void *id)
 iterates over the list and calls the callback function on each item.
int cp_hashlist_get_mode (cp_hashlist *list)
 find out what mode your cp_hashlist is running in
int cp_hashlist_set_mode (cp_hashlist *list, int mode)
 set the mode on your cp_hashlist
int cp_hashlist_unset_mode (cp_hashlist *list, int mode)
 unset mode bits on list
int cp_hashlist_set_min_size (cp_hashlist *list, unsigned long min_size)
 the internal table will not be resized to less than min_size
int cp_hashlist_set_max_fill_factor (cp_hashlist *list, int fill_factor)
 a resize is triggered when the table contains more items than table_size * fill_factor / 100
int cp_hashlist_set_min_fill_factor (cp_hashlist *list, int fill_factor)
 a resize is triggered when the table contains less items than table_size * fill_factor / 100 if table_size > min_size
void * cp_hashlist_resize_nosync (cp_hashlist *list, unsigned long new_size)
void * cp_hashlist_append (cp_hashlist *list, void *key, void *value)
 Append a new element (key, value) at the end of the list.
static int cp_hashlist_contains_internal (cp_hashlist *list, void *key)
int cp_hashlist_contains (cp_hashlist *list, void *key)
 returns non-zero if list contains key
void * cp_hashlist_append_by_option (cp_hashlist *list, void *key, void *value, int mode)
 Append a new element (key, value) at the end of the list with mode.
void * cp_hashlist_insert (cp_hashlist *list, void *key, void *value)
 Insert a new element (key, value) at the beginning of the list.
void * cp_hashlist_insert_by_option (cp_hashlist *list, void *key, void *value, int mode)
 Insert a new element (key, value) at the beginning of the list with mode.
void * cp_hashlist_get (cp_hashlist *list, void *key)
 Returns the first element with matching key.
static void * cp_hashlist_unlink_internal (cp_hashlist *list, cp_hashlist_entry *holder, int mode)
void * cp_hashlist_remove (cp_hashlist *list, void *key)
 Removes the entry with matching key and destroys it (internal locking mode).
void * cp_hashlist_remove_deep (cp_hashlist *list, void *key)
void * cp_hashlist_get_head (cp_hashlist *list)
 Returns the first element of the list.
void * cp_hashlist_get_tail (cp_hashlist *list)
 Returns the last element of the list.
void * cp_hashlist_remove_head (cp_hashlist *list)
 Removes the first entry and destroys it.
void * cp_hashlist_remove_head_by_option (cp_hashlist *list, int mode)
 Removes the first entry and destroys it.
void * cp_hashlist_remove_tail (cp_hashlist *list)
 Removes the last entry and destroys it.
void * cp_hashlist_remove_tail_by_option (cp_hashlist *list, int mode)
 Removes the last entry and destroys it.
unsigned long cp_hashlist_item_count (cp_hashlist *list)
 Get the number of elements in the collection.
int cp_hashlist_lock_internal (cp_hashlist *list, int lock_mode)
int cp_hashlist_unlock_internal (cp_hashlist *list)
int cp_hashlist_lock (cp_hashlist *list, int type)
 Locks the collection with the specified mode.
int cp_hashlist_unlock (cp_hashlist *list)
 Unlock the object.
void * cp_hashlist_entry_get_key (cp_hashlist_entry *entry)
 Get the key of the entry.
void * cp_hashlist_entry_get_value (cp_hashlist_entry *entry)
 Get the value of the entry.
int cp_hashlist_is_empty (cp_hashlist *list)
 Test if object is empty.
cp_hashlist_iteratorcp_hashlist_create_iterator (cp_hashlist *list, int type)
 Create a new iterator and initialize it at the beginning.
int cp_hashlist_iterator_init (cp_hashlist_iterator *iterator, cp_hashlist *list, int type)
int cp_hashlist_iterator_head (cp_hashlist_iterator *iterator)
 initialize the iterator at the beginning
int cp_hashlist_iterator_tail (cp_hashlist_iterator *iterator)
 set iterator at list tail
int cp_hashlist_iterator_init_tail (cp_hashlist_iterator *iterator, cp_hashlist *list, int type)
 Initialize the Iterator at the end.
int cp_hashlist_iterator_to_key (cp_hashlist_iterator *iterator, void *key)
 set iterator position at first occurence of given key
int cp_hashlist_iterator_release (cp_hashlist_iterator *iterator)
 Unlock the list of the Iterator.
int cp_hashlist_iterator_destroy (cp_hashlist_iterator *iterator)
 iterator destructor
cp_hashlist_entry * cp_hashlist_iterator_next (cp_hashlist_iterator *iterator)
 Go to the next entry in the list and return the content.
void * cp_hashlist_iterator_next_key (cp_hashlist_iterator *iterator)
 Go to the next entry in the list and return the key.
void * cp_hashlist_iterator_next_value (cp_hashlist_iterator *iterator)
 Go to the next entry in the list and return the content.
cp_hashlist_entry * cp_hashlist_iterator_prev (cp_hashlist_iterator *iterator)
 Go to the previous entry in the list and return the content.
void * cp_hashlist_iterator_prev_key (cp_hashlist_iterator *iterator)
 Go to the previous entry in the list and return the key.
void * cp_hashlist_iterator_prev_value (cp_hashlist_iterator *iterator)
 Go to the previous entry in the list and return the content.
cp_hashlist_entry * cp_hashlist_iterator_curr (cp_hashlist_iterator *iterator)
 return the entry at the current iterator position
void * cp_hashlist_iterator_curr_key (cp_hashlist_iterator *iterator)
 return the key at the current iterator position
void * cp_hashlist_iterator_curr_value (cp_hashlist_iterator *iterator)
 return the value at the current iterator position
cp_hashlist_entry * cp_hashlist_iterator_insert (cp_hashlist_iterator *iterator, void *key, void *value)
 add a mapping before the current iterator position
cp_hashlist_entry * cp_hashlist_iterator_append (cp_hashlist_iterator *iterator, void *key, void *value)
 add a mapping after the current iterator position
void * cp_hashlist_iterator_remove (cp_hashlist_iterator *iterator)
 remove the mapping at the current iterator position



void * cp_hashlist_remove_by_option (cp_hashlist *list, void *key, int mode)
 Removes the entry with matching key and destroys it with locking mode.

Function Documentation

void* cp_hashlist_append ( cp_hashlist list,
void *  key,
void *  value 
)

Append a new element (key, value) at the end of the list.

The operation is synchronized according to the properties of the object.

Precondition:
list != NULL
key != NULL

References cp_hashlist_append_by_option().

Referenced by cp_httpclient_fetch_ctl().

int cp_hashlist_callback ( cp_hashlist list,
int(*)(void *key, void *value, void *id)  cb,
void *  id 
)

iterates over the list and calls the callback function on each item.

Parameters:
list the hashlist
dk if not NULL, this function is invoked with the key for each entry
dv if not NULL, this function is invoked with the value for each entry

References COLLECTION_LOCK_READ.

cp_hashlist* cp_hashlist_create_by_mode ( int  mode,
unsigned long  size_hint,
cp_hashfunction  hash_fn,
cp_compare_fn  compare_fn 
)

constructor with parameters.

Parameters:
operation mode bitmap (see collection.h)
size_hint initial size of the cp_hashtable
hash_fn hash method
compare_fn hash compare method

References cp_hashlist_create_by_option().

cp_hashlist* cp_hashlist_create_by_option ( int  mode,
unsigned long  size_hint,
cp_hashfunction  hash_fn,
cp_compare_fn  compare_fn,
cp_copy_fn  copy_key,
cp_destructor_fn  free_key,
cp_copy_fn  copy_value,
cp_destructor_fn  free_value 
)

Constructor for copy mode.

Parameters:
mode Bitmap of operation mode (see collection.h)
size_hint initial size of the cp_hashtable
hash_fn hash method
compare_fn hash compare method
copy_key copy key method
copy_value copy value method

References COLLECTION_MODE_NOSYNC, and cp_hashtable_choose_size().

Referenced by cp_hashlist_create_by_mode().

cp_hashlist_iterator* cp_hashlist_create_iterator ( cp_hashlist list,
int  lock_mode 
)

Create a new iterator and initialize it at the beginning.

Parameters:
list list to iterate over
lock_mode locking mode to use
Returns:
new iterator object

References COLLECTION_LOCK_READ, and COLLECTION_LOCK_WRITE.

void cp_hashlist_destroy_by_option ( cp_hashlist list,
int  mode 
)

Destroy the object with the specified mode (override default).

Parameters:
list the object
mode locking mode
void cp_hashlist_destroy_custom ( cp_hashlist list,
cp_destructor_fn  dk,
cp_destructor_fn  dv 
)

This function does exactly what you would think it does.

Before it didn't - that was a bug. Now it does.

Referenced by cp_hashlist_destroy_deep(), and cp_httpclient_ctl_destroy().

void* cp_hashlist_entry_get_key ( cp_hashlist_entry *  entry  ) 

Get the key of the entry.

Note:
This function is fail-safe - works also on NULL entries. In any case you should check the result!
Return values:
key of the entry.
NULL if entry is NULL

Referenced by cp_hashlist_iterator_curr_key(), cp_hashlist_iterator_next_key(), and cp_hashlist_iterator_prev_key().

void* cp_hashlist_entry_get_value ( cp_hashlist_entry *  entry  ) 

Get the value of the entry.

Note:
This function is fail-safe - works also on NULL entries. In any case you should check the result!
Return values:
value of the entry.
NULL if entry is NULL

Referenced by cp_hashlist_iterator_curr_value(), cp_hashlist_iterator_next_value(), and cp_hashlist_iterator_prev_value().

void* cp_hashlist_insert ( cp_hashlist list,
void *  key,
void *  value 
)

Insert a new element (key, value) at the beginning of the list.

The operation is synchronized according to the properties of the object.

Precondition:
list != NULL
key != NULL
Returns:
value object

References cp_hashlist_insert_by_option().

void* cp_hashlist_insert_by_option ( cp_hashlist list,
void *  key,
void *  item,
int  mode 
)

Insert a new element (key, value) at the beginning of the list with mode.

Returns:
value object

References COLLECTION_LOCK_WRITE, COLLECTION_MODE_MULTIPLE_VALUES, and cp_hashlist_insert_internal().

Referenced by cp_hashlist_insert().

static cp_hashlist_entry * cp_hashlist_insert_internal ( cp_hashlist list,
cp_hashlist_entry *  entry 
) [static]

insert an entry

Parameters:
list the collection object
entry the entry to insert
Returns:
entry the inserted entry

References COLLECTION_MODE_NORESIZE.

Referenced by cp_hashlist_append_by_option(), cp_hashlist_insert_by_option(), cp_hashlist_iterator_append(), and cp_hashlist_iterator_insert().

int cp_hashlist_is_empty ( cp_hashlist list  ) 

Test if object is empty.

Return values:
true if no element contained.
false if at least one element is contained.

References cp_hashlist_item_count().

unsigned long cp_hashlist_item_count ( cp_hashlist  ) 

Get the number of elements in the collection.

Returns:
number of elements in the list.
Return values:
0 if list is NULL

References COLLECTION_LOCK_READ.

Referenced by cp_hashlist_is_empty(), and cp_httpclient_fetch_ctl_exec().

int cp_hashlist_iterator_head ( cp_hashlist_iterator iterator  ) 

initialize the iterator at the beginning

set the iterator at the beginning of the list and lock the list in the mode specified in type.

int cp_hashlist_iterator_init_tail ( cp_hashlist_iterator iterator,
cp_hashlist l,
int  lock_mode 
)

Initialize the Iterator at the end.

Set the iterator at the end of the list and lock the list in the mode specified in type.

References COLLECTION_LOCK_READ, and COLLECTION_LOCK_WRITE.

cp_hashlist_entry* cp_hashlist_iterator_next ( cp_hashlist_iterator iterator  ) 

Go to the next entry in the list and return the content.

Return values:
entry the next entry object.
NULL if reading beyond end or from empty list.

Referenced by cp_hashlist_iterator_next_key(), and cp_hashlist_iterator_next_value().

void* cp_hashlist_iterator_next_key ( cp_hashlist_iterator iterator  ) 

Go to the next entry in the list and return the key.

Returns:
object of the next entry.
Return values:
NULL if reading beyond end or from empty list.

References cp_hashlist_entry_get_key(), and cp_hashlist_iterator_next().

void* cp_hashlist_iterator_next_value ( cp_hashlist_iterator iterator  ) 

Go to the next entry in the list and return the content.

Returns:
object of the next entry.
Return values:
NULL if reading beyond end or from empty list.

References cp_hashlist_entry_get_value(), and cp_hashlist_iterator_next().

cp_hashlist_entry* cp_hashlist_iterator_prev ( cp_hashlist_iterator iterator  ) 

Go to the previous entry in the list and return the content.

Return values:
entry the previous entry object.
NULL if reading beyond beginning or from empty list.

Referenced by cp_hashlist_iterator_prev_key(), and cp_hashlist_iterator_prev_value().

void* cp_hashlist_iterator_prev_key ( cp_hashlist_iterator iterator  ) 

Go to the previous entry in the list and return the key.

Returns:
object of the previous entry.
Return values:
NULL if reading beyond beginning or from empty list.

References cp_hashlist_entry_get_key(), and cp_hashlist_iterator_prev().

void* cp_hashlist_iterator_prev_value ( cp_hashlist_iterator iterator  ) 

Go to the previous entry in the list and return the content.

Returns:
object of the previous entry.
Return values:
NULL if reading beyond beginning or from empty list.

References cp_hashlist_entry_get_value(), and cp_hashlist_iterator_prev().

int cp_hashlist_iterator_release ( cp_hashlist_iterator iterator  ) 

Unlock the list of the Iterator.

If the locking mode is COLLECTION_LOCK_NONE, do nothing.

References COLLECTION_LOCK_NONE.

Referenced by cp_hashlist_iterator_destroy().

int cp_hashlist_lock ( cp_hashlist list,
int  type 
)

Locks the collection with the specified mode.

This overrides the default mode stored in the object.

References COLLECTION_MODE_NOSYNC.

void* cp_hashlist_remove ( cp_hashlist list,
void *  key 
)

Removes the entry with matching key and destroys it (internal locking mode).

Parameters:
list the object
key Key which is searched.
Return values:
value that was stored in the entry.
NULL if key was not found

References cp_hashlist_remove_by_option().

Referenced by cp_thread_pool_set_available().

CPROPS_DLL void * cp_hashlist_remove_by_option ( cp_hashlist list,
void *  key,
int  mode 
)

Removes the entry with matching key and destroys it with locking mode.

Parameters:
list the object
key Key which is searched.
mode locking mode
Return values:
value that was stored in the entry.
NULL if key was not found

References COLLECTION_LOCK_WRITE, and cp_hashlist_remove_internal().

Referenced by cp_hashlist_remove().

static cp_hashlist_entry * cp_hashlist_remove_entry_internal ( cp_hashlist list,
cp_hashlist_entry *  entry 
) [static]

remove specified entry

Parameters:
list the collection object
entry the entry to remove
Return values:
entry if successful the passed entry
NULL if the entry was not found

References _cp_hashlist_entry::bucket.

Referenced by cp_hashlist_remove_head_by_option(), and cp_hashlist_remove_tail_by_option().

void* cp_hashlist_remove_head ( cp_hashlist list  ) 

Removes the first entry and destroys it.

Returns:
Element that was stored in the first entry.

References cp_hashlist_remove_head_by_option().

void* cp_hashlist_remove_head_by_option ( cp_hashlist list,
int  mode 
)

Removes the first entry and destroys it.

Parameters:
list the object
mode locking mode
Returns:
Element that was stored in the first entry.

References COLLECTION_LOCK_WRITE, and cp_hashlist_remove_entry_internal().

Referenced by cp_hashlist_remove_head().

static cp_hashlist_entry * cp_hashlist_remove_internal ( cp_hashlist list,
void *  key 
) [static]

remove first entry matching key

Parameters:
list the collection object
key that is searched for
Return values:
entry if successful the removed entry
NULL if the entry was not found

References COLLECTION_MODE_NORESIZE.

Referenced by cp_hashlist_iterator_remove(), and cp_hashlist_remove_by_option().

void* cp_hashlist_remove_tail ( cp_hashlist list  ) 

Removes the last entry and destroys it.

Precondition:
list != NULL
Return values:
Element that was stored in the first entry if not empty.
NULL if collection is empty

References cp_hashlist_remove_tail_by_option().

void* cp_hashlist_remove_tail_by_option ( cp_hashlist list,
int  mode 
)

Removes the last entry and destroys it.

Parameters:
list the object
mode locking mode
Returns:
Element that was stored in the first entry.

References COLLECTION_LOCK_WRITE, and cp_hashlist_remove_entry_internal().

Referenced by cp_hashlist_remove_tail().