From 64c113ed06cb749fdc513162a1cddf4ec024fd72 Mon Sep 17 00:00:00 2001 From: Samo Penic <samo.penic@gmail.com> Date: Sat, 30 Nov 2013 10:10:13 +0000 Subject: [PATCH] Changes in data structure, added vertex_list_remove_vtx and vertex_list_add_vtx. Trying to fix the rest --- src/vertex.c | 63 +++++++++++++++++++++++-------- 1 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/vertex.c b/src/vertex.c index 38647aa..f1ae60b 100644 --- a/src/vertex.c +++ b/src/vertex.c @@ -39,31 +39,62 @@ return vlist; } + +ts_bool vertex_list_add_vtx(ts_vertex_list *vlist, ts_vertex *vtx){ + +#ifdef DEBUG + if(vtx==NULL || vlist==NULL) return TS_FAIL; +#endif + if(vlist->list_size < vlist->n+1){ + vlist->vtx=(ts_vertex **)realloc(vlist->vtx, (vlist->list_size+TS_VLIST_CHUNK)*sizeof(ts_vertex*)); + if(vlist->vtx==NULL){ + fatal("Error in vertex_list_add. Could not reallocate memory space.",9999); + } + vlist->list_size+=TS_VLIST_CHUNK; + } + vlist->vtx[n]=vtx; + vlist->n++; + return TS_SUCCESS; +} + + +/* Idea is to delete vertex by removing it from list. The emply place is then filled in by the last +vertex in the list. List can then be resized by one -- unless resize is required when max list size - +real list size > predefined value */ +ts_bool vertex_list_remove_vtx(ts_vertex_list *vlist, ts_vertex *vtx){ +#ifdef DEBUG + if(vtx==NULL || vlist==NULL) return TS_FAIL; +#endif + for(i=0; i<vlist->n;i++){ + if(vlist->vtx[i]==vtx){ + vlist->vtx[i]=vlist->vtx[n-1]; + vlist->n--; + if(vlist->list_size-vlist->n > TS_VLIST_CHUNK){ + vlist->vtx=(ts_vertex **)realloc(vlist->vtx, (vlist->list_size-TS_VLIST_CHUNK)*sizeof(ts_vertex*)); + if(vlist->vtx==NULL){ + fatal("Error in vertex_list_add. Could not reallocate memory space.",9999); + } + vlist->list_size-=TS_VLIST_CHUNK; + } + return TS_SUCCESS; + } + } + return TS_FAIL; +} + + ts_bool vtx_add_neighbour(ts_vertex *vtx, ts_vertex *nvtx){ ts_uint i; /* no neighbour can be null! */ if(vtx==NULL || nvtx==NULL) return TS_FAIL; /*if it is already a neighbour don't add it to the list */ - for(i=0; i<vtx->neigh_no;i++){ - if(vtx->neigh[i]==nvtx) return TS_FAIL; + for(i=0; i<vtx->neigh->n;i++){ + if(vtx->neigh->vtx[i]==nvtx) return TS_FAIL; } - ts_uint nn=++vtx->neigh_no; + ts_uint nn=++vtx->neigh->n; vtx->neigh=(ts_vertex **)realloc(vtx->neigh, nn*sizeof(ts_vertex *)); vtx->neigh[nn-1]=nvtx; -/* This was a bug in creating DIPYRAMID (the neighbours were not in right - * order). - */ - /* pa se sosedu dodamo vertex */ - /*if it is already a neighbour don't add it to the list */ -/* - for(i=0; i<nvtx->data->neigh_no;i++){ - if(nvtx->data->neigh[i]==vtx) return TS_FAIL; - } - nn=++nvtx->data->neigh_no; - nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh, nn*sizeof(ts_vertex *)); - nvtx->data->neigh[nn-1]=vtx; -*/ return TS_SUCCESS; } -- Gitblit v1.9.3