2018-01-05 21:17:55 +00:00
|
|
|
/*
|
2020-01-12 16:50:37 +00:00
|
|
|
* Copyright (c) 2020 Calvin Rose
|
2018-01-05 21:17:55 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifndef JANET_VECTOR_H_defined
|
|
|
|
#define JANET_VECTOR_H_defined
|
2018-01-05 21:17:55 +00:00
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/*
|
2018-01-06 16:09:15 +00:00
|
|
|
* vector code modified from
|
2018-01-05 21:17:55 +00:00
|
|
|
* https://github.com/nothings/stb/blob/master/stretchy_buffer.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This is mainly used code such as the assembler or compiler, which
|
2019-06-02 03:31:39 +00:00
|
|
|
* need vector like data structures that are only garbage collected in case
|
|
|
|
* of an error, and normally rely on malloc/free. */
|
2018-01-05 21:17:55 +00:00
|
|
|
|
2019-06-02 03:31:39 +00:00
|
|
|
#define janet_v_free(v) (((v) != NULL) ? (janet_sfree(janet_v__raw(v)), 0) : 0)
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_v_push(v, x) (janet_v__maybegrow(v, 1), (v)[janet_v__cnt(v)++] = (x))
|
|
|
|
#define janet_v_pop(v) (janet_v_count(v) ? janet_v__cnt(v)-- : 0)
|
|
|
|
#define janet_v_count(v) (((v) != NULL) ? janet_v__cnt(v) : 0)
|
|
|
|
#define janet_v_last(v) ((v)[janet_v__cnt(v) - 1])
|
|
|
|
#define janet_v_empty(v) (((v) != NULL) ? (janet_v__cnt(v) = 0) : 0)
|
|
|
|
#define janet_v_flatten(v) (janet_v_flattenmem((v), sizeof(*(v))))
|
2018-01-05 21:17:55 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_v__raw(v) ((int32_t *)(v) - 2)
|
|
|
|
#define janet_v__cap(v) janet_v__raw(v)[0]
|
|
|
|
#define janet_v__cnt(v) janet_v__raw(v)[1]
|
2018-01-05 21:17:55 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_v__needgrow(v, n) ((v) == NULL || janet_v__cnt(v) + (n) >= janet_v__cap(v))
|
|
|
|
#define janet_v__maybegrow(v, n) (janet_v__needgrow((v), (n)) ? janet_v__grow((v), (n)) : 0)
|
|
|
|
#define janet_v__grow(v, n) ((v) = janet_v_grow((v), (n), sizeof(*(v))))
|
2018-01-05 21:17:55 +00:00
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
/* Actual functions defined in vector.c */
|
2018-09-06 02:18:42 +00:00
|
|
|
void *janet_v_grow(void *v, int32_t increment, int32_t itemsize);
|
|
|
|
void *janet_v_flattenmem(void *v, int32_t itemsize);
|
2018-01-05 21:17:55 +00:00
|
|
|
|
|
|
|
#endif
|