Change file naming

This is a follow-up to previous commit.
This commit is contained in:
2025-02-02 21:40:31 +03:00
parent c89cf8f316
commit 586a9ace21
23 changed files with 41 additions and 40 deletions

126
include/BH/Algo.h Executable file
View File

@@ -0,0 +1,126 @@
#ifndef BH_ALGO_H
#define BH_ALGO_H
#include "Common.h"
/**
* Exchanges the values of two elements.
*
* \param dest Pointer to the element
* \param src Pointer to the element
* \param size Element size in bytes
*/
void BH_Swap(void *dest,
void *src,
size_t size);
/**
* Partitions the array relative to the specified pivot element.
*
* The pivot element can be part of the partitioned array.
*
* \param pivot Pointer to the pivot element
* \param array Pointer to the array
* \param size Array size
* \param element Element size in bytes
* \param equal Comparision function
*
* \return Pointer to the first element of the second partition.
*/
void *BH_Partition(void *pivot,
void *array,
size_t size,
size_t element,
BH_EqualCallback equal);
/**
* Sorts the array.
*
* \param array Pointer to the array
* \param size Array size
* \param element Element size in bytes
* \param equal Comparision function
*/
void BH_Sort(void *array,
size_t size,
size_t element,
BH_EqualCallback equal);
/**
* Makes heap in an array from the array.
*
* \param array Pointer to the array
* \param size Array size
* \param element Element size in bytes
* \param equal Comparision function
*/
void BH_HeapMake(void *array,
size_t size,
size_t element,
BH_EqualCallback equal);
/**
* Removes top/first element from a heap in an array.
*
* \param array Pointer to the array
* \param size Array size
* \param element Element size in bytes
* \param equal Comparasion function
*/
void BH_HeapRemove(void *array,
size_t size,
size_t element,
BH_EqualCallback equal);
/**
* Inserts new element into heap in an array.
*
* If the pointer to value is NULL, this function assumes that the new value
* is at the end of the array.
*
* \param value Pointer to the value
* \param array Pointer to the array
* \param size Array size
* \param element Element size in bytes
* \param equal Comparasion function
*/
void BH_HeapInsert(void *value,
void *array,
size_t size,
size_t element,
BH_EqualCallback equal);
/**
* Removes top/first element and inserts new element into heap in an array.
*
* If the pointer to value is NULL, this function assumes that the new value
* is at the end of the array.
*
* This function is roughly equivalent to the following code:
* \code
* BH_HeapRemove(array, size, element, equal);
* BH_HeapInsert(value, array, size - 1, element, equal);
* \endcode
*
* \param value Pointer to the value
* \param array Pointer to the array
* \param size Array size
* \param element Element size in bytes
* \param equal Comparasion function
*/
void BH_HeapReplace(void *value,
void *array,
size_t size,
size_t element,
BH_EqualCallback equal);
#endif /* BH_ALGO_H */

25
include/BH/Common.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef BH_COMMON_H
#define BH_COMMON_H
#include <stddef.h>
#include <stdint.h>
#define BH_OK 0x0000
#define BH_ERROR 0x0001
#define BH_NOIMPL 0x0002
#define BH_OOM 0x0003
#define BH_INVALID 0x0004
#define BH_FOUND 0x0005
#define BH_NOTFOUND 0x0006
#define BH_TIMEOUT 0x0007
#define BH_UNUSED(x) (void)(x)
#define BH_PTR2INT(x) ((intptr_t)(x))
#define BH_INT2PTR(x) ((void*)(x))
typedef int (*BH_EqualCallback)(const void *, const void *);
typedef size_t (*BH_HashCallback)(const void *);
#endif /* BH_COMMON_H */

223
include/BH/Hashmap.h Executable file
View File

@@ -0,0 +1,223 @@
#ifndef BH_HASHMAP_H
#define BH_HASHMAP_H
#include "Common.h"
typedef struct BH_Hashmap BH_Hashmap;
/**
* Creates the new hashmap handle.
*
* \param equal Comparision function
* \param hash Key hash function
*
* \return On success, returns hashmap handle.
* \return On failure, returns a null pointer.
*/
BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal,
BH_HashCallback hash);
/**
* Destroys the hashmap.
*
* \param hashmap Hashmap handle
*/
void BH_HashmapFree(BH_Hashmap *hashmap);
/**
* Clears the hashmap.
*
* \param hashmap Hashmap handle
*/
void BH_HashmapClear(BH_Hashmap *hashmap);
/**
* Reserves space in the hashmap.
*
* This function can both expand and shrink the available space in hashmap.
* This function takes into account current hashmap load factor.
*
* \param hashmap Hahsmap handle
* \param size Capacity
*
* \note Calling this function will invalidate iterators.
* \note Actual hashmap capacity can be bigger then requested.
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_HashmapReserve(BH_Hashmap *hashmap,
size_t size);
/**
* Inserts the pair of key-value into the hashmap.
*
* \param hashmap Hashmap handle
* \param key Key
* \param value Value
*
* \note This function allows duplicates to be inserted.
* \note Calling this function will invalidate iterators.
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_HashmapInsert(BH_Hashmap *hashmap,
void *key,
void *value);
/**
* Removes value from the hashmap.
*
* \param hashmap Hashmap handle
* \param key Key
*
* \note Calling this function will invalidate iterators.
* \note If hashmap contains several elements with the same key, this function
* will remove only one key-value pair.
*/
void BH_HashmapRemove(BH_Hashmap *hashmap,
void *key);
/**
* Returns value from the hashmap by key.
*
* \param hashmap Hashmap handle
* \param key Key
* \param value Value (optional)
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_HashmapAt(BH_Hashmap *hashmap,
void *key,
void **value);
/**
* Checks if the hashmap is empty.
*
* \param hashmap Hashmap handle
*
* \return If hashmap is empty, returns non-zero value
* \return If hashmap is not empty, returns zero value
*/
int BH_HashmapEmpty(BH_Hashmap *hashmap);
/**
* Returns the size of the hashmap.
*
* \param hashmap Hashmap handle
*
* \return Returns the size of the hashmap.
*/
size_t BH_HashmapSize(BH_Hashmap *hashmap);
/**
* Returns the capacity of the hashmap.
*
* \param hashmap Hashmap handle
*
* \return Returns the capacity of the hashmap.
*/
size_t BH_HashmapCapacity(BH_Hashmap *hashmap);
/**
* Returns the load factor of the hashmap.
*
* \param hashmap Hashmap handle
*
* \return Returns the load factor of the hashmap.
*/
float BH_HashmapFactor(BH_Hashmap *hashmap);
/**
* Sets the load factor of the hashmap.
*
* \param hashmap Hashmap handle
* \param factor Load factor
*
* \note New load factor will be applied on the next reserve/insert operation.
*/
void BH_HashmapSetFactor(BH_Hashmap *hashmap,
float factor);
/**
* Returns the iterator to the element in the hashmap with specified key.
*
* \param hashmap Hashmap handle
* \param iter Iterator
*
* \return On success, returns iterator value.
* \return On failure, returns NULL pointer.
*
* \note If hashmap contains several elements with the same key, this function
* will return iterator to one of them
*/
void *BH_HashmapIterAt(BH_Hashmap *hashmap,
void *key);
/**
* Returns the iterator to the next element in the hashmap.
*
* If the iterator is NULL pointer, this function will return iterator to the
* first element of the hashmap.
*
* \param hashmap Hashmap handle
* \param iter Iterator
*
* \return On success, returns new iterator value for the next element.
* \return On failure, returns NULL pointer.
*/
void *BH_HashmapIterNext(BH_Hashmap *hashmap,
void *iter);
/**
* Removes value from the hashmap pointed by by iterator.
*
* \param hashmap Hashmap handle
* \param iter Iterator
*
* \note Calling this function will invalidate iterators.
*/
void BH_HashmapIterRemove(BH_Hashmap *hashmap,
void *iter);
/**
* Returns key, pointed by the iterator.
*
* \param iter Iterator
*
* \return Returns key.
*/
void *BH_HashmapIterKey(void *iter);
/**
* Returns value, pointed by the iterator.
*
* \param iter Iterator
*
* \return Returns value.
*/
void *BH_HashmapIterValue(void *iter);
#endif /* BH_HASHMAP_H */

252
include/BH/IO.h Normal file
View File

@@ -0,0 +1,252 @@
#ifndef BH_IO_H
#define BH_IO_H
#include "Common.h"
#define BH_IO_INFO_CB 0x0000
#define BH_IO_INIT_CB 0x0001
#define BH_IO_DESTROY_CB 0x0002
#define BH_IO_OPEN_CB 0x0003
#define BH_IO_CLOSE_CB 0x0004
#define BH_IO_READ_CB 0x0005
#define BH_IO_WRITE_CB 0x0006
#define BH_IO_PEEK_CB 0x0007
#define BH_IO_TELL_CB 0x0008
#define BH_IO_SEEK_CB 0x0009
#define BH_IO_FLUSH_CB 0x000A
#define BH_IO_SIZE_CB 0x000B
#define BH_IO_FLAGS_CB 0x000C
#define BH_IO_CLEAR_CB 0x000D
#define BH_IO_READ 0x0001
#define BH_IO_WRITE 0x0002
#define BH_IO_READWRITE 0x0003
#define BH_IO_APPEND 0x0010
#define BH_IO_TRUNCATE 0x0020
#define BH_IO_CREATE 0x0040
#define BH_IO_EXIST 0x0080
#define BH_IO_SEEK_SET 0x0000
#define BH_IO_SEEK_CUR 0x0001
#define BH_IO_SEEK_END 0x0002
#define BH_IO_FLAG_OK 0x0000
#define BH_IO_FLAG_ERROR 0x0001
#define BH_IO_FLAG_EOF 0x0002
#define BH_IO_FLAG_OPEN 0x0004
#define BH_FILE_CLASSNAME "BH_File"
typedef struct BH_IO BH_IO;
typedef int (*BH_IOCallback)(void *, int ,void *, void *);
/**
* Creates the IO handle that represents file.
*
* \param path Path to the file
*
* \return On success, returns IO handle.
* \return On failure, returns NULL pointer.
*/
BH_IO *BH_FileNew(const char *path);
/**
* Creates the IO handle that represents buffered IO.
*
* \param io IO handle
*
* \return On success, returns IO handle.
* \return On failure, returns NULL pointer.
*/
BH_IO *BH_BufferNew(BH_IO *io);
/**
* Creates the IO handle with specified handler and context.
*
* \param func IO actions handler
* \param data Initialization data
*
* \return On success, returns IO handle.
* \return On failure, returns NULL pointer.
*/
BH_IO *BH_IONew(BH_IOCallback cb,
void *data);
/**
* Destroys the IO.
*
* \param io IO handle
*/
void BH_IOFree(BH_IO *io);
/**
* Returns the IO instance classname.
*
* \param io IO handle
*
* \return On success, returns pointer to constant string.
* \return On failure, returns NULL pointer
*/
const char *BH_IOClassname(BH_IO* io);
/**
* Opens the IO in specified mode of operation.
*
* \param io IO handle
* \param mode Mode of operation
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOOpen(BH_IO *io,
int mode);
/**
* Closes the IO.
*
* \param io IO handle
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOClose(BH_IO *io);
/**
* Reads up to specified amount of bytes from the IO into memory buffer.
*
* \param io IO handle
* \param buffer Pointer to the buffer
* \param size Bytes to read
* \param actual Bytes read
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IORead(BH_IO *io,
char *buffer,
size_t size,
size_t *actual);
/**
* Writes up to specified amount of bytes into the IO from the memory buffer.
*
* \param io IO handle
* \param buffer Pointer to the buffer
* \param size Bytes to write
* \param actual Bytes written
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOWrite(BH_IO *io,
const char *buffer,
size_t size,
size_t *actual);
/**
* Peeks up to specified amount of bytes from the IO handle.
*
* \param io IO handle
* \param buffer Pointer to the buffer
* \param size Bytes to peek
* \param actial Bytes peeked
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOPeek(BH_IO *io,
char *buffer,
size_t size,
size_t *actual);
/**
* Tells current offset in the IO.
*
* \param io IO handle
* \param position Offset
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOTell(BH_IO *io,
int64_t *position);
/**
* Seeks to specified offset in the IO.
*
* \param io IO handle
* \param position Offset
* \param direction Direction
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOSeek(BH_IO *io,
int64_t position,
int direction);
/**
* Flushes the internal buffers of the IO.
*
* \param io IO handle
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOFlush(BH_IO *io);
/**
* Returns the size of the IO (either total or available size).
*
* \param io IO handle
* \param size Available/total size
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOSize(BH_IO *io,
int64_t *size);
/**
* Returns flags of the IO.
*
* \param io IO handle
*
* \return Flags of the IO
*/
int BH_IOFlags(BH_IO *io);
/**
* Clears errors of the IO.
*
* \param io IO handle
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_IOClear(BH_IO *io);
#endif /* BH_IO_H */

1400
include/BH/Math.h Normal file

File diff suppressed because it is too large Load Diff

149
include/BH/Queue.h Executable file
View File

@@ -0,0 +1,149 @@
#ifndef BH_QUEUE_H
#define BH_QUEUE_H
#include "Common.h"
typedef struct BH_Queue BH_Queue;
/**
* Creates the new queue object.
*
* \return On success, returns the pointer to the new queue object.
* \return On failure, returns a null pointer.
*/
BH_Queue *BH_QueueNew(void);
/**
* Frees the \a queue object.
*
* \param queue Pointer to the queue object to be freed
*/
void BH_QueueFree(BH_Queue *queue);
/**
* Clears the \a queue object.
*
* \param queue Pointer to the queue object to be cleared
*/
void BH_QueueClear(BH_Queue *queue);
/**
* Reserves the space for \a size elements in the \a queue.
*
* This function can both expand and shrink the available space in \a queue.
*
* \param queue Pointer to the queue object to be resized in terms of capacity
* \param size New capacity of the queue
*
* \note Calling this function will invalidate iterators.
* \note Actual hashmap capacity can be bigger then requested.
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_QueueReserve(BH_Queue *queue,
size_t size);
/**
* Inserts the \a value into the \a queue.
*
* \param queue Pointer to the queue object
* \param value Value to be inserted
*
* \note Calling this function will invalidate iterators.
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int BH_QueueInsert(BH_Queue *queue,
void *value);
/**
* Removes front value from the \a queue.
*
* \param queue Pointer to the queue object
*
* \note Calling this function will invalidate iterators.
*/
void BH_QueueRemove(BH_Queue *queue);
/**
* Returns front value from the \a queue.
*
* \param queue Pointer to the queue object
*
* \return On success, returns front value from the queue.
* \return On failure, returns null pointer.
*/
int BH_QueueFront(BH_Queue *queue,
void **value);
/**
* Checks if the \a queue is empty.
*
* \param queue Pointer to the queue object
*
* \return If queue is empty, returns non-zero value
* \return If queue is not empty, returns zero value
*/
int BH_QueueEmpty(BH_Queue *queue);
/**
* Returns the size of the \a queue.
*
* \param queue Pointer to the queue object
*
* \return Returns the size of the queue.
*/
size_t BH_QueueSize(BH_Queue *queue);
/**
* Returns the capacity of the \a queue.
*
* \param queue Pointer to the queue object
*
* \return Returns the capacity of the queue.
*/
size_t BH_QueueCapacity(BH_Queue *queue);
/**
* Returns the iterator to the next element in the \a queue.
*
* \param queue Pointer to the queue object
* \param iter Opaque iterator value
*
* \return If the \a iter doesn't point to the last element of the queue,
* returns next iterator value.
* \return If the \a iter point to the last element of the queue, returns
* null pointer.
* \return If the \a iter is the null pointer, returns iterator to the
* first element of the queue.
*/
void *BH_QueueIterNext(BH_Queue *queue,
void *iter);
/**
* Returns the value, pointed by the queue iterator \a iter.
*
* \param iter Opaque iterator value
*
* \return Returns value, pointed by iterator.
*/
void *BH_QueueIterValue(void *iter);
#endif /* BH_QUEUE_H */