This repository has been archived on 2021-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
dynarray/src/dynarray.h

110 lines
3.6 KiB
C

/*
* This file is part of corelibs. (https://git.redxen.eu/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 cl_dynarray_err;
typedef struct cl_dynarray_t cl_dynarray_t;
struct corelibs_dynarray_interface {
// Makers
const struct {
// New - allocate new array
cl_dynarray_err (*const new)(size_t sbyte, cl_dynarray_t** ptr);
// Slicer - gets a slice of a array
cl_dynarray_err (*const slice)(const cl_dynarray_t* arr, uintmax_t pos, uintmax_t len, cl_dynarray_t** ssave);
} make;
// Free
cl_dynarray_err (*const free)(cl_dynarray_t* arr);
// Modifiers
const struct {
const struct {
// Capacity
cl_dynarray_err (*const cap)(cl_dynarray_t* ptr, uintmax_t len);
// Capacity lock (dynamic / static capacity)
cl_dynarray_err (*const lock)(cl_dynarray_t* ptr, bool cap);
} arr;
const struct {
// Appender - Adds element at end of array
cl_dynarray_err (*const app)(cl_dynarray_t* arr, uintmax_t cnt, const void* elem);
// Inserter - Adds element at position, pushing next element 1 position further
cl_dynarray_err (*const ins)(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem);
// Replacer - Replaces array item or creates it if not existent
cl_dynarray_err (*const rep)(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem);
// Remover - removes element from array
cl_dynarray_err (*const rm)(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt);
} ct;
} mod;
// Fetchers
const struct {
cl_dynarray_err (*const len)(const cl_dynarray_t* arr, uintmax_t* save);
cl_dynarray_err (*const size)(const cl_dynarray_t* arr, size_t* save);
const struct {
cl_dynarray_err (*const len)(const cl_dynarray_t* arr, uintmax_t* save);
cl_dynarray_err (*const lock)(const cl_dynarray_t* arr, bool* save);
} cap;
} get;
// Comparers
const struct {
cl_dynarray_err (*const data)(const cl_dynarray_t* a, const cl_dynarray_t* b, bool* eq);
} cmp;
// Exporter
const struct {
cl_dynarray_err (*const slice)(const cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, void* save);
} export;
// Errors - functions return them on run-time problems
const struct {
const cl_dynarray_err ok, // No error
unknown; // Unknown error
const struct {
const cl_dynarray_err alloc, // Memory allocation failed
null, // Passed a null pointer
oob; // Out of bounds modification or fetch
} mem;
const struct {
const cl_dynarray_err undef, // Undefined variable
immut, // Immutable (constant) variable
inval, // Invalid value passed
ncompat; // Incompatible comparasion
} var;
} err;
};
extern const struct corelibs_dynarray_interface cl_dynarray;
#endif /* CORELIBS_GUARD_DYNARRAY */