Move everything out of src folder
This commit is contained in:
1 parent
2c1eb41280
commit
47ece66520
135 files changed
+13
-7
No files matched your search
+644
@@ -0,0 +1,644 @@
|
||||
/**
|
||||
* Copyright (c) 2019 Leonardo Vencovsky
|
||||
*
|
||||
* This file is part of the C Macro Collections Library.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/* Implementation Detail Functions */
|
||||
/* None */
|
||||
|
||||
#ifdef CMC_SAC
|
||||
struct SNAME *CMC_(PFX, _new)(struct CMC_DEF_FVAL(SNAME) * f_val)
|
||||
#else
|
||||
struct SNAME *CMC_(PFX, _new)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val)
|
||||
#endif
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
#ifdef CMC_SAC
|
||||
return CMC_(PFX, _new_custom)(f_val, NULL, NULL);
|
||||
#else
|
||||
return CMC_(PFX, _new_custom)(capacity, f_val, NULL, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef CMC_SAC
|
||||
struct SNAME *CMC_(PFX, _new_custom)(struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc,
|
||||
CMC_CALLBACK_TYPE callbacks)
|
||||
#else
|
||||
struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc,
|
||||
CMC_CALLBACK_TYPE callbacks)
|
||||
#endif
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
CMC_CALLBACKS_MAYBE_UNUSED(callbacks);
|
||||
|
||||
#ifndef CMC_SAC
|
||||
if (capacity < 1)
|
||||
return NULL;
|
||||
#endif
|
||||
|
||||
if (!alloc)
|
||||
alloc = &cmc_alloc_node_default;
|
||||
|
||||
struct SNAME *_list_ = alloc->malloc(sizeof(struct SNAME));
|
||||
|
||||
if (!_list_)
|
||||
return NULL;
|
||||
|
||||
#ifdef CMC_SAC
|
||||
memset(_list_->buffer, 0, sizeof(V) * SIZE);
|
||||
#else
|
||||
_list_->buffer = alloc->calloc(capacity, sizeof(V));
|
||||
#endif
|
||||
|
||||
#ifndef CMC_SAC
|
||||
if (!_list_->buffer)
|
||||
{
|
||||
alloc->free(_list_);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CMC_SAC
|
||||
_list_->capacity = capacity;
|
||||
#endif
|
||||
_list_->count = 0;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
_list_->f_val = f_val;
|
||||
_list_->alloc = alloc;
|
||||
CMC_CALLBACKS_ASSIGN(_list_, callbacks);
|
||||
|
||||
return _list_;
|
||||
}
|
||||
|
||||
void CMC_(PFX, _clear)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (_list_->f_val && _list_->f_val->free)
|
||||
{
|
||||
for (size_t i = 0; i < _list_->count; i++)
|
||||
_list_->f_val->free(_list_->buffer[i]);
|
||||
}
|
||||
|
||||
#ifdef CMC_SAC
|
||||
memset(_list_->buffer, 0, sizeof(V) * SIZE);
|
||||
#else
|
||||
memset(_list_->buffer, 0, sizeof(V) * _list_->capacity);
|
||||
#endif
|
||||
|
||||
_list_->count = 0;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
}
|
||||
|
||||
void CMC_(PFX, _free)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (_list_->f_val && _list_->f_val->free)
|
||||
{
|
||||
for (size_t i = 0; i < _list_->count; i++)
|
||||
_list_->f_val->free(_list_->buffer[i]);
|
||||
}
|
||||
|
||||
#ifndef CMC_SAC
|
||||
_list_->alloc->free(_list_->buffer);
|
||||
#endif
|
||||
_list_->alloc->free(_list_);
|
||||
}
|
||||
|
||||
void CMC_(PFX, _customize)(struct SNAME *_list_, CMC_ALLOC_TYPE alloc, CMC_CALLBACK_TYPE callbacks)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
CMC_CALLBACKS_MAYBE_UNUSED(callbacks);
|
||||
|
||||
#ifdef CMC_SAC
|
||||
CMC_UNUSED_PARAM(alloc);
|
||||
#else
|
||||
if (!alloc)
|
||||
_list_->alloc = &cmc_alloc_node_default;
|
||||
else
|
||||
_list_->alloc = alloc;
|
||||
#endif
|
||||
|
||||
CMC_CALLBACKS_ASSIGN(_list_, callbacks);
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _push_front)(struct SNAME *_list_, V value)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _full)(_list_))
|
||||
{
|
||||
#ifdef CMC_SAC
|
||||
_list_->flag = CMC_FLAG_FULL;
|
||||
return false;
|
||||
#else
|
||||
if (!CMC_(PFX, _resize)(_list_, _list_->count * 2))
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!CMC_(PFX, _empty)(_list_))
|
||||
{
|
||||
memmove(_list_->buffer + 1, _list_->buffer, _list_->count * sizeof(V));
|
||||
}
|
||||
|
||||
_list_->buffer[0] = value;
|
||||
|
||||
_list_->count++;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _push_at)(struct SNAME *_list_, V value, size_t index)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (index > _list_->count)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_RANGE;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (CMC_(PFX, _full)(_list_))
|
||||
{
|
||||
#ifdef CMC_SAC
|
||||
_list_->flag = CMC_FLAG_FULL;
|
||||
return false;
|
||||
#else
|
||||
if (!CMC_(PFX, _resize)(_list_, _list_->count * 2))
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
memmove(_list_->buffer + index + 1, _list_->buffer + index, (_list_->count - index) * sizeof(V));
|
||||
|
||||
_list_->buffer[index] = value;
|
||||
_list_->count++;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _push_back)(struct SNAME *_list_, V value)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _full)(_list_))
|
||||
{
|
||||
#ifdef CMC_SAC
|
||||
_list_->flag = CMC_FLAG_FULL;
|
||||
return false;
|
||||
#else
|
||||
if (!CMC_(PFX, _resize)(_list_, _list_->count * 2))
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
_list_->buffer[_list_->count++] = value;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _pop_front)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(_list_))
|
||||
{
|
||||
_list_->flag = CMC_FLAG_EMPTY;
|
||||
return false;
|
||||
}
|
||||
|
||||
memmove(_list_->buffer, _list_->buffer + 1, (_list_->count - 1) * sizeof(V));
|
||||
|
||||
_list_->buffer[--_list_->count] = (V){ 0 };
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _pop_at)(struct SNAME *_list_, size_t index)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(_list_))
|
||||
{
|
||||
_list_->flag = CMC_FLAG_EMPTY;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index >= _list_->count)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_RANGE;
|
||||
return false;
|
||||
}
|
||||
|
||||
memmove(_list_->buffer + index, _list_->buffer + index + 1, (_list_->count - index - 1) * sizeof(V));
|
||||
|
||||
_list_->buffer[--_list_->count] = (V){ 0 };
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _pop_back)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(_list_))
|
||||
{
|
||||
_list_->flag = CMC_FLAG_EMPTY;
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
return false;
|
||||
}
|
||||
|
||||
_list_->buffer[--_list_->count] = (V){ 0 };
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
V CMC_(PFX, _front)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(_list_))
|
||||
{
|
||||
_list_->flag = CMC_FLAG_EMPTY;
|
||||
return (V){ 0 };
|
||||
}
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return _list_->buffer[0];
|
||||
}
|
||||
|
||||
V CMC_(PFX, _get)(struct SNAME *_list_, size_t index)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(_list_))
|
||||
{
|
||||
_list_->flag = CMC_FLAG_EMPTY;
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
return (V){ 0 };
|
||||
}
|
||||
|
||||
if (index >= _list_->count)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_RANGE;
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
return (V){ 0 };
|
||||
}
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return _list_->buffer[index];
|
||||
}
|
||||
|
||||
V *CMC_(PFX, _get_ref)(struct SNAME *_list_, size_t index)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(_list_))
|
||||
{
|
||||
_list_->flag = CMC_FLAG_EMPTY;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (index >= _list_->count)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_RANGE;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return &(_list_->buffer[index]);
|
||||
}
|
||||
|
||||
V CMC_(PFX, _back)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(_list_))
|
||||
{
|
||||
_list_->flag = CMC_FLAG_EMPTY;
|
||||
return (V){ 0 };
|
||||
}
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return _list_->buffer[_list_->count - 1];
|
||||
}
|
||||
|
||||
size_t CMC_(PFX, _index_of)(struct SNAME *_list_, V value, bool from_start)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (_list_->f_val == NULL || _list_->f_val->cmp == NULL)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_FTABLE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
size_t result = _list_->count;
|
||||
|
||||
if (from_start)
|
||||
{
|
||||
for (size_t i = 0; i < _list_->count; i++)
|
||||
{
|
||||
if (0 == _list_->f_val->cmp(_list_->buffer[i], value))
|
||||
{
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = _list_->count; i > 0; i--)
|
||||
{
|
||||
if (0 == _list_->f_val->cmp(_list_->buffer[i - 1], value))
|
||||
{
|
||||
result = i - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _contains)(struct SNAME *_list_, V value)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (_list_->f_val == NULL || _list_->f_val->cmp == NULL)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_FTABLE;
|
||||
return false;
|
||||
}
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
bool result = false;
|
||||
|
||||
for (size_t i = 0; i < _list_->count; i++)
|
||||
{
|
||||
if (0 == _list_->f_val->cmp(_list_->buffer[i], value))
|
||||
{
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _empty)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
return _list_->count == 0;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _full)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
#ifdef CMC_SAC
|
||||
return _list_->count >= SIZE;
|
||||
#else
|
||||
return _list_->count >= _list_->capacity;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t CMC_(PFX, _count)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
return _list_->count;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _fits)(struct SNAME *_list_, size_t size)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
#ifdef CMC_SAC
|
||||
return _list_->count + size <= SIZE;
|
||||
#else
|
||||
return _list_->count + size <= _list_->capacity;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t CMC_(PFX, _capacity)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
#ifdef CMC_SAC
|
||||
CMC_UNUSED_PARAM(_list_);
|
||||
return SIZE;
|
||||
#else
|
||||
return _list_->capacity;
|
||||
#endif
|
||||
}
|
||||
|
||||
int CMC_(PFX, _flag)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
return _list_->flag;
|
||||
}
|
||||
|
||||
#ifndef CMC_SAC
|
||||
bool CMC_(PFX, _resize)(struct SNAME *_list_, size_t capacity)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
if (_list_->capacity == capacity)
|
||||
return true;
|
||||
|
||||
if (capacity < _list_->count)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_INVALID;
|
||||
return false;
|
||||
}
|
||||
|
||||
V *new_buffer = _list_->alloc->realloc(_list_->buffer, sizeof(V) * capacity);
|
||||
|
||||
if (!new_buffer)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_ALLOC;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* TODO zero out new slots */
|
||||
|
||||
_list_->buffer = new_buffer;
|
||||
_list_->capacity = capacity;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct SNAME *CMC_(PFX, _copy_of)(struct SNAME *_list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
#ifdef CMC_SAC
|
||||
struct SNAME *result = CMC_(PFX, _new_custom)(_list_->f_val, _list_->alloc, NULL);
|
||||
#else
|
||||
struct SNAME *result = CMC_(PFX, _new_custom)(_list_->capacity, _list_->f_val, _list_->alloc, NULL);
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_ALLOC;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CMC_CALLBACKS_ASSIGN(result, _list_->callbacks);
|
||||
|
||||
if (_list_->f_val && _list_->f_val->cpy)
|
||||
{
|
||||
for (size_t i = 0; i < _list_->count; i++)
|
||||
result->buffer[i] = _list_->f_val->cpy(_list_->buffer[i]);
|
||||
}
|
||||
else
|
||||
memcpy(result->buffer, _list_->buffer, sizeof(V) * _list_->count);
|
||||
|
||||
result->count = _list_->count;
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _equals)(struct SNAME *_list1_, struct SNAME *_list2_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
_list1_->flag = CMC_FLAG_OK;
|
||||
_list2_->flag = CMC_FLAG_OK;
|
||||
|
||||
if (_list1_->count != _list2_->count)
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i < _list1_->count; i++)
|
||||
{
|
||||
if (0 != _list1_->f_val->cmp(_list1_->buffer[i], _list2_->buffer[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,623 @@
|
||||
/**
|
||||
* Copyright (c) 2019 Leonardo Vencovsky
|
||||
*
|
||||
* This file is part of the C Macro Collections Library.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* INIT
|
||||
*
|
||||
* The part 'INIT' gives a new way of initializing a collection. The collection
|
||||
* struct is not heap allocated, only its internal structure (nodes, buffers).
|
||||
*/
|
||||
#ifdef CMC_EXT_INIT
|
||||
|
||||
#ifdef CMC_SAC
|
||||
struct SNAME CMC_(PFX, _init)(struct CMC_DEF_FVAL(SNAME) * f_val)
|
||||
#else
|
||||
struct SNAME CMC_(PFX, _init)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val)
|
||||
#endif
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
#ifdef CMC_SAC
|
||||
return CMC_(PFX, _init_custom)(f_val, NULL, NULL);
|
||||
#else
|
||||
return CMC_(PFX, _init_custom)(capacity, f_val, NULL, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef CMC_SAC
|
||||
struct SNAME CMC_(PFX, _init_custom)(struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc,
|
||||
CMC_CALLBACK_TYPE callbacks)
|
||||
#else
|
||||
struct SNAME CMC_(PFX, _init_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc,
|
||||
CMC_CALLBACK_TYPE callbacks)
|
||||
#endif
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
CMC_CALLBACKS_MAYBE_UNUSED(callbacks);
|
||||
|
||||
struct SNAME _list_ = { 0 };
|
||||
|
||||
#ifndef CMC_SAC
|
||||
if (capacity < 1)
|
||||
return _list_;
|
||||
#endif
|
||||
|
||||
if (!f_val)
|
||||
return _list_;
|
||||
|
||||
if (!alloc)
|
||||
alloc = &cmc_alloc_node_default;
|
||||
|
||||
#ifdef CMC_SAC
|
||||
memset(_list_.buffer, 0, sizeof(V) * SIZE);
|
||||
#else
|
||||
_list_.buffer = alloc->calloc(capacity, sizeof(V));
|
||||
#endif
|
||||
|
||||
if (!_list_.buffer)
|
||||
return _list_;
|
||||
|
||||
#ifndef CMC_SAC
|
||||
_list_.capacity = capacity;
|
||||
#endif
|
||||
_list_.count = 0;
|
||||
_list_.flag = CMC_FLAG_OK;
|
||||
_list_.f_val = f_val;
|
||||
_list_.alloc = alloc;
|
||||
CMC_CALLBACKS_ASSIGN(&_list_, callbacks);
|
||||
|
||||
return _list_;
|
||||
}
|
||||
|
||||
void CMC_(PFX, _release)(struct SNAME _list_)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (_list_.f_val->free)
|
||||
{
|
||||
for (size_t i = 0; i < _list_.count; i++)
|
||||
_list_.f_val->free(_list_.buffer[i]);
|
||||
}
|
||||
|
||||
#ifndef CMC_SAC
|
||||
_list_.alloc->free(_list_.buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* CMC_EXT_INIT */
|
||||
|
||||
/**
|
||||
* ITER
|
||||
*
|
||||
* List bi-directional iterator.
|
||||
*/
|
||||
#ifdef CMC_EXT_ITER
|
||||
|
||||
struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_start)(struct SNAME *target)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
struct CMC_DEF_ITER(SNAME) iter;
|
||||
|
||||
iter.target = target;
|
||||
iter.cursor = 0;
|
||||
iter.start = true;
|
||||
iter.end = CMC_(PFX, _empty)(target);
|
||||
|
||||
return iter;
|
||||
}
|
||||
|
||||
struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_end)(struct SNAME *target)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
struct CMC_DEF_ITER(SNAME) iter;
|
||||
|
||||
iter.target = target;
|
||||
iter.cursor = 0;
|
||||
iter.start = CMC_(PFX, _empty)(target);
|
||||
iter.end = true;
|
||||
|
||||
if (!CMC_(PFX, _empty)(target))
|
||||
iter.cursor = target->count - 1;
|
||||
|
||||
return iter;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _iter_at_start)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
return CMC_(PFX, _empty)(iter->target) || iter->start;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _iter_at_end)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
return CMC_(PFX, _empty)(iter->target) || iter->end;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _iter_to_start)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (!CMC_(PFX, _empty)(iter->target))
|
||||
{
|
||||
iter->cursor = 0;
|
||||
iter->start = true;
|
||||
iter->end = CMC_(PFX, _empty)(iter->target);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _iter_to_end)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (!CMC_(PFX, _empty)(iter->target))
|
||||
{
|
||||
iter->start = CMC_(PFX, _empty)(iter->target);
|
||||
iter->cursor = iter->target->count - 1;
|
||||
iter->end = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _iter_next)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (iter->end)
|
||||
return false;
|
||||
|
||||
if (iter->cursor + 1 == iter->target->count)
|
||||
{
|
||||
iter->end = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
iter->start = CMC_(PFX, _empty)(iter->target);
|
||||
|
||||
iter->cursor++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _iter_prev)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (iter->start)
|
||||
return false;
|
||||
|
||||
if (iter->cursor == 0)
|
||||
{
|
||||
iter->start = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
iter->end = CMC_(PFX, _empty)(iter->target);
|
||||
|
||||
iter->cursor--;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Returns true only if the iterator moved */
|
||||
bool CMC_(PFX, _iter_advance)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (iter->end)
|
||||
return false;
|
||||
|
||||
if (iter->cursor + 1 == iter->target->count)
|
||||
{
|
||||
iter->end = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (steps == 0 || iter->cursor + steps >= iter->target->count)
|
||||
return false;
|
||||
|
||||
iter->start = CMC_(PFX, _empty)(iter->target);
|
||||
|
||||
iter->cursor += steps;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Returns true only if the iterator moved */
|
||||
bool CMC_(PFX, _iter_rewind)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (iter->start)
|
||||
return false;
|
||||
|
||||
if (iter->cursor == 0)
|
||||
{
|
||||
iter->start = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (steps == 0 || iter->cursor < steps)
|
||||
return false;
|
||||
|
||||
iter->end = CMC_(PFX, _empty)(iter->target);
|
||||
|
||||
iter->cursor -= steps;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Returns true only if the iterator was able to be positioned at the */
|
||||
/* given index */
|
||||
bool CMC_(PFX, _iter_go_to)(struct CMC_DEF_ITER(SNAME) * iter, size_t index)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (index >= iter->target->count)
|
||||
return false;
|
||||
|
||||
if (iter->cursor > index)
|
||||
return CMC_(PFX, _iter_rewind)(iter, iter->cursor - index);
|
||||
else if (iter->cursor < index)
|
||||
return CMC_(PFX, _iter_advance)(iter, index - iter->cursor);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
V CMC_(PFX, _iter_value)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(iter->target))
|
||||
return (V){ 0 };
|
||||
|
||||
return iter->target->buffer[iter->cursor];
|
||||
}
|
||||
|
||||
V *CMC_(PFX, _iter_rvalue)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (CMC_(PFX, _empty)(iter->target))
|
||||
return NULL;
|
||||
|
||||
return &(iter->target->buffer[iter->cursor]);
|
||||
}
|
||||
|
||||
size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
return iter->cursor;
|
||||
}
|
||||
|
||||
#endif /* CMC_EXT_ITER */
|
||||
|
||||
/**
|
||||
* SEQ
|
||||
*
|
||||
* Push and pop sequence of items.
|
||||
*/
|
||||
#ifdef CMC_EXT_SEQ
|
||||
|
||||
bool CMC_(PFX, _seq_push_front)(struct SNAME *_list_, V *values, size_t size)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (size == 0)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_INVALID;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CMC_(PFX, _fits)(_list_, size))
|
||||
{
|
||||
#ifdef CMC_SAC
|
||||
_list_->flag = CMC_FLAG_FULL;
|
||||
return false;
|
||||
#else
|
||||
if (!CMC_(PFX, _resize)(_list_, _list_->count + size))
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
memmove(_list_->buffer + size, _list_->buffer, _list_->count * sizeof(V));
|
||||
|
||||
memcpy(_list_->buffer, values, size * sizeof(V));
|
||||
|
||||
_list_->count += size;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _seq_push_at)(struct SNAME *_list_, V *values, size_t size, size_t index)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (size == 0)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_INVALID;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index > _list_->count)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_RANGE;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index == 0)
|
||||
return CMC_(PFX, _seq_push_front)(_list_, values, size);
|
||||
else if (index == _list_->count)
|
||||
return CMC_(PFX, _seq_push_back)(_list_, values, size);
|
||||
|
||||
if (!CMC_(PFX, _fits)(_list_, size))
|
||||
{
|
||||
#ifdef CMC_SAC
|
||||
_list_->flag = CMC_FLAG_FULL;
|
||||
return false;
|
||||
#else
|
||||
if (!CMC_(PFX, _resize)(_list_, _list_->count + size))
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
memmove(_list_->buffer + index + size, _list_->buffer + index, (_list_->count - index) * sizeof(V));
|
||||
|
||||
memcpy(_list_->buffer + index, values, size * sizeof(V));
|
||||
|
||||
_list_->count += size;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _seq_push_back)(struct SNAME *_list_, V *values, size_t size)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (size == 0)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_INVALID;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CMC_(PFX, _fits)(_list_, size))
|
||||
{
|
||||
#ifdef CMC_SAC
|
||||
_list_->flag = CMC_FLAG_FULL;
|
||||
return false;
|
||||
#else
|
||||
if (!CMC_(PFX, _resize)(_list_, _list_->count + size))
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
memcpy(_list_->buffer + _list_->count, values, size * sizeof(V));
|
||||
|
||||
_list_->count += size;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _seq_pop_at)(struct SNAME *_list_, size_t from, size_t to)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (from > to)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_INVALID;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (to >= _list_->count)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_RANGE;
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t length = (to - from + 1);
|
||||
|
||||
memmove(_list_->buffer + from, _list_->buffer + to + 1, (_list_->count - to - 1) * sizeof(V));
|
||||
|
||||
memset(_list_->buffer + _list_->count - length, 0, length * sizeof(V));
|
||||
|
||||
_list_->count -= to - from + 1;
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct SNAME *CMC_(PFX, _seq_sublist)(struct SNAME *_list_, size_t from, size_t to)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
if (from > to)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_INVALID;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (to >= _list_->count)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_RANGE;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t length = to - from + 1;
|
||||
|
||||
#ifdef CMC_SAC
|
||||
struct SNAME *result = CMC_(PFX, _new_custom)(_list_->f_val, _list_->alloc, CMC_CALLBACKS_GET(_list_));
|
||||
#else
|
||||
struct SNAME *result = CMC_(PFX, _new_custom)(length, _list_->f_val, _list_->alloc, CMC_CALLBACKS_GET(_list_));
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
{
|
||||
_list_->flag = CMC_FLAG_ALLOC;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(result->buffer, _list_->buffer, length * sizeof(V));
|
||||
|
||||
memmove(_list_->buffer + from, _list_->buffer + to + 1, (_list_->count - to - 1) * sizeof(V));
|
||||
|
||||
memset(_list_->buffer + _list_->count - length, 0, length * sizeof(V));
|
||||
|
||||
_list_->count -= length;
|
||||
result->count = length;
|
||||
|
||||
_list_->flag = CMC_FLAG_OK;
|
||||
|
||||
CMC_CALLBACKS_CALL(_list_);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* CMC_EXT_SEQ */
|
||||
|
||||
/**
|
||||
* STR
|
||||
*
|
||||
* Print helper functions.
|
||||
*/
|
||||
#ifdef CMC_EXT_STR
|
||||
|
||||
bool CMC_(PFX, _to_string)(struct SNAME *_list_, FILE *fptr)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
struct SNAME *l_ = _list_;
|
||||
|
||||
return 0 <= fprintf(fptr,
|
||||
"struct %s<%s> "
|
||||
"at %p { "
|
||||
"buffer:%p, "
|
||||
"capacity:%" PRIuMAX ", "
|
||||
"count:%" PRIuMAX ", "
|
||||
"flag:%d, "
|
||||
"f_val:%p, "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }",
|
||||
CMC_TO_STRING(SNAME), CMC_TO_STRING(V), l_, l_->buffer,
|
||||
#ifdef CMC_SAC
|
||||
(size_t)SIZE
|
||||
#else
|
||||
l_->capacity
|
||||
#endif
|
||||
,
|
||||
l_->count, l_->flag, l_->f_val, l_->alloc, CMC_CALLBACKS_GET(l_));
|
||||
}
|
||||
|
||||
bool CMC_(PFX, _print)(struct SNAME *_list_, FILE *fptr, const char *start, const char *separator, const char *end)
|
||||
{
|
||||
#ifdef CMC_DEV
|
||||
CMC_DEV_FCALL;
|
||||
#endif
|
||||
|
||||
fprintf(fptr, "%s", start);
|
||||
|
||||
for (size_t i = 0; i < _list_->count; i++)
|
||||
{
|
||||
if (!_list_->f_val->str(fptr, _list_->buffer[i]))
|
||||
return false;
|
||||
|
||||
if (i + 1 < _list_->count)
|
||||
fprintf(fptr, "%s", separator);
|
||||
}
|
||||
|
||||
fprintf(fptr, "%s", end);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif /* CMC_EXT_STR */
|
||||
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Copyright (c) 2019 Leonardo Vencovsky
|
||||
*
|
||||
* This file is part of the C Macro Collections Library.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* INIT
|
||||
*
|
||||
* The part 'INIT' gives a new way of initializing a collection. The collection
|
||||
* struct is not heap allocated, only its internal structure (nodes, buffers).
|
||||
*/
|
||||
#ifdef CMC_EXT_INIT
|
||||
|
||||
#ifdef CMC_SAC
|
||||
struct SNAME CMC_(PFX, _init)(struct CMC_DEF_FVAL(SNAME) * f_val);
|
||||
struct SNAME CMC_(PFX, _init_custom)(struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc,
|
||||
CMC_CALLBACK_TYPE callbacks);
|
||||
#else
|
||||
struct SNAME CMC_(PFX, _init)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val);
|
||||
struct SNAME CMC_(PFX, _init_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc,
|
||||
CMC_CALLBACK_TYPE callbacks);
|
||||
#endif
|
||||
void CMC_(PFX, _release)(struct SNAME _list_);
|
||||
|
||||
#endif /* CMC_EXT_INIT */
|
||||
|
||||
/**
|
||||
* ITER
|
||||
*
|
||||
* List bi-directional iterator.
|
||||
*/
|
||||
#ifdef CMC_EXT_ITER
|
||||
|
||||
/* Iterator Initialization */
|
||||
struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_start)(struct SNAME *target);
|
||||
struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_end)(struct SNAME *target);
|
||||
/* Iterator State */
|
||||
bool CMC_(PFX, _iter_at_start)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
bool CMC_(PFX, _iter_at_end)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
/* Iterator Movement */
|
||||
bool CMC_(PFX, _iter_to_start)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
bool CMC_(PFX, _iter_to_end)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
bool CMC_(PFX, _iter_next)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
bool CMC_(PFX, _iter_prev)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
bool CMC_(PFX, _iter_advance)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps);
|
||||
bool CMC_(PFX, _iter_rewind)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps);
|
||||
bool CMC_(PFX, _iter_go_to)(struct CMC_DEF_ITER(SNAME) * iter, size_t index);
|
||||
/* Iterator Access */
|
||||
V CMC_(PFX, _iter_value)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
V *CMC_(PFX, _iter_rvalue)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter);
|
||||
|
||||
#endif /* CMC_EXT_ITER */
|
||||
|
||||
/**
|
||||
* SEQ
|
||||
*
|
||||
* Push and pop sequence of items.
|
||||
*/
|
||||
#ifdef CMC_EXT_SEQ
|
||||
|
||||
/* List Sequence Input and Output */
|
||||
bool CMC_(PFX, _seq_push_front)(struct SNAME *_list_, V *values, size_t size);
|
||||
bool CMC_(PFX, _seq_push_at)(struct SNAME *_list_, V *values, size_t size, size_t index);
|
||||
bool CMC_(PFX, _seq_push_back)(struct SNAME *_list_, V *values, size_t size);
|
||||
bool CMC_(PFX, _seq_pop_at)(struct SNAME *_list_, size_t from, size_t to);
|
||||
struct SNAME *CMC_(PFX, _seq_sublist)(struct SNAME *_list_, size_t from, size_t to);
|
||||
|
||||
#endif /* CMC_EXT_SEQ */
|
||||
|
||||
/**
|
||||
* STR
|
||||
*
|
||||
* Print helper functions.
|
||||
*/
|
||||
#ifdef CMC_EXT_STR
|
||||
|
||||
/* Debug prints the struct to fptr */
|
||||
bool CMC_(PFX, _to_string)(struct SNAME *_list_, FILE *fptr);
|
||||
/* Prints each item in the list as an array */
|
||||
bool CMC_(PFX, _print)(struct SNAME *_list_, FILE *fptr, const char *start, const char *separator, const char *end);
|
||||
|
||||
#endif /* CMC_EXT_STR */
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Copyright (c) 2019 Leonardo Vencovsky
|
||||
*
|
||||
* This file is part of the C Macro Collections Library.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* ITER
|
||||
*
|
||||
* List bi-directional iterator.
|
||||
*/
|
||||
#ifdef CMC_EXT_ITER
|
||||
|
||||
/* List Iterator */
|
||||
struct CMC_DEF_ITER(SNAME)
|
||||
{
|
||||
/* Target List */
|
||||
struct SNAME *target;
|
||||
/* Cursor's position (index) */
|
||||
size_t cursor;
|
||||
/* If the iterator has reached the start of the iteration */
|
||||
bool start;
|
||||
/* If the iterator has reached the end of the iteration */
|
||||
bool end;
|
||||
};
|
||||
|
||||
#endif /* CMC_EXT_ITER */
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright (c) 2019 Leonardo Vencovsky
|
||||
*
|
||||
* This file is part of the C Macro Collections Library.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/* Value function table */
|
||||
struct CMC_DEF_FVAL(SNAME)
|
||||
{
|
||||
/* Comparator function */
|
||||
CMC_DEF_FTAB_CMP(V);
|
||||
/* Copy function */
|
||||
CMC_DEF_FTAB_CPY(V);
|
||||
/* To string function */
|
||||
CMC_DEF_FTAB_STR(V);
|
||||
/* Free from memory function */
|
||||
CMC_DEF_FTAB_FREE(V);
|
||||
/* Hash function */
|
||||
CMC_DEF_FTAB_HASH(V);
|
||||
/* Priority function */
|
||||
CMC_DEF_FTAB_PRI(V);
|
||||
};
|
||||
|
||||
/* Collection Allocation and Deallocation */
|
||||
#ifdef CMC_SAC
|
||||
struct SNAME *CMC_(PFX, _new)(struct CMC_DEF_FVAL(SNAME) * f_val);
|
||||
struct SNAME *CMC_(PFX, _new_custom)(struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc,
|
||||
CMC_CALLBACK_TYPE callbacks);
|
||||
#else
|
||||
struct SNAME *CMC_(PFX, _new)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val);
|
||||
struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc,
|
||||
CMC_CALLBACK_TYPE callbacks);
|
||||
#endif
|
||||
void CMC_(PFX, _clear)(struct SNAME *_list_);
|
||||
void CMC_(PFX, _free)(struct SNAME *_list_);
|
||||
/* Customization of Allocation and Callbacks */
|
||||
void CMC_(PFX, _customize)(struct SNAME *_list_, CMC_ALLOC_TYPE alloc, CMC_CALLBACK_TYPE callbacks);
|
||||
/* Collection Input and Output */
|
||||
bool CMC_(PFX, _push_front)(struct SNAME *_list_, V value);
|
||||
bool CMC_(PFX, _push_at)(struct SNAME *_list_, V value, size_t index);
|
||||
bool CMC_(PFX, _push_back)(struct SNAME *_list_, V value);
|
||||
bool CMC_(PFX, _pop_front)(struct SNAME *_list_);
|
||||
bool CMC_(PFX, _pop_at)(struct SNAME *_list_, size_t index);
|
||||
bool CMC_(PFX, _pop_back)(struct SNAME *_list_);
|
||||
/* Element Access */
|
||||
V CMC_(PFX, _front)(struct SNAME *_list_);
|
||||
V CMC_(PFX, _get)(struct SNAME *_list_, size_t index);
|
||||
V *CMC_(PFX, _get_ref)(struct SNAME *_list_, size_t index);
|
||||
V CMC_(PFX, _back)(struct SNAME *_list_);
|
||||
size_t CMC_(PFX, _index_of)(struct SNAME *_list_, V value, bool from_start);
|
||||
/* Collection State */
|
||||
bool CMC_(PFX, _contains)(struct SNAME *_list_, V value);
|
||||
bool CMC_(PFX, _empty)(struct SNAME *_list_);
|
||||
bool CMC_(PFX, _full)(struct SNAME *_list_);
|
||||
size_t CMC_(PFX, _count)(struct SNAME *_list_);
|
||||
bool CMC_(PFX, _fits)(struct SNAME *_list_, size_t size);
|
||||
size_t CMC_(PFX, _capacity)(struct SNAME *_list_);
|
||||
int CMC_(PFX, _flag)(struct SNAME *_list_);
|
||||
/* Collection Utility */
|
||||
bool CMC_(PFX, _resize)(struct SNAME *_list_, size_t capacity);
|
||||
struct SNAME *CMC_(PFX, _copy_of)(struct SNAME *_list_);
|
||||
bool CMC_(PFX, _equals)(struct SNAME *_list1_, struct SNAME *_list2_);
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Copyright (c) 2019 Leonardo Vencovsky
|
||||
*
|
||||
* This file is part of the C Macro Collections Library.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
struct SNAME
|
||||
{
|
||||
#ifdef CMC_SAC
|
||||
V buffer[SIZE];
|
||||
#else
|
||||
/* Dynamic array of elements */
|
||||
V *buffer;
|
||||
/* Current array capacity */
|
||||
size_t capacity;
|
||||
#endif
|
||||
/* Current amount of elements */
|
||||
size_t count;
|
||||
/* Flags indicating errors or success */
|
||||
int flag;
|
||||
/* Value function table */
|
||||
struct CMC_DEF_FVAL(SNAME) * f_val;
|
||||
/* Custom allocation functions */
|
||||
CMC_ALLOC_TYPE alloc;
|
||||
/* Custom callback functions */
|
||||
CMC_CALLBACKS_DECL;
|
||||
};
|
||||
Reference in new issue
Block a user