Data Structures | Macros | Typedefs | Functions
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...
 

Macros

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

Typedefs

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

Functions

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

Detailed Description

Generic types.

Macro Definition 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:

1 struct object {
2  tommy_node node;
3  // other fields
4 };
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().

1 struct object {
2  tommy_node node;
3  int value;
4 };
5 
6 int compare(const void* obj_a, const void* obj_b)
7 {
8  if (((const struct object*)obj_a)->value < ((const struct object*)obj_b)->value)
9  return -1;
10  if (((const struct object*)obj_a)->value > ((const struct object*)obj_b)->value)
11  return 1;
12  return 0;
13 }
14 
15 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.

1 struct object {
2  tommy_node node;
3  int value;
4 };
5 
6 int compare(const void* arg, const void* obj)
7 {
8  const int* value_to_find = arg;
9  const struct object* object_to_compare = obj;
10 
11  return *value_to_find != object_to_compare->value;
12 }
13 
14 int value_to_find = 1;
15 struct object* obj = tommy_hashtable_search(&hashtable, compare, &value_to_find, tommy_inthash_u32(value_to_find));
16 if (!obj) {
17  // not found
18 } else {
19  // found
20 }
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.

1 tommy_list_foreach(&list, (tommy_foreach_func*)free);
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.