Skip to content

Instantly share code, notes, and snippets.

@cs-fedy
Last active September 8, 2020 10:00
Show Gist options
  • Save cs-fedy/aa054d434672959a3042bd4e6fe591b8 to your computer and use it in GitHub Desktop.
Save cs-fedy/aa054d434672959a3042bd4e6fe591b8 to your computer and use it in GitHub Desktop.
vector implementation in C programming language.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct {
int size;
int capacity;
int *data;
} Vector;
Vector * init(int capacity);
void Append(Vector *, int key);
void Prepend(Vector *, int key);
void DeleteAtIndex(Vector *, int index);
void DeleteKey(Vector *, int key);
void Pop(Vector *);
int Find(Vector, int key);
void Resize(Vector *, int newSize);
int GetSize(Vector);
int GetCapacity(Vector);
unsigned IsEmpty(Vector);
unsigned IsFull(Vector);
Vector * init(int capacity) {
Vector * vt = malloc(sizeof(Vector));
vt -> size = 0;
vt -> capacity = 0;
vt -> data = malloc(capacity * sizeof(int));
if (vt -> data != NULL) {
vt -> capacity = capacity;
}
return vt;
}
void allocateNewMemoryCase(Vector * vt) {
int * tmp_arr = NULL;
vt -> capacity++;
// fix realloc: invalid next size.
tmp_arr = realloc(vt -> data, sizeof(int) * vt -> capacity);
if (tmp_arr != NULL) {
vt -> data = tmp_arr;
} else {
perror("Error while reallocating vt -> items!!");
exit(EXIT_FAILURE);
}
}
void Append(Vector * vt, int key) {
if (vt -> data == NULL) {
perror("arr -> items is NULL!!");
exit(EXIT_FAILURE);
}
if (IsFull(*vt)) {
allocateNewMemoryCase(vt);
}
vt -> data[vt -> size++] = key;
}
void shiftRight(Vector * vt, int start) {
int index;
for (index = start; index >= 1; index--) {
vt -> data[index] = vt -> data[index - 1];
}
}
void Prepend(Vector * vt, int key) {
if (vt -> data == NULL) {
perror("arr -> items is NULL!!");
exit(EXIT_FAILURE);
}
if (IsFull(*vt)) {
allocateNewMemoryCase(vt);
}
shiftRight(vt, vt -> size);
vt -> data[0] = key;
vt -> size++;
}
void shiftLeft(Vector * vt, int start) {
int index;
for (index = start; index < GetSize(*vt); index++) {
vt -> data[index - 1] = vt -> data[index];
}
vt -> data = realloc(vt -> data, (vt -> size -1) * sizeof(int));
}
void DeleteAtIndex(Vector * vt, int index) {
if (index >= vt -> size) {
perror("index out of range!!");
exit(EXIT_FAILURE);
}
shiftLeft(vt, index + 1);
vt -> size--;
vt -> capacity--;
}
void DeleteKey(Vector * vt, int key) {
int keyIndex = Find(*vt, key);
assert(keyIndex > -1);
DeleteAtIndex(vt, keyIndex);
}
void Pop(Vector * vt) {
assert(!IsEmpty(*vt));
vt -> data = realloc(vt -> data, (--vt -> size) * sizeof(int));
vt -> capacity--;
}
int Find(Vector vt, int key) {
int index;
for (index = 0; index < GetSize(vt); index++) {
if (vt.data[index] == key) {
return index;
}
}
return -1;
}
void Resize(Vector * vt, int newSize) {
int index, size = GetSize(*vt);
if (newSize < size) {
for (index = 0; index < size - newSize; index++) {
Pop(vt);
}
vt -> size = newSize;
} else {
perror("new size is greeter than vt's data size!!");
exit(EXIT_FAILURE);
}
}
int GetSize(Vector vt) {
return vt.size;
}
int GetCapacity(Vector vt) {
return vt.capacity;
}
unsigned IsEmpty(Vector vt) {
return GetSize(vt) == 0;
}
unsigned IsFull(Vector vt) {
return GetSize(vt) == GetCapacity(vt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment