Data Structures | Defines | Typedefs | Functions

tommyds/tommytypes.h File Reference

Generic types. More...

Go to the source code of this file.

Data Structures

struct  tommy_node_struct
 Data structure node. More...

Defines

#define tommy_malloc   malloc
 Generic malloc(), calloc(), realloc() and free() functions.
#define TOMMY_KEY_BIT   (sizeof(tommy_key_t) * 8)
 Bits into the tommy_key_t type.

Typedefs

typedef uint32_t tommy_uint32_t
 Generic uint32_t type.
typedef uint64_t tommy_uint64_t
 Generic uint64_t type.
typedef uintptr_t tommy_uintptr_t
 Generic uintptr_t type.
typedef size_t tommy_size_t
 Generic size_t type.
typedef ptrdiff_t tommy_ptrdiff_t
 Generic ptrdiff_t type.
typedef int tommy_bool_t
 Generic boolean type.
typedef tommy_uint32_t tommy_uint_t
 Generic unsigned integer type.
typedef tommy_uint32_t tommy_count_t
 Generic unsigned integer for counting objects.
typedef tommy_uint32_t tommy_key_t
 Key type used in indexed data structures to store the key or the hash value.
typedef struct tommy_node_struct tommy_node
 Data structure node.
typedef int tommy_compare_func (const void *obj_a, const void *obj_b)
 Compare function for elements.
typedef int tommy_search_func (const void *arg, const void *obj)
 Search function for elements.
typedef void tommy_foreach_func (void *obj)
 Foreach function.
typedef void tommy_foreach_arg_func (void *arg, void *obj)
 Foreach function with an argument.

Functions

tommy_uint_t tommy_ilog2_u32 (tommy_uint32_t value)
 Bit scan reverse or integer log2.
tommy_uint_t tommy_ctz_u32 (tommy_uint32_t value)
 Bit scan forward or trailing zero count.
tommy_uint32_t tommy_roundup_pow2_u32 (tommy_uint32_t value)
 Rounds up to the next power of 2.
int tommy_haszero_u32 (tommy_uint32_t value)
 Check if the specified word has a byte at 0.

Detailed Description

Generic types.


Define Documentation

#define tommy_malloc   malloc

Generic malloc(), calloc(), realloc() and free() functions.

Redefine them to what you need. By default they map to the C malloc(), calloc(), realloc() and free().

#define TOMMY_KEY_BIT   (sizeof(tommy_key_t) * 8)

Bits into the tommy_key_t type.


Typedef Documentation

typedef uint32_t tommy_uint32_t

Generic uint32_t type.

typedef uint64_t tommy_uint64_t

Generic uint64_t type.

typedef uintptr_t tommy_uintptr_t

Generic uintptr_t type.

typedef size_t tommy_size_t

Generic size_t type.

typedef ptrdiff_t tommy_ptrdiff_t

Generic ptrdiff_t type.

typedef int tommy_bool_t

Generic boolean type.

Generic unsigned integer type.

It has no specific size, as is used to store only small values. To make the code more efficient, a full 32 bit integer is used.

Generic unsigned integer for counting objects.

TommyDS doesn't support more than 2^32-1 objects.

Key type used in indexed data structures to store the key or the hash value.

typedef struct tommy_node_struct tommy_node

Data structure node.

This node type is shared between all the data structures and used to store some info directly into the objects you want to store.

A typical declaration is:

 struct object {
     tommy_node node;
     // other fields
 };
typedef int tommy_compare_func(const void *obj_a, const void *obj_b)

Compare function for elements.

Parameters:
obj_aPointer to the first object to compare.
obj_bPointer to the second object to compare.
Returns:
<0 if the first element is less than the second, ==0 equal, >0 if greather.

This function is like the C strcmp().

 struct object {
     tommy_node node;
     int value;
 };

 int compare(const void* obj_a, const void* obj_b)
 {
     if (((const struct object*)obj_a)->value < ((const struct object*)obj_b)->value)
         return -1;
     if (((const struct object*)obj_a)->value > ((const struct object*)obj_b)->value)
         return 1;
     return 0;
 }

 tommy_list_sort(&list, compare);
typedef int tommy_search_func(const void *arg, const void *obj)

Search function for elements.

Parameters:
argPointer to the value to search as passed at the search function.
objPointer to the object to compare to.
Returns:
==0 if the value matches the element. !=0 if different.

The first argument is a pointer to the value to search exactly as it's passed at the search function called. The second argument is a pointer to the object inside the hashtable to compare.

The return value has to be 0 if the values are equal. != 0 if they are different.

 struct object {
     tommy_node node;
     int value;
 };

 int compare(const void* arg, const void* obj)
 {
     const int* value_to_find = arg;
     const struct object* object_to_compare = obj;

     return *value_to_find != object_to_compare->value;
 }

 int value_to_find = 1;
 struct object* obj = tommy_hashtable_search(&hashtable, compare, &value_to_find, tommy_inthash_u32(value_to_find));
 if (!obj) {
     // not found
 } else {
     // found
 }
typedef void tommy_foreach_func(void *obj)

Foreach function.

Parameters:
objPointer to the object to iterate.

A typical example is to use free() to deallocate all the objects in a list.

typedef void tommy_foreach_arg_func(void *arg, void *obj)

Foreach function with an argument.

Parameters:
argPointer to a generic argument.
objPointer to the object to iterate.

Function Documentation

tommy_uint_t tommy_ilog2_u32 ( tommy_uint32_t  value)

Bit scan reverse or integer log2.

Return the bit index of the most significant 1 bit.

If no bit is set, the result is undefined. To force a return 0 in this case, you can use tommy_ilog2_u32(value | 1).

Other interesting ways for bitscan are at:

Bit Twiddling Hacks http://graphics.stanford.edu/~seander/bithacks.html

Chess Programming BitScan http://chessprogramming.wikispaces.com/BitScan

Parameters:
valueValue to scan. 0 is not allowed.
Returns:
The index of the most significant bit set.
tommy_uint_t tommy_ctz_u32 ( tommy_uint32_t  value)

Bit scan forward or trailing zero count.

Return the bit index of the least significant 1 bit.

If no bit is set, the result is undefined.

Parameters:
valueValue to scan. 0 is not allowed.
Returns:
The index of the least significant bit set.
tommy_uint32_t tommy_roundup_pow2_u32 ( tommy_uint32_t  value)

Rounds up to the next power of 2.

For the value 0, the result is undefined.

Returns:
The smallest power of 2 not less than the specified value.
int tommy_haszero_u32 ( tommy_uint32_t  value)

Check if the specified word has a byte at 0.

Returns:
0 or 1.