Skip to content

Commit

Permalink
Optimized array, list and map for embedded systems
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Kalatozishvili committed Sep 19, 2023
1 parent 2815439 commit 2f489b0
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"utility": "c",
"xcli.h": "c",
"xlog.h": "c",
"xtime.h": "c"
"xtime.h": "c",
"stdlib.h": "c",
"xdef.h": "c"
}
}
20 changes: 10 additions & 10 deletions src/data/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
* with some sorting and search algorithms.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "array.h"

xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint64_t nKey)
xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey)
{
xarray_data_t *pNewData = (xarray_data_t*)malloc(sizeof(xarray_data_t));
if (pNewData == NULL) return NULL;
Expand Down Expand Up @@ -78,7 +76,7 @@ void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed)

if (nSize)
{
pArr->pData = (xarray_data_t**)calloc(nSize, sizeof(xarray_data_t*));
pArr->pData = (xarray_data_t**)malloc(nSize * sizeof(xarray_data_t*));
if (pArr->pData == NULL) return NULL;
}

Expand Down Expand Up @@ -242,7 +240,7 @@ int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize)
return XArray_Add(pArr, pNewData);
}

int XArray_AddDataKey(xarray_t *pArr, void *pData, size_t nSize, uint64_t nKey)
int XArray_AddDataKey(xarray_t *pArr, void *pData, size_t nSize, uint32_t nKey)
{
xarray_data_t *pNewData = XArray_NewData(pData, nSize, nKey);

Expand Down Expand Up @@ -282,7 +280,7 @@ size_t XArray_GetSize(xarray_t *pArr, size_t nIndex)
return pArrData ? pArrData->nSize : 0;
}

uint64_t XArray_GetKey(xarray_t *pArr, size_t nIndex)
uint32_t XArray_GetKey(xarray_t *pArr, size_t nIndex)
{
if (nIndex >= pArr->nSize) return 0;
xarray_data_t *pArrData = pArr->pData[nIndex];
Expand Down Expand Up @@ -379,13 +377,15 @@ void XArray_Swap(xarray_t *pArr, size_t nIndex1, size_t nIndex2)

static int XArray_CompareSize(const void *pData1, const void *pData2, void *pCtx)
{
(void)pCtx;
xarray_data_t *pFirst = (xarray_data_t*)pData1;
xarray_data_t *pSecond = (xarray_data_t*)pData2;
return (int)pFirst->nSize - (int)pSecond->nSize;
}

static int XArray_CompareKey(const void *pData1, const void *pData2, void *pCtx)
{
(void)pCtx;
xarray_data_t *pFirst = (xarray_data_t*)pData1;
xarray_data_t *pSecond = (xarray_data_t*)pData2;
return (int)pFirst->nKey - (int)pSecond->nKey;
Expand Down Expand Up @@ -449,7 +449,7 @@ void XArray_BubbleSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx)
}
}

int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey)
int XArray_LinearSearch(xarray_t *pArr, uint32_t nKey)
{
if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE;
size_t i = 0;
Expand All @@ -463,7 +463,7 @@ int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey)
return XARRAY_FAILURE;
}

int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey)
int XArray_SentinelSearch(xarray_t *pArr, uint32_t nKey)
{
if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE;
int i, nRet = 0, nLast = (int)pArr->nUsed - 1;
Expand All @@ -488,7 +488,7 @@ int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey)
return nRet;
}

int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey)
int XArray_DoubleSearch(xarray_t *pArr, uint32_t nKey)
{
if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE;
int nFront = 0, nBack = (int)pArr->nUsed - 1;
Expand All @@ -508,7 +508,7 @@ int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey)
return XARRAY_FAILURE;
}

int XArray_BinarySearch(xarray_t *pArr, uint64_t nKey)
int XArray_BinarySearch(xarray_t *pArr, uint32_t nKey)
{
if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE;
int nLeft = 0, nRight = (int)pArr->nUsed - 1;
Expand Down
17 changes: 8 additions & 9 deletions src/data/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
extern "C" {
#endif

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

Expand All @@ -35,7 +34,7 @@ typedef enum {
typedef struct XArrayData {
void* pData;
size_t nSize;
uint64_t nKey;
uint32_t nKey;
} xarray_data_t;

typedef int(*xarray_comparator_t)(const void*, const void*, void*);
Expand All @@ -51,7 +50,7 @@ typedef struct XArray_ {
size_t nUsed;
} xarray_t;

xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint64_t nKey);
xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey);
void XArray_FreeData(xarray_data_t *pArrData);
void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData);

Expand All @@ -65,11 +64,11 @@ void XArray_Free(xarray_t **ppArr);
int XArray_Add(xarray_t *pArr, xarray_data_t *pNewData);
int XArray_AddData(xarray_t *pArr, void* pData, size_t nSize);
int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize);
int XArray_AddDataKey(xarray_t *pArr, void* pData, size_t nSize, uint64_t nKey);
int XArray_AddDataKey(xarray_t *pArr, void* pData, size_t nSize, uint32_t nKey);
void* XArray_GetDataOr(xarray_t *pArr, size_t nIndex, void *pRet);
void* XArray_GetData(xarray_t *pArr, size_t nIndex);
size_t XArray_GetSize(xarray_t *pArr, size_t nIndex);
uint64_t XArray_GetKey(xarray_t *pArr, size_t nIndex);
uint32_t XArray_GetKey(xarray_t *pArr, size_t nIndex);
uint8_t XArray_Contains(xarray_t *pArr, size_t nIndex);

xarray_data_t* XArray_Remove(xarray_t *pArr, size_t nIndex);
Expand All @@ -81,10 +80,10 @@ void XArray_BubbleSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx);
void XArray_QuickSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx, int nStart, int nFinish);
void XArray_SortBy(xarray_t *pArr, int nSortBy);

int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey);
int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey);
int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey);
int XArray_BinarySearch(xarray_t *pArr, uint64_t nKey);
int XArray_SentinelSearch(xarray_t *pArr, uint32_t nKey);
int XArray_LinearSearch(xarray_t *pArr, uint32_t nKey);
int XArray_DoubleSearch(xarray_t *pArr, uint32_t nKey);
int XArray_BinarySearch(xarray_t *pArr, uint32_t nKey);

xarray_data_t* XArray_Get(xarray_t *pArr, size_t nIndex);
xarray_data_t* XArray_Set(xarray_t *pArr, size_t nIndex, xarray_data_t *pNewData);
Expand Down
20 changes: 18 additions & 2 deletions src/data/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
* @brief Implementation of dynamically allocated hash map
*/

#include <string.h>
#include "map.h"

#ifdef _XMAP_USE_CRYPT
#include "crc32.h"
#include "sha256.h"
#endif

#define XFNV_OFFSET_32 2166136261
#define XFNV_PRIME_32 16777619
Expand All @@ -23,8 +27,18 @@ int XMap_Init(xmap_t *pMap, size_t nSize)
pMap->nUsed = 0;
if (!nSize) return XMAP_OK;

pMap->pPairs = (xmap_pair_t*)calloc(nSize, sizeof(xmap_pair_t));
return (pMap->pPairs == NULL) ? XMAP_OMEM : XMAP_OK;
pMap->pPairs = (xmap_pair_t*)malloc(nSize * sizeof(xmap_pair_t));
if (pMap->pPairs == NULL) return XMAP_OMEM;

size_t i;
for (i = 0; i < pMap->nTableSize; i++)
{
pMap->pPairs[i].nUsed = 0;
pMap->pPairs[i].pData = NULL;
pMap->pPairs[i].pKey = NULL;
}

return XMAP_OK;
}

xmap_t *XMap_New(size_t nSize)
Expand Down Expand Up @@ -102,6 +116,7 @@ void XMap_Destroy(xmap_t *pMap)
XMap_Free(pMap);
}

#ifdef _XMAP_USE_CRYPT
int XMap_HashMIX(xmap_t *pMap, const char *pStr)
{
if (!pMap->nTableSize) return XMAP_EINIT;
Expand Down Expand Up @@ -154,6 +169,7 @@ int XMap_HashSHA(xmap_t *pMap, const char *pStr)

return nHash % pMap->nTableSize;
}
#endif /* _XMAP_USE_CRYPT */

int XMap_Hash(xmap_t *pMap, const char *pStr)
{
Expand Down
12 changes: 8 additions & 4 deletions src/data/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
extern "C" {
#endif

#include "xstd.h"
#include <stdint.h>
#include <stdlib.h>

#define XMAP_INITIAL_SIZE 16
#define XMAP_CHAIN_LENGTH 32
Expand Down Expand Up @@ -61,10 +62,13 @@ int XMap_PutPair(xmap_t *pMap, xmap_pair_t *pPair);
int XMap_Remove(xmap_t *pMap, const char* pKey);
int XMap_Update(xmap_t *pMap, int nHash, char *pKey, void *pValue);

#ifdef _XMAP_USE_CRYPT
int XMap_HashMIX(xmap_t* pMap, const char* pStr);
int XMap_HashFNV(xmap_t* pMap, const char* pStr);
int XMap_HashSHA(xmap_t* pMap, const char* pStr);
#endif

int XMap_GetHash(xmap_t *pMap, const char* pKey);
int XMap_HashMIX(xmap_t *pMap, const char *pStr);
int XMap_HashFNV(xmap_t *pMap, const char *pStr);
int XMap_HashSHA(xmap_t *pMap, const char *pStr);
int XMap_Hash(xmap_t *pMap, const char *pStr);

int XMap_Iterate(xmap_t *pMap, xmap_iterator_t itfunc, void *pCtx);
Expand Down
26 changes: 13 additions & 13 deletions src/data/xbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ uint8_t *XByteData_Dup(const uint8_t *pBuff, size_t nLength)
if (pData == NULL) return NULL;

memcpy(pData, pBuff, nLength);
pData[nLength] = XSTR_NUL;
pData[nLength] = '\0';

return pData;
}
Expand Down Expand Up @@ -65,7 +65,7 @@ int XByteBuffer_Resize(xbyte_buffer_t *pBuffer, size_t nSize)
}

pBuffer->nUsed = (pBuffer->nUsed >= nSize) ? (nSize - 1) : pBuffer->nUsed;
if (pBuffer->nUsed < nSize) pBuffer->pData[pBuffer->nUsed] = XSTR_NUL;
if (pBuffer->nUsed < nSize) pBuffer->pData[pBuffer->nUsed] = '\0';

pBuffer->nSize = nSize;
return (int)pBuffer->nSize;
Expand All @@ -75,7 +75,7 @@ int XByteBuffer_Terminate(xbyte_buffer_t *pBuffer, size_t nPosit)
{
if (pBuffer->pData == NULL || !pBuffer->nUsed) return XSTDERR;
size_t nTerminatePosit = XSTD_MIN(pBuffer->nUsed, nPosit);
pBuffer->pData[nTerminatePosit] = XSTR_NUL;
pBuffer->pData[nTerminatePosit] = '\0';
pBuffer->nUsed = nTerminatePosit;
return XSTDOK;
}
Expand Down Expand Up @@ -212,14 +212,6 @@ int XByteBuffer_AddStr(xbyte_buffer_t *pBuffer, xstring_t *pStr)
return XByteBuffer_Add(pBuffer, pData, pStr->nLength);
}

int XByteBuffer_AddByte(xbyte_buffer_t *pBuffer, uint8_t nByte)
{
if (XByteBuffer_Reserve(pBuffer, 2) <= 0) return XSTDERR;
pBuffer->pData[pBuffer->nUsed++] = nByte;
pBuffer->pData[pBuffer->nUsed] = '\0';
return (int)pBuffer->nUsed;
}

int XByteBuffer_AddFmt(xbyte_buffer_t *pBuffer, const char *pFmt, ...)
{
size_t nBytes = 0;
Expand All @@ -241,6 +233,14 @@ int XByteBuffer_AddFmt(xbyte_buffer_t *pBuffer, const char *pFmt, ...)
return nStatus;
}

int XByteBuffer_AddByte(xbyte_buffer_t *pBuffer, uint8_t nByte)
{
if (XByteBuffer_Reserve(pBuffer, 2) <= 0) return XSTDERR;
pBuffer->pData[pBuffer->nUsed++] = nByte;
pBuffer->pData[pBuffer->nUsed] = '\0';
return (int)pBuffer->nUsed;
}

int XByteBuffer_NullTerm(xbyte_buffer_t *pBuffer)
{
if (XByteBuffer_Reserve(pBuffer, 1) <= 0) return XSTDERR;
Expand Down Expand Up @@ -317,7 +317,7 @@ xbool_t XByteBuffer_HasData(xbyte_buffer_t *pBuffer)

int XDataBuffer_Init(xdata_buffer_t *pBuffer, size_t nSize, int nFixed)
{
pBuffer->pData = (void**)calloc(nSize, sizeof(void*));
pBuffer->pData = (void**)malloc(nSize * sizeof(void*));
if (pBuffer->pData == NULL)
{
pBuffer->nSize = 0;
Expand Down Expand Up @@ -445,7 +445,7 @@ int XRingBuffer_Init(xring_buffer_t *pBuffer, size_t nSize)
pBuffer->nUsed = 0;
unsigned int i;

pBuffer->pData = (xbyte_buffer_t**)calloc(nSize, sizeof(xbyte_buffer_t*));
pBuffer->pData = (xbyte_buffer_t**)malloc(nSize * sizeof(xbyte_buffer_t*));
if (pBuffer->pData == NULL) return 0;

for (i = 0; i < pBuffer->nSize; i++)
Expand Down
1 change: 0 additions & 1 deletion src/data/xbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#ifndef __XUTILS_BUFFER_H__
#define __XUTILS_BUFFER_H__

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "xstr.h"
Expand Down
1 change: 1 addition & 0 deletions src/net/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "api.h"
#include "xver.h"
#include "xstr.h"
#include "xbuf.h"
#include "sha1.h"
#include "base64.h"
Expand Down
1 change: 1 addition & 0 deletions src/sys/xfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C" {
#endif

#include "xstd.h"
#include "xstr.h"
#include "xbuf.h"
#include "array.h"

Expand Down
2 changes: 1 addition & 1 deletion src/xver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define XUTILS_VERSION_MAX 2
#define XUTILS_VERSION_MIN 5
#define XUTILS_BUILD_NUMBER 48
#define XUTILS_BUILD_NUMBER 49

#ifdef __cplusplus
extern "C" {
Expand Down

0 comments on commit 2f489b0

Please sign in to comment.