libUnihan  0.5.3
Data Structures | Macros | Typedefs | Enumerations | Functions
collection.h File Reference

Functions for collection data structures such as set and array . More...

#include <glib.h>

Go to the source code of this file.

Data Structures

struct  HashSet
 HashSet: A set which backed by Hash Table. More...
 

Macros

#define G_PTR_ARRAY_REMOVE_ALL(array)   if (array->len>0) g_ptr_array_remove_range(array,0,array->len)
 Remove all elements of a GPTRArray. More...
 
#define G_ARRAY_CONCAT(dest, src)   g_array_append_vals(dest,src->data,src->len)
 Concatenate two GArrays. More...
 
#define G_ARRAY_REMOVE_ALL(array)   if (array->len>0) g_array_remove_range(array,0,array->len)
 Remove all elements of a GArray. More...
 

Typedefs

typedef gboolean(* ForeachCallbackFunc )(gpointer data, gpointer userdata)
 Foreach callback function prototype. More...
 

Enumerations

enum  ElementType { ELEMENTS_TYPE_INTEGER, ELEMENTS_TYPE_STRING, ELEMENTS_TYPE_POINTER, ELEMENTS_TYPE_CUSTOM }
 Enumeration of element types which the collection stores. More...
 

Functions

HashSethashSet_new_default (ElementType type)
 New a HashSet instance with default handling functions. More...
 
HashSethashSet_new (ElementType type, GHashFunc hash_func, GEqualFunc element_equal_func)
 New a HashSet instance. More...
 
HashSethashSet_new_full (ElementType type, GHashFunc hash_func, GEqualFunc element_equal_func, GDestroyNotify element_destroy_func)
 New a HashSet instance, while specifying element destroy function. More...
 
void hashSet_copy (HashSet *dest, HashSet *src)
 Copy from another HashSet. More...
 
guint hashSet_get_size (HashSet *hashSet)
 Get the number of elements in the HashSet. More...
 
gboolean hashSet_has_element (HashSet *hashSet, gconstpointer element)
 Whether a element is in the HashSet. More...
 
gboolean hashSet_add_element (HashSet *hashSet, gpointer element)
 Add an element to the HashSet. More...
 
void hashSet_remove_all (HashSet *hashSet)
 Remove all elements in the set. More...
 
gboolean hashSet_remove_element (HashSet *hashSet, gconstpointer element)
 Remove an element to the HashSet. More...
 
void hashSet_steal_all (HashSet *hashSet)
 Steal all elements in the set. More...
 
gboolean hashSet_steal_element (HashSet *hashSet, gconstpointer element)
 Steal an element to the HashSet. More...
 
char * hashSet_to_string (HashSet *hashSet)
 New a string representation for the content of the HashSet. More...
 
void hashSet_destroy (HashSet *hashSet)
 Free the hashSet instance. More...
 
int integer_compareFunc (gconstpointer a, gconstpointer b)
 GCompareFunc that compares two intergers. More...
 
int g_array_find (GArray *array, gpointer element, gint elementSize, GCompareFunc func)
 Find an element in a GArray. More...
 
GArray * g_array_copy (GArray *dest, GArray *src)
 Copy from another GArray. More...
 
Set operations.

Set operation functions such as union and interset.

void hashSet_union (HashSet *result, HashSet *hashSet1, HashSet *hashSet2)
 Union two sets. More...
 
void hashSet_intersect (HashSet *result, HashSet *hashSet1, HashSet *hashSet2)
 Intersect two sets. More...
 

Detailed Description

This header file lists functions for collection data structures such as set and array.

Macro Definition Documentation

#define G_ARRAY_CONCAT (   dest,
  src 
)    g_array_append_vals(dest,src->data,src->len)

This macro appends GArray src to dest . The results is stored in dest.

Parameters
destThe GArray to be append to.
srcThe GArray to be appended from.
Returns
The GArray dest.
#define G_ARRAY_REMOVE_ALL (   array)    if (array->len>0) g_array_remove_range(array,0,array->len)

This macro removes all elements in array.

Parameters
arrayGArray to be processed.
#define G_PTR_ARRAY_REMOVE_ALL (   array)    if (array->len>0) g_ptr_array_remove_range(array,0,array->len)

This macro removes all elements in array.

Parameters
arrayGPtrArray to be processed.

Typedef Documentation

typedef gboolean(* ForeachCallbackFunc)(gpointer data, gpointer userdata)
Parameters
dataThe collection to be process.
userdataThe other options for the callback function. Can be NULL.
Returns
TRUE to continue; FALSE to break.

A prototype for callback functions that used with foreach functions.

Enumeration Type Documentation

Specifying the element types of for collection has following benefits:

  1. Easier type checking
  2. Automatically assign the corresponding comparison functions.
See also
HashSet
Enumerator
ELEMENTS_TYPE_INTEGER 

Specify that collection elements are integers.

ELEMENTS_TYPE_STRING 

Specify that collection elements are strings.

ELEMENTS_TYPE_POINTER 

Specify that collection elements are pointers.

ELEMENTS_TYPE_CUSTOM 

Specify that collection elements are user-defined type.

Function Documentation

GArray* g_array_copy ( GArray *  dest,
GArray *  src 
)

This function copies the elements from src to dest. The old elements in dest will be removed by G_ARRAY_REMOVE_ALL(). Note that if src is NULL, content of dest will be freed.

Parameters
destGArray to be copied to.
srcGArray to be copied to.
Returns
The GArray dest.
int g_array_find ( GArray *  array,
gpointer  element,
gint  elementSize,
GCompareFunc  func 
)

This function uses func, A GCompareFunc to compare and find an element in array. The prototype of GCompareFunc is: int (*GCompareFunc) (gconstpointer a, gconstpointer b)

Note that it finds whether the content of element is in the array .

Parameters
arrayThe array to be processed.
elementThe element to be found.
elementSizeThe number of bytes which the content of element occupies.
funcThe GCompareFunc that compar
Returns
index of the element in the array; -1 if such element cannot be found.
gboolean hashSet_add_element ( HashSet hashSet,
gpointer  element 
)

Add an element to hashSet, if it does not yet have an identical element. Note that this function only copies the pointer of element, not the content. So try not free the space that the element points to until element is removed from hashSet.

Parameters
hashSetThe HashSet to be processed.
elementThe element to be added.
Returns
TRUE if the element has been added, FALSE if element has not been added because an identical element exists.
void hashSet_copy ( HashSet dest,
HashSet src 
)

This function copies the elements from src to dest. The old elements in dest will be removed by hashSet_remove_all().

Parameters
destThe HashSet to be copied to.
srcThe HashSet to be copied from.
void hashSet_destroy ( HashSet hashSet)
Parameters
hashSetThe HashSet to be processed.
guint hashSet_get_size ( HashSet hashSet)

This function gets the number of elements in the HashSet.

Parameters
hashSetThe HashSet to be processed.
Returns
Number of elements in the HashSet.
gboolean hashSet_has_element ( HashSet hashSet,
gconstpointer  element 
)

This function determines whether a element is in the HashSet.

Parameters
hashSetThe HashSet to be processed.
elementThe element to be found.
Returns
TRUE if element is in hashSet ; FALSE otherwise.
void hashSet_intersect ( HashSet result,
HashSet hashSet1,
HashSet hashSet2 
)

This function performs a set intersect operation on hashSet1 and hashSet2, and put the result set in result. The set result can be either hashSet1, hashSet2 or another HashSet, but cannot be NULL.

Parameters
hashSet1The HashSet to be processed.
hashSet2The HashSet to be processed.
resultThe HashSet that holds the result.
HashSet* hashSet_new ( ElementType  type,
GHashFunc  hash_func,
GEqualFunc  element_equal_func 
)

This function returns a new HashSet. It is actually hashSet_new_full(type,hash_func,element_equal_func,NULL).

Parameters
typeElementType.
hash_funcGHashFunc callback function for computing hash.
element_equal_funcGEqualFunc callback function for comparing two keys.
Returns
a new HashSet instance.
See also
hashSet_new_default()
hashSet_new_full()
HashSet* hashSet_new_default ( ElementType  type)

This function returns a new HashSet with default handling functions. Specifically:

ElementTypehash_funcelement_equal_func element_destroy_funcvalue_destroy_func
ELEMENTS_TYPE_INTEGERg_int_hash()g_int_equal() NULLNULL
ELEMENTS_TYPE_STRINGg_str_hash()g_str_equal() NULLNULL
ELEMENTS_TYPE_POINTERg_direct_hash()g_direct_equal() NULLNULL
ELEMENTS_TYPE_CUSTOMNULLNULL NULLNULL

Note that no default handler functions are associated with element type ELEMENTS_TYPE_CUSTOM, please use hashSet_new() or hashSet_new_full() instead.

Parameters
typeElementType.
Returns
a new HashSet instance.
See also
hashSet_new()
hashSet_new_full()
HashSet* hashSet_new_full ( ElementType  type,
GHashFunc  hash_func,
GEqualFunc  element_equal_func,
GDestroyNotify  element_destroy_func 
)

This function returns a new HashSet, while specifying element destroy function. It passes to GHashTable as g_hash_table_new_full(hash_func,element_equal_func,element_destroy_func,NULL).

Parameters
typeElementType.
hash_funcGHashFunc callback function for computing hash.
element_equal_funcGEqualFunc callback function for comparing two keys.
element_destroy_funcGGDestroyNotify callback function that free the element storage.
Returns
a new HashSet instance.
See also
hashSet_new_default()
hashSet_new()
void hashSet_remove_all ( HashSet hashSet)

This function removes all elements in hashSet. If element_destroy_func is supplied when s is created, it will be executed to free the elements.

Parameters
hashSetThe HashSet to be processed.
See also
hashSet_remove_element()
hashSet_steal_all()
hashSet_steal_element()
gboolean hashSet_remove_element ( HashSet hashSet,
gconstpointer  element 
)

Remove an element from hashSet. Note that the element_destroy_func will be executed if it was given when constructing hashSet, otherwise, only the pointer element will be removed, not the content.

Parameters
hashSetThe HashSet to be processed.
elementThe element to be removed.
Returns
TRUE if the element was found and removed. FALSE otherwise.
See also
hashSet_remove_all()
hashSet_steal_all()
hashSet_steal_element()
void hashSet_steal_all ( HashSet hashSet)

This function "steals" all elements from hashSet by removing all the element from it, but not free the content that the elements point to, in spite of whether the element_destroy_func is given or not.

Parameters
hashSetThe HashSet to be processed.
See also
hashSet_remove_all()
hashSet_remove_element()
hashSet_steal_element()
gboolean hashSet_steal_element ( HashSet hashSet,
gconstpointer  element 
)

This function "steals" an element from hashSet by removing element from it, but not free the content that element points to, in spite of whether the element_destroy_func is given or not.

Parameters
hashSetThe HashSet to be processed.
elementThe element to be removed.
Returns
TRUE if the element was found and removed. FALSE otherwise.
See also
hashSet_remove_all()
hashSet_remove_element()
hashSet_steal_all()
char* hashSet_to_string ( HashSet hashSet)

This function produces a string representation of the HashSet. The output formats are like (in printf() format):

  • ELEMENTS_TYPE_INTEGER: [ d d d ... ]
  • ELEMENTS_TYPE_STRING: [ s s s ... ]
  • ELEMENTS_TYPE_POINTER: [ p p p ... ]
  • ELEMENTS_TYPE_CUSTOM: [ p p p ... ]

The return string is new allocated, use free() or g_free() to free it.

Parameters
hashSetThe HashSet to be processed.
Returns
a newly allocated string which represents the content of result The HashSet that holds the result.
void hashSet_union ( HashSet result,
HashSet hashSet1,
HashSet hashSet2 
)

This function performs a set union operation on hashSet1 and hashSet2, and put the result set in result. The set result can be either hashSet1, hashSet2 or another HashSet, but cannot be NULL.

Parameters
hashSet1The HashSet to be processed.
hashSet2The HashSet to be processed.
resultThe HashSet that holds the result.
int integer_compareFunc ( gconstpointer  a,
gconstpointer  b 
)
Parameters
apointer to an integer.
bpointer to another integer.
Returns
negative value if value of a < b; zero if value of a = b; positive value if value of a > b.