Skip to content

Instantly share code, notes, and snippets.

@ghoomfrog
Last active February 8, 2023 17:13
Show Gist options
  • Save ghoomfrog/c4418c0d427fc702c39a1160e79d3133 to your computer and use it in GitHub Desktop.
Save ghoomfrog/c4418c0d427fc702c39a1160e79d3133 to your computer and use it in GitHub Desktop.
My vector implementation for C.
/* MIT License
Copyright (c) 2019-2020 Unlimiter
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.
*/
#ifndef UVECTOR_H
#define UVECTOR_H
#include <stdlib.h>
#include <string.h>
/*
* To use a vector, firstly you need to declare a structure with "vdecl".
* Secondly you need to instanciate that structure.
* Then you can use all the other macros for that instance.
* */
// Type of vector length and size (v.length and v.size).
// You should change it if you need bigger or smaller vector capacity.
typedef size_t vcapacity_t;
// Whether the last resize was successful or not.
_Bool vresized = 1;
// Make a struct with a vector which items are of type `type`.
// `state` means whether memory is allocated for `ptr`.
#define vdecl(name, type) \
typedef struct name { \
char state; \
vcapacity_t length, size; \
type *ptr; \
} name
// Initialize a vector.
#define vinit(v) do { \
(v).state = 1; \
(v).length = 0; \
(v).size = sizeof(*(v).ptr); \
(v).ptr = malloc(sizeof(*(v).ptr)); \
} while(0)
// Get the first item of a vector.
#define vhead(v) ((v).ptr[0])
// Get the last item of a vector.
#define vtail(v) ((v).ptr[(v).length - 1])
// Set the size of a vector.
#define vresize(v, s) do { \
void* ptr = realloc((v).ptr, s); \
vresized = (_Bool)ptr; \
if (ptr) { \
(v).size = s; \
(v).ptr = ptr; \
if ((v).length * sizeof(*(v).ptr) > s) \
(v).length = (s) / sizeof(*(v).ptr); \
} \
} while(0)
// Get an item from a vector.
#define vget(v, i) ((v).ptr[i])
// Set an item to another in a vector.
#define vset(v, i, item) do { \
(v).ptr[i] = item; \
} while(0)
// Append an item to a vector.
#define vpush(v, item) do { \
_Bool full = 0; \
if ((v).size / sizeof(*(v).ptr) == (v).length) { \
full = 1; \
vresize(v, (v).size * 2); \
} \
if (!full || vresized) \
(v).ptr[(v).length++] = item; \
} while(0)
// Append all bytes in a string to a vector respectively.
#define vpushstr(v, s) do { \
int slen = strlen(s); \
for (int i = 0; i < slen; i++) \
vpush(v, s[i]); \
} while(0)
// Remove an item from a vector.
#define vremove(v, i) do { \
if (i >= 0 && i < (v).length) { \
for (capacity_t c = i; c < (v).length - 1; c++) \
(v).ptr[c] = (v).ptr[c + 1]; \
(v).length--; \
} \
} while(0)
// Remove the last item of a vector.
#define vremovetail(v) do { \
(v).length--; \
} while(0)
// Remove and get the last item of a vector.
#define vpop(v) ((v).length >= 0 ? (v).ptr[--(v).length] : vtail(v))
// Free the memory allocated by a vector.
#define vfree(v) do { \
if ((v).state) { \
free((v).ptr); \
(v).state = 0; \
} \
} while(0)
// Clear a vector.
#define vclear(v) do { \
(v).length = 0; \
(v).size = sizeof(*(v).ptr); \
} while(0)
// Copy a vector (`v0`) to another (`v1`).
#define vcopy(v1, v0) do { \
vclear(v1); \
for (int i = 0; i < (v0).length; i++) \
vpush(v1, vget(v0, i)); \
} while(0)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment