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 +++++++++++++++++++++++-------- src/general.h | 11 +++-- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/general.h b/src/general.h index f3328f6..cc051f5 100644 --- a/src/general.h +++ b/src/general.h @@ -136,8 +136,9 @@ ts_double x; /**< The x coordinate of vertex. */ ts_double y; /**< The y coordinate of vertex. */ ts_double z; /**< The z coordinate of vertex. */ - ts_uint neigh_no; /**< The number of neighbours. */ - struct ts_vertex **neigh; /**< The pointer that holds neigh_no pointers to this structure. */ +// ts_uint neigh_no; /**< The number of neighbours. */ +// struct ts_vertex **neigh; /**< The pointer that holds neigh_no pointers to this structure. */ + struct ts_vertex_list *neigh; ts_double *bond_length; /**< Obsolete! The bond lenght is moved to ts_bond */ ts_double *bond_length_dual; /**< Obsolete! Bond length in dual lattice is moved to ts_bond! */ ts_double curvature; @@ -157,11 +158,13 @@ }; typedef struct ts_vertex ts_vertex; -typedef struct { +struct ts_vertex_list{ ts_uint n; + ts_uint list_size; ts_vertex **vtx; -} ts_vertex_list; +}; +typedef struct ts_vertex_list ts_vertex_list; struct ts_bond { ts_uint idx; 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