Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
70 lines (58 sloc) 1.16 KB
// svec.c
// Author: Nat Tuck
// 3650F2017, Challenge01 Hints
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "svec.h"
// Print for use with token printings
void
dyArray_print_with_interrupt(svec* arr) {
for (int i = 0; i < arr->size; i++) {
printf("%s\n", arr->data[i]);
}
}
svec*
make_svec()
{
svec* sv = malloc(sizeof(svec));
sv->size = 0;
sv->cap = 4;
sv->data = malloc(4 * sizeof(char*));
memset(sv->data, 0, 4 * sizeof(char*));
return sv;
}
void
free_svec(svec* sv)
{
for (int ii = 0; ii < sv->size; ++ii) {
if (sv->data[ii] != 0) {
free(sv->data[ii]);
}
}
free(sv->data);
free(sv);
}
char*
svec_get(svec* sv, int ii)
{
assert(ii >= 0 && ii < sv->size);
return sv->data[ii];
}
void
svec_put(svec* sv, int ii, char* item)
{
assert(ii >= 0 && ii < sv->size);
sv->data[ii] = strdup(item);
}
void svec_push_back(svec* sv, char* item)
{
int ii = sv->size;
if (ii >= sv->cap) {
sv->cap *= 2;
sv->data = (char**) realloc(sv->data, sv->cap * sizeof(char*));
}
sv->size = ii + 1;
svec_put(sv, ii, item);
}
You can’t perform that action at this time.