blob: 4546364437b36c2858f15d85a15958096a83992f [file] [log] [blame]
/*
* *****************************************************************************
*
* Copyright 2018 Gavin D. Howard
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* *****************************************************************************
*
* Definitions for bc vectors (resizable arrays).
*
*/
#ifndef BC_VECTOR_H
#define BC_VECTOR_H
#include <stdlib.h>
#include <stdint.h>
#include <status.h>
#define BC_VEC_INVALID_IDX ((size_t) -1)
#define BC_VEC_INITIAL_CAP (1<<5)
typedef void (*BcVecFree)(void*);
typedef int (*BcVecCmp)(const void*, const void*);
typedef struct BcVec {
char *array;
size_t len;
size_t cap;
size_t size;
BcVecFree dtor;
} BcVec;
// ** Exclude start. **
BcStatus bc_vec_init(BcVec *vec, size_t esize, BcVecFree dtor);
BcStatus bc_vec_expand(BcVec *vec, size_t request);
void bc_vec_pop(BcVec *vec);
void bc_vec_npop(BcVec *vec, size_t n);
BcStatus bc_vec_push(BcVec *vec, size_t n, const void *data);
BcStatus bc_vec_pushByte(BcVec *vec, uint8_t data);
BcStatus bc_vec_pushAt(BcVec *vec, const void *data, size_t idx);
BcStatus bc_vec_setToString(BcVec *vec, size_t len, const char *str);
void* bc_vec_top(const BcVec *vec);
void* bc_vec_item(const BcVec *vec, size_t idx);
void* bc_vec_item_rev(const BcVec *vec, size_t idx);
void bc_vec_free(void *vec);
// ** Exclude end. **
typedef struct BcVecO {
BcVec vec;
BcVecCmp cmp;
} BcVecO;
// ** Exclude start. **
BcStatus bc_veco_init(BcVecO* vec, size_t esize, BcVecFree dtor, BcVecCmp cmp);
BcStatus bc_veco_insert(BcVecO* vec, const void *data, size_t *idx);
size_t bc_veco_index(const BcVecO *v, const void *data);
// ** Exclude end. **
#define bc_veco_item(v, idx) (bc_vec_item(&(v)->vec, (idx)))
#define bc_veco_free(v) (bc_vec_free(&(v)->vec))
#endif // BC_VECTOR_H