110 lines
3.4 KiB
C
110 lines
3.4 KiB
C
|
/*
|
||
|
* This file is part of corelibs. (https://git.redxen.eu/caskd/corelibs)
|
||
|
* Copyright (c) 2021 Alex-David Denes
|
||
|
*
|
||
|
* corelibs is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* any later version.
|
||
|
*
|
||
|
* corelibs is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with corelibs. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
#include <stdbool.h> // bool
|
||
|
#include <stddef.h> // size_t
|
||
|
#include <stdint.h> // uintmax_t
|
||
|
|
||
|
#ifndef CORELIBS_GUARD_DYNARRAY
|
||
|
#define CORELIBS_GUARD_DYNARRAY
|
||
|
|
||
|
typedef signed short int dynarray_err;
|
||
|
typedef struct dynarray_t dynarray_t;
|
||
|
|
||
|
struct corelibs_dynarray_interface {
|
||
|
|
||
|
// Makers
|
||
|
const struct {
|
||
|
// New - allocate new array
|
||
|
dynarray_err (*const new)(size_t sbyte, dynarray_t** ptr);
|
||
|
// Slicer - gets a slice of a array
|
||
|
dynarray_err (*const slice)(const dynarray_t* arr, uintmax_t pos, uintmax_t len, dynarray_t** ssave);
|
||
|
} make;
|
||
|
|
||
|
// Free
|
||
|
dynarray_err (*const free)(dynarray_t* arr);
|
||
|
|
||
|
// Modifiers
|
||
|
const struct {
|
||
|
|
||
|
const struct {
|
||
|
// Capacity
|
||
|
dynarray_err (*const cap)(dynarray_t* ptr, uintmax_t len);
|
||
|
// Capacity lock (dynamic / static capacity)
|
||
|
dynarray_err (*const lock)(dynarray_t* ptr, bool cap);
|
||
|
} arr;
|
||
|
|
||
|
const struct {
|
||
|
// Appender - Adds element at end of array
|
||
|
dynarray_err (*const app)(dynarray_t* arr, uintmax_t cnt, const void* elem);
|
||
|
// Inserter - Adds element at position, pushing next element 1 position further
|
||
|
dynarray_err (*const ins)(dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem);
|
||
|
// Replacer - Replaces array item or creates it if not existent
|
||
|
dynarray_err (*const rep)(dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem);
|
||
|
// Remover - removes element from array
|
||
|
dynarray_err (*const rm)(dynarray_t* arr, uintmax_t pos, uintmax_t cnt);
|
||
|
} ct;
|
||
|
|
||
|
} mod;
|
||
|
|
||
|
// Fetchers
|
||
|
const struct {
|
||
|
dynarray_err (*const len)(const dynarray_t* arr, uintmax_t* save);
|
||
|
dynarray_err (*const size)(const dynarray_t* arr, size_t* save);
|
||
|
const struct {
|
||
|
dynarray_err (*const len)(const dynarray_t* arr, uintmax_t* save);
|
||
|
dynarray_err (*const lock)(const dynarray_t* arr, bool* save);
|
||
|
} cap;
|
||
|
} get;
|
||
|
|
||
|
// Comparers
|
||
|
const struct {
|
||
|
dynarray_err (*const data)(const dynarray_t* a, const dynarray_t* b, bool* eq);
|
||
|
} cmp;
|
||
|
|
||
|
// Exporter
|
||
|
const struct {
|
||
|
dynarray_err (*const slice)(const dynarray_t* arr, uintmax_t pos, uintmax_t cnt, void* save);
|
||
|
} export;
|
||
|
|
||
|
// Errors - functions return them on run-time problems
|
||
|
const struct {
|
||
|
|
||
|
const dynarray_err ok, // No error
|
||
|
unknown; // Unknown error
|
||
|
|
||
|
const struct {
|
||
|
const dynarray_err alloc, // Memory allocation failed
|
||
|
null, // Passed a null pointer
|
||
|
oob; // Out of bounds modification or fetch
|
||
|
} mem;
|
||
|
|
||
|
const struct {
|
||
|
const dynarray_err undef, // Undefined variable
|
||
|
immut, // Immutable (constant) variable
|
||
|
inval, // Invalid value passed
|
||
|
ncompat; // Incompatible comparasion
|
||
|
} var;
|
||
|
|
||
|
} err;
|
||
|
};
|
||
|
extern const struct corelibs_dynarray_interface dynarray;
|
||
|
|
||
|
#endif /* CORELIBS_GUARD_DYNARRAY */
|
||
|
|