Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
vector.c File Reference

This is a vector implemenation in C. More...

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
Include dependency graph for vector.c:

Data Structures

struct  Vector
 for IO operations More...
 

Functions

void init (Vector *vec, int val)
 This function initilaizes the vector and gives it a size of 1 and initializes the first index to 0.
 
void delete (Vector *vec)
 This function clears the heap memory allocated by the Vector.
 
void clear (Vector *vec)
 This function clears the contents of the Vector.
 
int len (Vector *vec)
 This function returns the length the Vector.
 
void push (Vector *vec, int val)
 This function pushes a value to the end of the Vector.
 
int get (Vector *vec, int index)
 This function get the item at the specified index of the Vector.
 
void set (Vector *vec, int index, int val)
 This function sets an item at the specified index of the Vector.
 
int next (Vector *vec)
 This function gets the next item from the Vector each time it's called.
 
void * begin (Vector *vec)
 This function returns the pointer to the begining of the Vector.
 
void print (Vector *vec)
 This function prints the entire Vector as a list.
 
static void test ()
 This function tests the functions used to work with Vectors.
 
int main ()
 Main function.
 

Detailed Description

This is a vector implemenation in C.

A vector is an expandable array.

This vector implementation in C comes with some wrapper functions that lets the user work with data without having to worrying about memory.

Function Documentation

◆ begin()

void * begin ( Vector vec)

This function returns the pointer to the begining of the Vector.

@params Vector* (a pointer to the Vector struct)

Returns
: void*
116 {
117 return (void*)vec->contents;
118}
int * contents
the internal array itself
Definition vector.c:15

◆ clear()

void clear ( Vector vec)

This function clears the contents of the Vector.

@params Vector* (a pointer to the Vector struct)

Returns
: none
46 {
47 delete(vec);
48 init(vec, 0);
49}
void init(Vector *vec, int val)
This function initilaizes the vector and gives it a size of 1 and initializes the first index to 0.
Definition vector.c:25
Here is the call graph for this function:

◆ delete()

void delete ( Vector vec)

This function clears the heap memory allocated by the Vector.

@params Vector* (a pointer to the Vector struct)

Returns
: none
37 {
38 free(vec->contents);
39}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26

◆ get()

int get ( Vector vec,
int  index 
)

This function get the item at the specified index of the Vector.

@params Vector* (a pointer to the Vector struct) @params int (the index to get value from)

Returns
: int
78 {
79 if(index < vec->len) {
80 return vec->contents[index];
81 }
82 return -1;
83}

◆ init()

void init ( Vector vec,
int  val 
)

This function initilaizes the vector and gives it a size of 1 and initializes the first index to 0.

@params Vector* (a pointer to the Vector struct) @params int (the actual data to be passed to the vector)

Returns
none
25 {
26 vec->contents = (int*)malloc(sizeof(int));
27 vec->contents[0] = val;
28 vec->current = 0;
29 vec->len = 1;
30}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
int len
contains the length of the vector
Definition vector.c:13
int current
holds the current item
Definition vector.c:14

◆ len()

int len ( Vector vec)

This function returns the length the Vector.

@params Vector* (a pointer to the Vector struct)

Returns
: int
56 {
57 return vec->len;
58}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
156 {
157 test();
158
159 Vector vec;
160 init(&vec, 10);
161 push(&vec, 20);
162 print(&vec);
163 set(&vec, 0, 11);
164 set(&vec, 1, 22);
165 print(&vec);
166 printf("Length: %d\n", len(&vec));
167 return 0;
168}
void push(struct Stack *p, char ch)
push function
Definition infix_to_postfix.c:55
for IO operations
Definition vector.c:12
void set(Vector *vec, int index, int val)
This function sets an item at the specified index of the Vector.
Definition vector.c:91
static void test()
This function tests the functions used to work with Vectors.
Definition vector.c:138
void print(Vector *vec)
This function prints the entire Vector as a list.
Definition vector.c:125
Here is the call graph for this function:

◆ next()

int next ( Vector vec)

This function gets the next item from the Vector each time it's called.

@params Vector* (a pointer to the Vector struct)

Returns
: int
102 {
103 if(vec->current == vec->len) {
104 vec->current = 0;
105 }
106 int current_val = vec->contents[vec->current];
107 vec->current++;
108 return current_val;
109}

◆ print()

void print ( Vector vec)

This function prints the entire Vector as a list.

@params Vector* (a pointer to the Vector struct)

Returns
: none
125 {
126 int size = vec->len;
127 printf("[ ");
128 for(int count = 0; count < size; count++) {
129 printf("%d ", vec->contents[count]);
130 }
131 printf("]\n");
132}

◆ push()

void push ( Vector vec,
int  val 
)

This function pushes a value to the end of the Vector.

@params Vector* (a pointer to the Vector struct) @params int (the value to be pushed)

Returns
: none
66 {
67 vec->contents = realloc(vec->contents, (sizeof(int) * (vec->len + 1)));
68 vec->contents[vec->len] = val;
69 vec->len++;
70}
Here is the call graph for this function:

◆ set()

void set ( Vector vec,
int  index,
int  val 
)

This function sets an item at the specified index of the Vector.

@params Vector* (a pointer to the Vector struct) @params int (the index to set value at)

Returns
: none
91 {
92 if(index < vec->len) {
93 vec->contents[index] = val;
94 }
95}

◆ test()

static void test ( void  )
static

This function tests the functions used to work with Vectors.

Returns
: none
138 {
139 Vector vec;
140 init(&vec, 10);
141 assert(get(&vec, 0) == 10);
142 push(&vec, 20);
143 assert(get(&vec, 1) == 20);
144 set(&vec, 0, 11);
145 assert(get(&vec, 0) == 11);
146 assert(next(&vec) == 11);
147 set(&vec, 1, 22);
148 assert(get(&vec, 1) == 22);
149 assert(len(&vec) == 2);
150}
int get(Vector *vec, int index)
This function get the item at the specified index of the Vector.
Definition vector.c:78
int next(Vector *vec)
This function gets the next item from the Vector each time it's called.
Definition vector.c:102
Here is the call graph for this function: