Created
July 5, 2016 12:44
-
-
Save amanuel2/d20145aeb1535a2089f714df0d69a610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdlib.h> | |
#include<stdio.h> | |
#include<stdbool.h> | |
#include<stddef.h> | |
#include<stdint.h> | |
#include<errno.h> | |
#include "../includes/Vertex.h" | |
/** | |
* Vertex Library C | |
* | |
* GCC C99 <Vertex.c> | |
* | |
* @author Amanuel Bogale | |
* @copyright 2016 Amanuel Bogale | |
* @license http://www.opensource.org/licenses/mit-license.html MIT License | |
* | |
*/ | |
/* | |
* Vertex Initalizer | |
* Always call after initalizing a | |
* vertex | |
*/ | |
vertex_am* init_vertex(size_t size, vertex_am* vertex) | |
{ | |
vertex = malloc(size); | |
vertex->current_size = size; | |
vertex->size_contents = 0; | |
vertex->contents = NULL; | |
return vertex; | |
} | |
/* | |
* Get Number of elements | |
*/ | |
long long get_elements_num(vertex_am vert) | |
{ | |
return(vert.size_contents); | |
} | |
/* | |
* Add to the end | |
* of the Dynamic Array | |
* / "Vertex" | |
*/ | |
void add_end(vertex_am *vert, void* val) | |
{ | |
vert->contents = (void **)realloc(vert->contents,sizeof(*vert->contents) * (vert->size_contents+1)); | |
if(vert->contents == NULL) | |
{ | |
#define ERROR_VERT_AM 1 | |
printf("%s" , "ERROR NULL"); | |
} | |
else | |
{ | |
printf("SEpERATION\n"); | |
for(int i=0; i<=(vert->size_contents-1); i++) | |
{ | |
void* val_c_ind_ptrd = vert->contents[i]; | |
int *val_c_ptr = (int*)val_c_ind_ptrd; | |
printf("%d \n" , *val_c_ptr); | |
} | |
vert->contents[vert->size_contents] = val; | |
vert->size_contents++; | |
} | |
} | |
/* | |
* Add to the beggining | |
* of the Dynamic Array | |
* / "Vertex" | |
*/ | |
void add_beg(vertex_am *vert, void* val) | |
{ | |
vert->contents = (void **)realloc(vert->contents,sizeof(*vert->contents) * (vert->size_contents+1)); | |
for(int i=(vert->size_contents-1); i>=0 ; i--) | |
{ | |
void* val_c_ind_ptrd = vert->contents[i]; | |
vert->contents[i+1]=val_c_ind_ptrd; | |
} | |
vert->size_contents++; | |
vert->contents[0]=val; | |
} | |
/* | |
* Add to the specified | |
* index of the Dynamic | |
* Array / "Vertex" | |
*/ | |
void add_index(vertex_am *vert, void* val,long long index) | |
{ | |
if(index >= (vert->size_contents-1)) | |
{ | |
printf("Cant Put a Index Greater than the size. NOTE: GO BY INDEX / 0"); | |
#define ERROR_VERT_AM 1 | |
exit(1); | |
} | |
//0,1,2,3 | |
vert->contents = (void **)realloc(vert->contents,sizeof(*vert->contents) * (vert->size_contents+1)); | |
for(int i=(vert->size_contents-1); i>=(int)index; i--) | |
{ | |
void* val_c_ind_ptrd = vert->contents[i]; | |
vert->contents[i+1]=val_c_ind_ptrd; | |
} | |
vert->size_contents++; | |
vert->contents[index]=val; | |
} | |
/* | |
* Get Value, based on Index | |
*/ | |
void* get_val(vertex_am vert,long long index) | |
{ | |
return (vert.contents[index]); | |
} | |
/* | |
* Get index based on value. | |
* Thier might be multiple | |
* values that are the same. | |
* So the param decides this | |
* @param index, FIRST for | |
* first value that appears | |
* LAST for last value that appears | |
*/ | |
int get_index(vertex_am vert, void*key,index in) | |
{ | |
int returner; | |
for(int i=0; i<(vert.size_contents); i++) | |
{ | |
if((*(int*)vert.contents[i]) == (*(int*)key)) | |
{ | |
returner=i; | |
if(in == FIRST) | |
{ | |
break; | |
} | |
} | |
} | |
return (returner); | |
} | |
/* | |
* Get the index based on | |
* after how many occurrences | |
* do you decide to get the index | |
* . If -1 is returned, you have incorrect | |
* specifier. NOTE: START FROM 0 | |
*/ | |
int get_specified_index(vertex_am vert, void*key, int specifier) | |
{ | |
int returner = -1; | |
for(int i=0; i<(vert.size_contents); i++) | |
{ | |
if(i==specifier) | |
{ | |
if((*(int*)vert.contents[i]) == (*(int*)key)) | |
{ | |
returner=i; | |
} | |
} | |
} | |
return returner; | |
} | |
/* | |
* Delete the Last | |
* Element on the | |
* Dynamic Array. | |
* @param returns true | |
* if successfully deleted, | |
* false if unsuccessful | |
*/ | |
bool pop_end(vertex_am *vert) | |
{ | |
bool status = true; | |
//{0,1,2,3} | |
// printf("%lld" , vert->size_contents); | |
vert->contents[(vert->size_contents-1)]=0; | |
for(int i=0; i<=(vert->size_contents-1); i++) | |
{ | |
void* val_c_ind_ptrd = vert->contents[i]; | |
int *val_c_ptr = (int*)val_c_ind_ptrd; | |
printf("%d \n" , *val_c_ptr); | |
} | |
if(errno) | |
{ | |
status = false; | |
} | |
vert->size_contents-=1; | |
return status; | |
} | |
/* | |
* Vertex "Detilazer" | |
* Always call at the end | |
* of lifetime of a vertex | |
* to stop leaks | |
*/ | |
void end_vertex(vertex_am* vertex) | |
{ | |
free(vertex->contents); | |
free(vertex); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment