forked from ranjanvittal/LinearGraphs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynarray.cc
45 lines (35 loc) · 809 Bytes
/
dynarray.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#define INITIAL_CAPACITY 1
#define DEBUG
#include <stdio.h>
#include <stdlib.h>
#include "dynarray.h"
void Cry(int condition,const char* error = ""){
if (condition){
fprintf (stderr, "ERROR: %s", error);
exit(-1);
}
}
void Vector::Initialize(int h){
capacity = INITIAL_CAPACITY;
cellsize = h;
length = 0;
data = NULL;
AllocateData();
}
//default count = 1
int Vector::Extend (int count){
if ((capacity - length) < count){
while (capacity < length + count){
capacity *= 2;
}
AllocateData();
}
length += count;
return length;
}
void Vector::AllocateData(){
data = realloc (data, cellsize * capacity);
//#ifdef DEBUG
Cry(data == NULL, "Realloc Error in vector");
//#endif
}