Skip to content

Commit

Permalink
[lib] Renamed static functions in list
Browse files Browse the repository at this point in the history
  • Loading branch information
mta452 committed Oct 13, 2018
1 parent 543f094 commit 32831f7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 72 deletions.
55 changes: 25 additions & 30 deletions Source/SFList.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015 Muhammad Tayyab Akram
* Copyright (C) 2015-2018 Muhammad Tayyab Akram
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
*/

#include <SFConfig.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -24,13 +23,9 @@
#include "SFBase.h"
#include "SFList.h"

#define SF_DEFAULT_LIST_CAPACITY 4

static void _SFListEnsureCapacity(_SFListRef list, SFUInteger capacity);
static void *_SFListGetItemPtr(_SFListRef list, SFUInteger index);
static void _SFListMoveItems(_SFListRef list, SFUInteger srcIndex, SFUInteger dstIndex, SFUInteger itemCount);
#define DEFAULT_LIST_CAPACITY 4

SF_PRIVATE void _SFListInitialize(_SFListRef list, SFUInteger itemSize)
SF_PRIVATE void InitializeList(ListRef list, SFUInteger itemSize)
{
/* Item size MUST be greater than 0. */
SFAssert(itemSize > 0);
Expand All @@ -41,27 +36,27 @@ SF_PRIVATE void _SFListInitialize(_SFListRef list, SFUInteger itemSize)
list->_itemSize = itemSize;
}

SF_PRIVATE void _SFListFinalize(_SFListRef list)
SF_PRIVATE void FinalizeItemsBuffer(ListRef list)
{
free(list->_data);
}

SF_PRIVATE void _SFListFinalizeKeepingArray(_SFListRef list, void **outArray, SFUInteger *outCount)
SF_PRIVATE void ExtractItemsBuffer(ListRef list, void **outArray, SFUInteger *outCount)
{
if (list->count > 0) {
_SFListSetCapacity(list, list->count);
SetItemCapacity(list, list->count);

*outArray = list->_data;
*outCount = list->count;
} else {
_SFListFinalize(list);
FinalizeItemsBuffer(list);

*outArray = NULL;
*outCount = 0;
}
}

SF_PRIVATE void _SFListSetCapacity(_SFListRef list, SFUInteger capacity)
SF_PRIVATE void SetItemCapacity(ListRef list, SFUInteger capacity)
{
/* The new capacity must be larger than total number of elements in the list. */
SFAssert(capacity >= list->count);
Expand All @@ -72,76 +67,76 @@ SF_PRIVATE void _SFListSetCapacity(_SFListRef list, SFUInteger capacity)
}
}

static void _SFListEnsureCapacity(_SFListRef list, SFUInteger capacity)
static void EnsureItemCapacity(ListRef list, SFUInteger capacity)
{
if (list->capacity < capacity) {
SFUInteger newCapacity = (list->capacity ? list->count * 2 : SF_DEFAULT_LIST_CAPACITY);
SFUInteger newCapacity = (list->capacity ? list->count * 2 : DEFAULT_LIST_CAPACITY);
if (newCapacity < capacity) {
newCapacity = capacity;
}

_SFListSetCapacity(list, newCapacity);
SetItemCapacity(list, newCapacity);
}
}

static void *_SFListGetItemPtr(_SFListRef list, SFUInteger index)
static void *GetItemPointer(ListRef list, SFUInteger index)
{
/* The index must fall within allocated capacity. */
SFAssert(index < list->capacity);

return list->_data + (index * list->_itemSize);
}

static void _SFListMoveItems(_SFListRef list, SFUInteger srcIndex, SFUInteger dstIndex, SFUInteger itemCount)
static void MoveItemRange(ListRef list, SFUInteger srcIndex, SFUInteger dstIndex, SFUInteger itemCount)
{
/* The capacity must be available to move the block. */
SFAssert((srcIndex + itemCount) <= list->capacity && (dstIndex + itemCount) <= list->capacity);

if (itemCount) {
memmove(_SFListGetItemPtr(list, dstIndex), _SFListGetItemPtr(list, srcIndex), list->_itemSize * itemCount);
memmove(GetItemPointer(list, dstIndex), GetItemPointer(list, srcIndex), list->_itemSize * itemCount);
}
}

SF_PRIVATE void _SFListReserveRange(_SFListRef list, SFUInteger index, SFUInteger count)
SF_PRIVATE void ReserveItemRange(ListRef list, SFUInteger index, SFUInteger count)
{
/* The index must be valid and there should be no integer overflow. */
SFAssert(index <= list->count && index <= (index + count));

_SFListEnsureCapacity(list, list->count + count);
_SFListMoveItems(list, index, index + count, list->count - index);
EnsureItemCapacity(list, list->count + count);
MoveItemRange(list, index, index + count, list->count - index);
list->count += count;
}

SF_PRIVATE void _SFListRemoveRange(_SFListRef list, SFUInteger index, SFUInteger count)
SF_PRIVATE void RemoveItemRange(ListRef list, SFUInteger index, SFUInteger count)
{
SFUInteger nextIndex = index + count;

/* The specified item indexes must be valid and there should be no integer overflow. */
SFAssert(nextIndex <= list->count && index <= nextIndex);

_SFListMoveItems(list, nextIndex, index, list->count - nextIndex);
MoveItemRange(list, nextIndex, index, list->count - nextIndex);
list->count -= count;
}

SF_PRIVATE void _SFListClear(_SFListRef list)
SF_PRIVATE void RemoveAllItems(ListRef list)
{
list->count = 0;
}

SF_PRIVATE void _SFListTrimExcess(_SFListRef list)
SF_PRIVATE void TrimExcessCapacity(ListRef list)
{
_SFListSetCapacity(list, list->count);
SetItemCapacity(list, list->count);
}

SF_PRIVATE SFUInteger _SFListIndexOfItem(_SFListRef list, const void *itemPtr, SFUInteger index, SFUInteger count)
SF_PRIVATE SFUInteger SearchItemInRange(ListRef list, const void *itemPtr, SFUInteger index, SFUInteger count)
{
SFUInteger max = index + count;

/* The range must be valid and there should be no integer overflow. */
SFAssert((list->count > 0 ? max <= list->count : max == 0) && index <= max);

for (; index < max; index++) {
void *currentPtr = _SFListGetItemPtr(list, index);
void *currentPtr = GetItemPointer(list, index);
if (memcmp(currentPtr, itemPtr, list->_itemSize) == 0) {
return index;
}
Expand All @@ -150,7 +145,7 @@ SF_PRIVATE SFUInteger _SFListIndexOfItem(_SFListRef list, const void *itemPtr, S
return SFInvalidIndex;
}

SF_PRIVATE void _SFListSort(_SFListRef list, SFUInteger index, SFUInteger count, SFComparison comparison)
SF_PRIVATE void SortItemRange(ListRef list, SFUInteger index, SFUInteger count, SFComparison comparison)
{
/* The range must be valid and there should be no integer overflow. */
SFAssert((list->count > 0 ? (index + count) <= list->count : (index + count) == 0)
Expand Down
85 changes: 43 additions & 42 deletions Source/SFList.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Muhammad Tayyab Akram
* Copyright (C) 2015-2018 Muhammad Tayyab Akram
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,12 +22,12 @@
#include "SFAssert.h"
#include "SFBase.h"

typedef struct _SFList {
typedef struct {
SFUInt8 *_data;
SFUInteger count;
SFUInteger capacity;
SFUInteger _itemSize;
} _SFList, *_SFListRef;
} List, *ListRef;

#define SF_LIST(type) \
struct { \
Expand All @@ -39,77 +39,78 @@ struct { \

typedef int (*SFComparison)(const void *item1, const void *item2);

SF_PRIVATE void _SFListInitialize(_SFListRef list, SFUInteger itemSize);
SF_PRIVATE void _SFListFinalize(_SFListRef list);
SF_PRIVATE void _SFListFinalizeKeepingArray(_SFListRef list, void **outArray, SFUInteger *outCount);
SF_PRIVATE void InitializeList(ListRef list, SFUInteger itemSize);
SF_PRIVATE void FinalizeItemsBuffer(ListRef list);
SF_PRIVATE void ExtractItemsBuffer(ListRef list, void **outArray, SFUInteger *outCount);

SF_PRIVATE void _SFListSetCapacity(_SFListRef list, SFUInteger capacity);
SF_PRIVATE void _SFListReserveRange(_SFListRef list, SFUInteger index, SFUInteger count);
SF_PRIVATE void _SFListRemoveRange(_SFListRef list, SFUInteger index, SFUInteger count);
SF_PRIVATE void SetItemCapacity(ListRef list, SFUInteger capacity);
SF_PRIVATE void ReserveItemRange(ListRef list, SFUInteger index, SFUInteger count);
SF_PRIVATE void RemoveItemRange(ListRef list, SFUInteger index, SFUInteger count);

SF_PRIVATE void _SFListClear(_SFListRef list);
SF_PRIVATE void _SFListTrimExcess(_SFListRef list);
SF_PRIVATE void RemoveAllItems(ListRef list);
SF_PRIVATE void TrimExcessCapacity(ListRef list);

SF_PRIVATE SFUInteger _SFListIndexOfItem(_SFListRef list, const void *itemPtr, SFUInteger index, SFUInteger count);
SF_PRIVATE void _SFListSort(_SFListRef list, SFUInteger index, SFUInteger count, SFComparison comparison);
SF_PRIVATE SFUInteger SearchItemInRange(ListRef list, const void *itemPtr, SFUInteger index, SFUInteger count);
SF_PRIVATE void SortItemRange(ListRef list, SFUInteger index, SFUInteger count, SFComparison comparison);

#define _SFListValidateIndex(list_, index_) \
#define CheckItemIndex(list_, index_) \
( \
SFAssert(index_ < (list_)->count) \
)

#define _SFListGetRef(list_, index_) \
#define GetItemReference(list_, index_) \
( \
_SFListValidateIndex(list_, index_), \
CheckItemIndex(list_, index_), \
&(list_)->items[index_] \
)

#define _SFListGetVal(list_, index_) \
#define GetItemAtIndex(list_, index_) \
( \
_SFListValidateIndex(list_, index_), \
CheckItemIndex(list_, index_), \
(list_)->items[index_] \
)

#define _SFListSetVal(list_, index_, item_) \
#define SetItemAtIndex(list_, index_, item_) \
do { \
_SFListValidateIndex(list_, index_), \
CheckItemIndex(list_, index_), \
(list_)->items[index_] = item_; \
} while (0)

#define _SFListInsert(list_, index_, item_) \
#define InsertItemAtIndex(list_, index_, item_) \
do { \
SFUInteger __insertIndex = index_; \
_SFListReserveRange((_SFListRef)(list_), __insertIndex, 1); \
_SFListSetVal(list_, __insertIndex, item_); \
ReserveItemRange((ListRef)(list_), __insertIndex, 1); \
SetItemAtIndex(list_, __insertIndex, item_); \
} while (0)

#define _SFListAdd(list_, item_) \
_SFListInsert(list_, (list_)->count, item_)
#define InsertItemAtEnd(list_, item_) \
InsertItemAtIndex(list_, (list_)->count, item_)


#define SFListInitialize(list, itemSize) _SFListInitialize((_SFListRef)(list), itemSize)
#define SFListFinalize(list) _SFListFinalize((_SFListRef)(list))
#define SFListInitialize(list, itemSize) InitializeList((ListRef)(list), itemSize)
#define SFListFinalize(list) FinalizeItemsBuffer((ListRef)(list))
#define SFListFinalizeKeepingArray(list, outArray, outCount) \
_SFListFinalizeKeepingArray((_SFListRef)(list), (void **)outArray, outCount)
ExtractItemsBuffer((ListRef)(list), (void **)outArray, outCount)

#define SFListSetCapacity(list, capacity) _SFListSetCapacity((_SFListRef)(list), capacity)
#define SFListReserveRange(list, index, count) _SFListReserveRange((_SFListRef)(list), index, count)
#define SFListRemoveRange(list, index, count) _SFListRemoveRange((_SFListRef)(list), index, count)
#define SFListSetCapacity(list, capacity) SetItemCapacity((ListRef)(list), capacity)
#define SFListReserveRange(list, index, count) ReserveItemRange((ListRef)(list), index, count)
#define SFListRemoveRange(list, index, count) RemoveItemRange((ListRef)(list), index, count)

#define SFListClear(list) _SFListClear((_SFListRef)(list))
#define SFListTrimExcess(list) _SFListTrimExcess((_SFListRef)(list))
#define SFListClear(list) RemoveAllItems((ListRef)(list))
#define SFListTrimExcess(list) TrimExcessCapacity((ListRef)(list))

#define SFListGetRef(list, index) _SFListGetRef(list, index)
#define SFListGetVal(list, index) _SFListGetVal(list, index)
#define SFListSetVal(list, index, item) _SFListSetVal(list, index, item)
#define SFListGetRef(list, index) GetItemReference(list, index)
#define SFListGetVal(list, index) GetItemAtIndex(list, index)
#define SFListSetVal(list, index, item) SetItemAtIndex(list, index, item)

#define SFListAdd(list, item) _SFListAdd(list, item)
#define SFListInsert(list, index, item) _SFListInsert(list, index, item)
#define SFListRemoveAt(list, index) _SFListRemoveRange((_SFListRef)(list), index, 1)
#define SFListAdd(list, item) InsertItemAtEnd(list, item)
#define SFListInsert(list, index, item) InsertItemAtIndex(list, index, item)
#define SFListRemoveAt(list, index) RemoveItemRange((ListRef)(list), index, 1)

#define SFListIndexOfItem(list, item, index, count) _SFListIndexOfItem((_SFListRef)(list), item, index, count)
#define SFListContainsItem(list, item) (_SFListIndexOfItem((_SFListRef)(list), item, 0, (list)->count) != SFInvalidIndex)
#define SFListIndexOfItem(list, item, index, count) SearchItemInRange((ListRef)(list), item, index, count)
#define SFListContainsItem(list, item) \
(SearchItemInRange((ListRef)(list), item, 0, (list)->count) != SFInvalidIndex)

#define SFListSort(list, index, count, comparison) _SFListSort((_SFListRef)(list), index, count, comparison);
#define SFListSort(list, index, count, comparison) SortItemRange((ListRef)(list), index, count, comparison);

#endif

0 comments on commit 32831f7

Please sign in to comment.