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...
 

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 uint64_t tommy_size_t
 Generic size_t type. More...
 
typedef int64_t tommy_ssize_t
 Generic ssize_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_size_t tommy_key_t
 Type used in indexed data structures to store the key of an object. More...
 
typedef tommy_size_t tommy_hash_t
 Type used in hashtables to store the hash of an object. 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_ilog2_u64 (tommy_uint64_t value)
 Bit scan reverse or integer log2 for 64 bits. More...
 
tommy_uint_t tommy_ctz_u32 (tommy_uint32_t value)
 Bit scan forward or trailing zero count. More...
 
tommy_uint_t tommy_ctz_u64 (tommy_uint64_t value)
 Bit scan forward or trailing zero count for 64 bits. More...
 
tommy_uint32_t tommy_roundup_pow2_u32 (tommy_uint32_t value)
 Rounds up to the next power of 2. More...
 
tommy_uint64_t tommy_roundup_pow2_u64 (tommy_uint64_t value)
 Rounds up to the next power of 2 for 64 bits. 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

◆ tommy_malloc

#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().

Typedef Documentation

◆ tommy_uint32_t

typedef uint32_t tommy_uint32_t

Generic uint32_t type.

◆ tommy_uint64_t

typedef uint64_t tommy_uint64_t

Generic uint64_t type.

◆ tommy_uintptr_t

typedef uintptr_t tommy_uintptr_t

Generic uintptr_t type.

◆ tommy_size_t

typedef uint64_t tommy_size_t

Generic size_t type.

◆ tommy_ssize_t

typedef int64_t tommy_ssize_t

Generic ssize_t type.

◆ tommy_ptrdiff_t

typedef ptrdiff_t tommy_ptrdiff_t

Generic ptrdiff_t type.

◆ tommy_bool_t

typedef int tommy_bool_t

Generic boolean type.

◆ tommy_uint_t

Generic unsigned integer type.

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

◆ tommy_key_t

Type used in indexed data structures to store the key of an object.

◆ tommy_hash_t

Type used in hashtables to store the hash of an object.

◆ tommy_node

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
};
Data structure node.
Definition: tommytypes.h:211

◆ tommy_compare_func

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 if equal, >0 if greater.

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);
TOMMY_API void tommy_list_sort(tommy_list *list, tommy_compare_func *cmp)
Sorts a list.

◆ tommy_search_func

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
}
tommy_uint32_t tommy_inthash_u32(tommy_uint32_t key)
Integer reversible hash function for 32 bits.
Definition: tommyhash.h:103
void * tommy_hashtable_search(tommy_hashtable *hashtable, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash)
Searches an element in the hashtable.
Definition: tommyhashtbl.h:207

◆ tommy_foreach_func

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.

void tommy_list_foreach(tommy_list *list, tommy_foreach_func *func)
Calls the specified function for each element in the list.
Definition: tommylist.h:358
void tommy_foreach_func(void *obj)
Foreach function.
Definition: tommytypes.h:318

◆ tommy_foreach_arg_func

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_ilog2_u32()

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_ilog2_u64()

tommy_uint_t tommy_ilog2_u64 ( tommy_uint64_t  value)

Bit scan reverse or integer log2 for 64 bits.

◆ tommy_ctz_u32()

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_ctz_u64()

tommy_uint_t tommy_ctz_u64 ( tommy_uint64_t  value)

Bit scan forward or trailing zero count for 64 bits.

◆ tommy_roundup_pow2_u32()

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.

◆ tommy_roundup_pow2_u64()

tommy_uint64_t tommy_roundup_pow2_u64 ( tommy_uint64_t  value)

Rounds up to the next power of 2 for 64 bits.

◆ tommy_haszero_u32()

int tommy_haszero_u32 ( tommy_uint32_t  value)

Check if the specified word has a byte at 0.

Returns
0 or 1.