Browse Source

Initial commit

master
commit
934516a7f5
  1. 3
      .gitignore
  2. 9
      LICENSE
  3. 8
      Makefile
  4. 26
      README.md
  5. 92
      src/cvec.c
  6. 44
      src/cvec.h
  7. 29
      testing/test.c

3
.gitignore vendored

@ -0,0 +1,3 @@
cvec.a
cvec.o
test

9
LICENSE

@ -0,0 +1,9 @@
The MIT License (MIT)
Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
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.

8
Makefile

@ -0,0 +1,8 @@
lib:
gcc -Wall -Werror src/cvec.c -c && ar rcs cvec.a cvec.o
clean:
rm -f test cvec.o cvec.a
test: clean lib
gcc -Wall -Werror -O2 testing/test.c cvec.a -o test

26
README.md

@ -0,0 +1,26 @@
# CVEC - C vector
### "Reinventing the bicycle"-class C vector library
## Usage
The values are stored internally in vector's `contents` as `char`s so any types fit, but only one type per vector should be used. It is your responsibility to not use different types in one vector.
The overall usage should be done via shipped cvec_* functions, but in case of the need for some pointer math magic, all necessary fields are there for your usage.
This snippet should give you the general idea of usage.
```
cvec vec = cvec_new(sizeof(size_t), 1);
size_t val = 0x15dc;
cvec_put(&vec, &val);
printf("sizeof(size_t) -> %zu\n", sizeof(size_t));
printf("%s; size %zu\n", vec.contents, vec.size);
val = 0x02ec;
cvec_put(&vec, &val);
printf("After realloc: %s; size: %zu\n", vec.contents, vec.size);
printf("At index %d: %zx\n", 1, *(size_t*) cvec_at(&vec, 1));
cvec_free(&vec);
```
## License
MIT

92
src/cvec.c

@ -0,0 +1,92 @@
/*
The MIT License (MIT)
Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
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.
*/
#include "cvec.h"
#include <string.h>
// Create new vector with the maximum data elements of cap where data is data_size bytes long
cvec cvec_new(size_t data_size, size_t cap) {
return (cvec){
.cap = cap,
.size = 0,
.data_size = data_size,
.contents = (char*) malloc(data_size * cap),
};
}
// Deallocate vector's contents
void cvec_free(cvec* vec) {
if (vec) {
free(vec->contents);
vec = NULL;
}
}
// Put data at the end of the vector
void cvec_put(cvec* vec, const void* data) {
if (!vec || !data) {
return;
}
if (vec->size == vec->cap) {
vec->contents = realloc(vec->contents, vec->cap + vec->data_size);
}
for (size_t i = 0; i < vec->data_size; i++) {
vec->contents[vec->size] = ((char*) data)[i];
vec->size++;
}
}
// Get data at the specified index. Returns NULL if out of bounds
char* cvec_at(cvec* vec, size_t index) {
if (!vec) {
return NULL;
}
size_t real_index = vec->data_size * index;
if (real_index >= vec->size) {
return NULL;
}
return (vec->contents+real_index);
}
// Pop the last data element
void cvec_pop(cvec* vec) {
if (!vec) {
return;
}
if (vec->size > 0) {
vec->contents[vec->data_size*(vec->size-1)] = '\0';
vec->size--;
}
}
// Remove data element at specified index and shift data to left if necessary
void cvec_remove(cvec* vec, size_t index) {
if (!vec) {
return;
}
if (vec->size > 0) {
vec->contents[vec->data_size*index] = '\0';
if (vec->size > 0 && index < vec->size) {
// shift to left
memmove(&vec->contents[index-1], &vec->contents[index], vec->size-index);
for (size_t i = vec->size-index-1; i < vec->size; i++) {
vec->contents[i] = '\0';
}
}
vec->size--;
}
}

44
src/cvec.h

@ -0,0 +1,44 @@
/*
The MIT License (MIT)
Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
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 CVEC_H
#define CVEC_H
#include <stdlib.h>
// Basic vector structure
typedef struct cvec {
size_t cap;
size_t size;
size_t data_size;
char* contents;
} cvec;
// Create new vector with the maximum data elements of cap where data is data_size bytes long
cvec cvec_new(size_t data_size, size_t cap);
// Deallocate vector's contents
void cvec_free(cvec* vec);
// Put data at the end of the vector
void cvec_put(cvec* vec, const void* data);
// Get data at the specified index. Returns NULL if out of bounds
char* cvec_at(cvec* vec, size_t index);
// Pop the last data element
void cvec_pop(cvec* vec);
// Remove data element at specified index and shift data to left if necessary
void cvec_remove(cvec* vec, size_t index);
#endif

29
testing/test.c

@ -0,0 +1,29 @@
#include "../src/cvec.h"
#include <stdio.h>
int main() {
cvec vec = cvec_new(sizeof(char), 12);
cvec_put(&vec, "3");
cvec_put(&vec, "2");
cvec_put(&vec, "6");
cvec_put(&vec, "9");
printf("Initial contents: %s\n", vec.contents);
printf("Byte at %d -> %d\n", 2, *cvec_at(&vec, 2));
cvec_pop(&vec);
printf("After pop: %s\n", vec.contents);
printf("Byte at %d after pop -> %d\n", 2, *cvec_at(&vec, 2));
cvec_remove(&vec, 0);
printf("%s after remove on %d\n", vec.contents, 0);
cvec_free(&vec);
vec = cvec_new(sizeof(size_t), 1);
size_t val = (size_t) 0x15dc;
cvec_put(&vec, &val);
printf("sizeof(size_t) -> %zu\n", sizeof(size_t));
printf("%s; size %zu\n", vec.contents, vec.size);
val = 0x02ec;
cvec_put(&vec, &val);
printf("After realloc: %s; size: %zu\n", vec.contents, vec.size);
printf("At index %d: %zx\n", 1, *(size_t*) cvec_at(&vec, 1));
cvec_free(&vec);
}
Loading…
Cancel
Save