Simple C Variable Size Vector
 All Data Structures Files Functions Variables Typedefs Pages
vector.c
Go to the documentation of this file.
1 /*
2 Simple C variable size vector, a simple variable size vector written in C
3  Copyright (C) 2016-2017 Francisco Anderson Bezerra Rodrigues
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include"vector.h"
19 #include<stdio.h>
20 #include<stdlib.h>
21 #include<string.h>
22 
23 Vector* NewVector(unsigned long int elementSize)
24 {
25  Vector *ret= malloc(sizeof(Vector));
26  if(NULL == ret)
27  {
28  fprintf(stderr, "[ERROR] Allocation Error\n");
29  return NULL;
30  }
31  ret->elementSize= elementSize;
32  ret->numberOfElements=0;
33  ret->capacity= 0;
34  ret->elements= NULL;
35  return ret;
36 }
37 
38 static void Resize(Vector * vec)
39 {
40  vec->capacity= 2 * (vec->capacity) + 1;
41  vec->elements= realloc(vec->elements, (vec->capacity) * (vec->elementSize));
42  if(NULL == vec-> elements)
43  {
44  fprintf(stderr, "[ERROR] Memory reallocation error\n");
45  exit(1);
46  }
47 }
48 
50 {
51  if(vec->numberOfElements == vec->capacity)
52  {
53  Resize(vec);
54  }
55  (vec->numberOfElements)++;
56  return (vec->numberOfElements)-1;
57 }
58 
59 int VectorAppendCopy(Vector *vec, void* element)
60 {
61  VectorAllocateOne(vec);
62  char* newElementArea= ( ( char*) vec->elements) + (vec->elementSize)*( (vec->numberOfElements)-1);
63  memcpy(newElementArea, element, vec->elementSize);
64  return (vec->numberOfElements)-1;
65 }
66 
67 void* VectorGetElement(Vector *vec, int position)
68 {
69 /* while(position > capacity)
70  {
71  Resize(vec);
72  }
73 */
74  if( (vec->numberOfElements) <= position)
75  {
76  fprintf(stderr, "[ERROR] Bad element access\n");
77  return NULL;
78  }
79  return ((char*)vec->elements)+ ( position*(vec->elementSize) );
80 }
81 
83 {
84  free(vec->elements);
85  free(vec);
86  return NULL;
87 }
The vector data structre.
Definition: vector.h:30
unsigned long int elementSize
Definition: vector.h:33
int numberOfElements
Definition: vector.h:34
Vector * NewVector(unsigned long int elementSize)
Create a new vector.
Definition: vector.c:23
int VectorAppendCopy(Vector *vec, void *element)
Inserts a new element at the end of the vector.
Definition: vector.c:59
int capacity
Definition: vector.h:35
Vector * DeleteVector(Vector *vec)
Delete the vector and its elements.
Definition: vector.c:82
void * elements
Definition: vector.h:32
int VectorAllocateOne(Vector *vec)
Allocates space for at least one more element.
Definition: vector.c:49
void * VectorGetElement(Vector *vec, int position)
Gets the element at informed position.
Definition: vector.c:67