queue.h
author orudge
Sun, 22 Aug 2004 15:31:23 +0000
changeset 109 96a76a02e425
parent 107 c72381c27546
child 110 a22a6b07904b
permissions -rw-r--r--
(svn r110) Added modified English town name generator
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     1
#ifndef QUEUE_H
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     2
#define QUEUE_H
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     3
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     4
//#define NOFREE
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     5
//#define QUEUE_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     6
//#define HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     7
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     8
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     9
typedef struct Queue Queue;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    10
typedef bool Queue_PushProc(Queue* q, void* item, int priority);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    11
typedef void* Queue_PopProc(Queue* q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    12
typedef bool Queue_DeleteProc(Queue* q, void* item, int priority);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    13
typedef void Queue_ClearProc(Queue* q, bool free_values);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    14
typedef void Queue_FreeProc(Queue* q, bool free_values);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    15
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    16
typedef struct InsSortNode InsSortNode;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    17
struct InsSortNode {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    18
	void* item;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    19
	int priority;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    20
	InsSortNode* next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    21
};
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    22
typedef struct BinaryHeapNode BinaryHeapNode;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    23
	struct BinaryHeapNode {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    24
	void* item;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    25
	int priority;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    26
};
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    27
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    28
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    29
struct Queue{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    30
	/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    31
	 * Pushes an element into the queue, at the appropriate place for the queue.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    32
	 * Requires the queue pointer to be of an appropriate type, of course.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    33
	 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    34
	Queue_PushProc* push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    35
	/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    36
	 * Pops the first element from the queue. What exactly is the first element,
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    37
	 * is defined by the exact type of queue.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    38
	 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    39
	Queue_PopProc* pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    40
	/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    41
	 * Deletes the item from the queue. priority should be specified if
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    42
	 * known, which speeds up the deleting for some queue's. Should be -1
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    43
	 * if not known.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    44
	 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    45
	Queue_DeleteProc* del;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    46
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    47
	/* Clears the queue, by removing all values from it. It's state is
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    48
	 * effectively reset. If free_items is true, each of the items cleared
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    49
	 * in this way are free()'d.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    50
	 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    51
	Queue_ClearProc* clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    52
	/* Frees the queue, by reclaiming all memory allocated by it. After
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    53
	 * this it is no longer usable. If free_items is true, any remaining
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    54
	 * items are free()'d too. 
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    55
	 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    56
	Queue_FreeProc* free;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    57
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    58
	union {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    59
		struct {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    60
			uint max_size;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    61
			uint size;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    62
			void** elements;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    63
		} stack;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    64
		struct {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    65
			uint max_size;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    66
			uint head; /* The index where the last element should be inserted */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    67
			uint tail; /* The index where the next element should be read */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    68
			void** elements;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    69
		} fifo;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    70
		struct {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    71
			InsSortNode* first;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    72
		} inssort;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    73
		struct {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    74
			uint max_size;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    75
			uint size;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    76
			uint blocks; /* The amount of blocks for which space is reserved in elements */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    77
			BinaryHeapNode** elements;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    78
		} binaryheap;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    79
	} data;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    80
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    81
	/* If true, this struct will be free'd when the
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    82
	 * Queue is deleted. */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    83
	bool freeq;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    84
};
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    85
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    86
/* Initializes a stack and allocates internal memory. */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    87
void init_Stack(Queue* q, uint max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    88
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    89
/* Allocate a new stack with a maximum of max_size elements. */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    90
Queue* new_Stack(uint max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    91
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    92
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    93
 * Fifo
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    94
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    95
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    96
/* Initializes a fifo and allocates internal memory for maximum of max_size
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    97
 * elements */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    98
void init_Fifo(Queue* q, uint max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    99
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   100
/* Allocate a new fifo and initializes it with a maximum of max_size elements. */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   101
Queue* new_Fifo(uint max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   102
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   103
Queue* new_Fifo_in_buffer(uint max_size, void* buffer);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   104
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   105
int build_Fifo(void* buffer, uint size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   106
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   107
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   108
 * Insertion Sorter
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   109
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   110
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   111
/* Initializes a inssort and allocates internal memory. There is no maximum
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   112
 * size */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   113
void init_InsSort(Queue* q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   114
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   115
/* Allocate a new fifo and initializes it. There is no maximum size */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   116
Queue* new_InsSort();
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   117
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   118
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   119
 *  Binary Heap
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   120
 *  For information, see:
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   121
 *   http://www.policyalmanac.org/games/binaryHeaps.htm
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   122
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   123
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   124
/* The amount of elements that will be malloc'd at a time */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   125
#define BINARY_HEAP_BLOCKSIZE_BITS 10
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   126
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   127
/* Initializes a binary heap and allocates internal memory for maximum of
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   128
 * max_size elements */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   129
void init_BinaryHeap(Queue* q, uint max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   130
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   131
/* Allocate a new binary heap and initializes it with a maximum of max_size
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   132
 * elements. */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   133
Queue* new_BinaryHeap(uint max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   134
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   135
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   136
 * Hash
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   137
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   138
typedef struct HashNode HashNode;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   139
struct HashNode {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   140
	uint key1;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   141
	uint key2;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   142
	void* value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   143
	HashNode* next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   144
};
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   145
/* Generates a hash code from the given key pair. You should make sure that
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   146
 * the resulting range is clearly defined.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   147
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   148
typedef uint Hash_HashProc(uint key1, uint key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   149
typedef struct Hash {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   150
	/* The hash function used */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   151
	Hash_HashProc* hash;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   152
	/* The amount of items in the hash */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   153
	uint size;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   154
	/* The number of buckets allocated */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   155
	uint num_buckets;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   156
	/* A pointer to an array of num_buckets buckets. */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   157
	HashNode* buckets;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   158
	/* A pointer to an array of numbuckets booleans, which will be true if
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   159
	 * there are any Nodes in the bucket */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   160
	bool* buckets_in_use;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   161
	/* If true, buckets will be freed in delete_hash */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   162
	bool freeb;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   163
	/* If true, the pointer to this struct will be freed in delete_hash */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   164
	bool freeh;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   165
} Hash;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   166
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   167
/* Call these function to manipulate a hash */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   168
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   169
/* Deletes the value with the specified key pair from the hash and returns
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   170
 * that value. Returns NULL when the value was not present. The value returned
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   171
 * is _not_ free()'d! */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   172
void* Hash_Delete(Hash* h, uint key1, uint key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   173
/* Sets the value associated with the given key pair to the given value.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   174
 * Returns the old value if the value was replaced, NULL when it was not yet present. */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   175
void* Hash_Set(Hash* h, uint key1, uint key2, void* value);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   176
/* Gets the value associated with the given key pair, or NULL when it is not
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   177
 * present. */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   178
void* Hash_Get(Hash* h, uint key1, uint key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   179
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   180
/* Call these function to create/destroy a hash */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   181
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   182
/* Builds a new hash, with num_buckets buckets. Make sure that hash() always
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   183
 * returns a hash less than num_buckets! Call delete_hash after use */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   184
Hash* new_Hash(Hash_HashProc* hash, int num_buckets);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   185
/* Builds a new hash in an existing struct. Make sure that hash() always
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   186
 * returns a hash less than num_buckets! Call delete_hash after use */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   187
void init_Hash(Hash* h, Hash_HashProc* hash, int num_buckets);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   188
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   189
 * Deletes the hash and cleans up. Only cleans up memory allocated by new_Hash
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   190
 * & friends. If free is true, it will call free() on all the values that
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   191
 * are left in the hash.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   192
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   193
void delete_Hash(Hash* h, bool free_values);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   194
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   195
 * Cleans the hash, but keeps the memory allocated
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   196
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   197
void clear_Hash(Hash* h, bool free_values);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   198
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   199
 * Gets the current size of the Hash
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   200
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   201
uint Hash_Size(Hash* h);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   202
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   203
#endif /* QUEUE_H */