Browse Source

Added cvec

master
parent
commit
ece5ea6f5b
  1. 2
      LICENSE
  2. 93
      src/datastruct/cvec.c
  3. 44
      src/datastruct/cvec.h
  4. 57
      testing/test.c

2
LICENSE

@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022, 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:

93
src/datastruct/cvec.c

@ -0,0 +1,93 @@
/*
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->contents = NULL;
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/datastruct/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

57
testing/test.c

@ -12,6 +12,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stdint.h>
@ -23,6 +24,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include "../src/fs/fs.h"
#include "../src/bits/bits.h"
#include "../src/math/vector.h"
#include "../src/datastruct/cvec.h"
int test_rng() {
lcg(76);
@ -218,6 +220,53 @@ int test_math() {
return EXIT_SUCCESS;
}
int test_cvec() {
cvec vec = cvec_new(sizeof(char), 12);
cvec_put(&vec, "3");
cvec_put(&vec, "2");
cvec_put(&vec, "6");
cvec_put(&vec, "9");
if (strncmp(vec.contents, "3269", 4) != 0) {
printf("[ERROR] Initial contents do not match: expected %s; got %s\n", "3269", vec.contents);
return EXIT_FAILURE;
}
if (*cvec_at(&vec, 2) != '6') {
printf("[ERROR] Failed to get char at index %d: expected to get %d, but got %d\n", 2, '6', *cvec_at(&vec, 2));
return EXIT_FAILURE;
}
cvec_pop(&vec);
if (strncmp(vec.contents, "326", 3) != 0) {
printf("[ERROR] Failed to pop: expected the contents to be %s; got %s instead\n", "326", vec.contents);
return EXIT_FAILURE;
}
cvec_remove(&vec, 0);
if (strncmp(vec.contents, "26", 2) != 0) {
printf("[ERROR] Failed to remove element at index %d: expected contents to get: %s; got %s\n", 0, "26", vec.contents);
return EXIT_FAILURE;
}
cvec_free(&vec);
if (vec.contents != NULL) {
printf("[ERROR] Failed to free vector's contents\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int test_datastruct() {
if (test_cvec() == EXIT_FAILURE) {
printf("[ERROR] CVEC test failed\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int main() {
// rng
printf("[INFO] Testing rng...\n");
@ -267,5 +316,13 @@ int main() {
printf("[INFO] Math test passed\n\n");
}
// datastruct
printf("[INFO] Testing datastruct...\n");
if (test_datastruct() == EXIT_FAILURE) {
printf("[INFO] Datastruct test failed\n\n");
} else {
printf("[INFO] Datastruct test passed\n\n");
}
return EXIT_SUCCESS;
}
Loading…
Cancel
Save