Created
September 19, 2018 10:05
-
-
Save ElectricCoffee/ecfdd321c19a39383770d23ca5fbb4bb to your computer and use it in GitHub Desktop.
A simple "for the hell of it" implementation of a dynamically resizing array in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// i32_vec.c | |
// vector_idea | |
// | |
// Created by Nikolaj Lepka on 2018-09-19. | |
// Copyright © 2018 wausoft.eu. All rights reserved. | |
// | |
#include "i32_vec.h" | |
/** | |
* Initialises a new vector with a set capacity | |
* returns a fresh vector | |
*/ | |
struct i32_vec init_with_capacity(int cap) { | |
struct i32_vec vect; | |
vect.capacity = cap; | |
vect.head = 0; | |
vect.vec = calloc(cap, sizeof(int)); | |
return vect; | |
} | |
/** | |
* Initialises a new vector with capacity 1 | |
* returns a fresh vector | |
*/ | |
struct i32_vec init(void) { | |
return init_with_capacity(1); | |
} | |
/** UNSAFE -- MAY GO OUT OF BOUNDS!! | |
* gets the value at the index | |
*/ | |
int get(struct i32_vec *vect, size_t index) { | |
return vect->vec[index]; | |
} | |
/** UNSAFE -- MAY GO OUT OF BOUNDS!! | |
* sets a value to a given index | |
*/ | |
void set(struct i32_vec *vect, size_t index, int data) { | |
vect->vec[index] = data; | |
} | |
/** | |
* Pushes a new element onto the vector | |
* Increases the capacity of the vector if pushing goes out of bounds | |
* Returns false if reallocation fails | |
*/ | |
bool push(struct i32_vec *vect, int value) { | |
/* expand memory if capacity gets exceeded */ | |
if (vect->head + 1 == vect->capacity) { | |
vect->vec = realloc(vect->vec, sizeof *(vect->vec) + 1); | |
if (!vect->vec) { | |
return false; | |
} | |
vect->capacity++; | |
} | |
set(vect, vect->head, value); | |
vect->head++; | |
return true; | |
} | |
/** | |
* Pops an element from the vector, and moves the head back | |
*/ | |
int pop(struct i32_vec *vect) { | |
int result = peek(vect); | |
if (vect->head - 1 > 0) { | |
vect->head--; | |
} | |
return result; | |
} | |
/** | |
* Gets the element at the head | |
*/ | |
int peek(struct i32_vec *vect) { | |
return get(vect, vect->head); | |
} | |
/** | |
* Converts an array to a vector. | |
* Takes the array's capacity, and a pointer to the array | |
*/ | |
struct i32_vec from_array(int capacity, int *array) { | |
struct i32_vec vec = init_with_capacity(capacity); | |
int i; | |
for (i = 0; i < capacity; i++) { | |
push(&vec, array[i]); | |
} | |
return vec; | |
} | |
void destroy(struct i32_vec *vect) { | |
vect->head = 0; | |
vect->capacity = 0; | |
free(vect->vec); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// i32_vec.h | |
// vector_idea | |
// | |
// Created by Nikolaj Lepka on 2018-09-19. | |
// Copyright © 2018 wausoft.eu. All rights reserved. | |
// | |
#ifndef i32_vec_h | |
#define i32_vec_h | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <stdlib.h> | |
struct i32_vec { | |
unsigned int capacity; | |
size_t head; | |
int *vec; | |
}; | |
struct i32_vec init_with_capacity(int); | |
struct i32_vec init(void); | |
int get(struct i32_vec*, size_t); | |
void set(struct i32_vec*, size_t, int); | |
bool push(struct i32_vec*, int); | |
int pop(struct i32_vec*); | |
int peek(struct i32_vec*); | |
struct i32_vec from_array(int, int*); | |
void destroy(struct i32_vec*); | |
#endif /* i32_vec_h */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// vector_idea | |
// | |
// Created by Nikolaj Lepka on 2018-09-18. | |
// Copyright © 2018 wausoft.eu. All rights reserved. | |
// | |
#include <stdio.h> | |
#include "i32_vec.h" | |
void print_vec(struct i32_vec*); | |
int main(int argc, const char * argv[]) { | |
int arr[8] = {1, 2, 3, 4, 5, 6, 7, 8}; | |
struct i32_vec vec = from_array(8, arr); | |
print_vec(&vec); | |
pop(&vec); /* pop 8 */ | |
pop(&vec); /* pop 7 */ | |
push(&vec, 3); | |
print_vec(&vec); | |
destroy(&vec); /* deallocate everything */ | |
return 0; | |
} | |
void print_vec(struct i32_vec *vec) { | |
int i; | |
printf("capacity: %d, head index: %zu\n", vec->capacity, vec->head); | |
for (i = 0; i < vec->head; i++) { | |
int data = get(vec, i); | |
printf("data: %d\n", data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment