Gears-C/src/ds.h
2025-04-18 21:06:13 +10:00

97 lines
2.4 KiB
C

#pragma once
// ::DataStructures::Macros::Header::
#define NodeDir(node) (node == node->parent->left ? RB_LEFT : RB_RIGHT)
// ::DataStructures::RedBlackTree::Header::
typedef enum RBNodeColor_e
{
RB_RED,
RB_BLACK,
} RBNodeColor;
typedef enum RBNodeDir_e
{
RB_LEFT,
RB_RIGHT,
} RBNodeDir;
typedef struct RBNode
{
union
{
struct
{
struct RBNode *left;
struct RBNode *right;
};
struct RBNode *child[2];
};
struct RBNode *parent;
i32 value;
rawptr data;
RBNodeColor color;
} RBNode;
typedef struct RBTree
{
RBNode *root;
RBNode *nil;
} RBTree;
static void CreateRBTree (RBTree *tree);
static void RBTreeInsert (RBTree *tree, RBNode *node);
static b32 RBTreeSearch (RBTree *tree, i32 value, RBNode **node);
static void RBTreeDelete (RBTree *tree, i32 value);
static void RBTreeLeftRotate (RBTree *tree, RBNode *node);
static void RBTreeRightRotate(RBTree *tree, RBNode *node);
static void RBTreeRotate (RBTree *tree, RBNode *node, RBNodeDir dir);
static void RBTreeCorrect (RBTree *tree, RBNode *node);
static void RBTreeTransplant (RBTree *tree, RBNode *node, RBNode *placed_node);
// ::DataStructures::HashTable::Functions::Header::
typedef struct KeyValuePair
{
union
{
u64 key_u64;
};
union
{
Str8 value_string;
rawptr value_rawptr;
u32 value_u32;
u64 value_u64;
};
} KeyValuePair;
typedef struct HashNode
{
struct HashNode *next;
KeyValuePair v;
} HashNode;
typedef struct HashList
{
HashNode *first;
HashNode *last;
} HashList;
typedef struct HashTable
{
HashElement *lists;
u32 elem_count;
} HashTable;
static void InitHashTable(HashTable *table, u32 init_size);
static void HashTableClear(HashTable *table);
static HashNode *HashTablePush(HashTable *table, u64 hash, KeyValuePair value);
static HashNode *HashTablePushU32(HashTable *table, u64 key, u32 value);
static HashNode *HashTablePushU64(HashTable *table, u64 key, u64 value);
static HashNode *HashTablePushStr8(HashTable *table, u64 key, Str8 value);
static HashNode *HashTablePushRawptr(HashTable *table, u64 key, rawptr value);