From 73771431acbfbd1ffa2f3aeabb21fed9016fca42 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo@andromeda>
Date: Sat, 27 Nov 2010 20:11:27 +0000
Subject: [PATCH] Before adding additional completeley unnecessary level of pointers in vertex list.

---
 src/main.c    |    6 +-
 src/vertex.c  |   64 +++++++++++++++++--------------
 src/general.h |   19 ++++++---
 src/vertex.h  |    7 ++-
 4 files changed, 55 insertions(+), 41 deletions(-)

diff --git a/src/general.h b/src/general.h
index 5b4b491..bd96c7a 100644
--- a/src/general.h
+++ b/src/general.h
@@ -113,31 +113,37 @@
  *
  *  ts_vertex holds the data for one single point (bead, vertex) in the space. To understand how to use it
  *  here is a detailed description of the fields in the data structure. */
-struct ts_vertex {
+struct ts_vertex_data {
         ts_uint idx; /**< Represents index of the vertex point. Should become obsolete in C. */        
         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. Careful when using pointers to pointers! Also developers do mistakes here.  */
+        struct ts_vertex **neigh; /**< The pointer that holds neigh_no pointers to this structure. Careful when using pointers to pointers! Also developers do mistakes here.  */
         ts_double *bond_length;
         ts_double *bond_length_dual;
         ts_double curvature;
         ts_double energy;
         ts_double energy_h;
         ts_uint tristar_no;
-        struct ts_triangle ***tristar;
-        struct ts_bond ***bond;
+        struct ts_triangle **tristar;
+        struct ts_bond **bond;
         struct ts_cell *cell;
         ts_double xk;
         ts_double c;
         ts_uint id;
 };
+typedef struct ts_vertex_data ts_vertex_data;
+
+struct ts_vertex {
+        ts_uint idx;
+        ts_vertex_data *data;
+};
 typedef struct ts_vertex ts_vertex;
 
 typedef struct {
     ts_uint n;
-    ts_vertex **vtx;
+    ts_vertex *vtx;
 
 } ts_vertex_list;
 
@@ -211,6 +217,7 @@
 
 //ts_uint ts_fprintf(FILE *fd, char *fmt, va_list ap);
 
-#define VTX(vlist,n) &(vlist->vtx[n])
+#define VTX(n) &(vlist->vtx[n])
+#define VTX_DATA(n) vlist->vtx[n].data
 
 #endif
diff --git a/src/main.c b/src/main.c
index bc994df..d6c2854 100644
--- a/src/main.c
+++ b/src/main.c
@@ -16,11 +16,11 @@
 ts_vertex_list *vlist=init_vertex_list(5);
 
 
-retval=vtx_add_neighbour(VTX(vlist,1),VTX(vlist,0));
+retval=vtx_add_neighbour(VTX(1),VTX(0));
 if(retval==TS_FAIL) printf("1. already a member or vertex is null!\n");
-retval=vtx_add_neighbour(VTX(vlist,0),VTX(vlist,1));
+retval=vtx_add_neighbour(VTX(0),VTX(1));
 if(retval==TS_FAIL) printf("2. already a member or vertex is null!\n");
-
+VTX_DATA(1)->x=1.0;
 vtx_list_free(vlist);
 printf("Done.\n");
 return 0; //program finished perfectly ok. We return 0.
diff --git a/src/vertex.c b/src/vertex.c
index 60123ab..b83f609 100644
--- a/src/vertex.c
+++ b/src/vertex.c
@@ -16,21 +16,20 @@
 		return vlist;
 	}
 	
-    vlist->vtx=(ts_vertex **)malloc(N*sizeof(ts_vertex *));
+    vlist->vtx=(ts_vertex *)malloc(N*sizeof(ts_vertex));
     if(vlist->vtx==NULL)
         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
-    for(i=0;i<N;i++) vlist->vtx[i]=init_vertex(i);
+    for(i=0;i<N;i++) vlist->vtx[i].data=init_vertex_data();
     vlist->n=N;
 	return vlist;
 }
 
-ts_vertex *init_vertex(ts_uint idx){
-    ts_vertex *vtx;
-    vtx=(ts_vertex *)malloc(sizeof(ts_vertex));
-    if(vtx==NULL)
+ts_vertex_data *init_vertex_data(){
+    ts_vertex_data *data;
+    data=(ts_vertex_data *)malloc(sizeof(ts_vertex_data));
+    if(data==NULL)
         fatal("Fatal error reserving memory space for ts_vertex! Memory full?", 100);
-    vtx->idx=idx;
-    return vtx;
+    return data;
 }
 
 /*
@@ -46,27 +45,27 @@
 
 
 
-ts_bool vtx_add_neighbour(ts_vertex **vtx, ts_vertex **nvtx){
+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->data->neigh_no;i++){
+        if(vtx->data->neigh[i]==nvtx) return TS_FAIL;
     }
-    ts_uint nn=(*vtx)->neigh_no++;
-    (*vtx)->neigh=(ts_vertex ***)realloc((*vtx)->neigh, nn*sizeof(ts_vertex **));
-    (*vtx)->neigh[nn]=nvtx;
+    ts_uint nn=vtx->data->neigh_no++;
+    vtx->data->neigh=(ts_vertex **)realloc(vtx->data->neigh, nn*sizeof(ts_vertex *));
+    vtx->data->neigh[nn]=nvtx;
 
     /* pa se sosedu dodamo vertex */
     /*if it is already a neighbour don't add it to the list */
-    for(i=0; i<(*nvtx)->neigh_no;i++){
-        if((*nvtx)->neigh[i]==vtx) return TS_FAIL;
+    for(i=0; i<nvtx->data->neigh_no;i++){
+        if(nvtx->data->neigh[i]==vtx) return TS_FAIL;
     } 
-    nn=(*nvtx)->neigh_no++;
-    (*nvtx)->neigh=(ts_vertex ***)realloc((*nvtx)->neigh, nn*sizeof(ts_vertex **));
-    (*nvtx)->neigh[nn]=vtx;
+    nn=nvtx->data->neigh_no++;
+    nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh, nn*sizeof(ts_vertex *));
+    nvtx->data->neigh[nn]=vtx;
 
 
 /* Ustvari bond in doloci dolzino */
@@ -75,28 +74,35 @@
 }
 
 
-ts_bool vtx_free(ts_vertex **vtx){
-    if((*vtx)->neigh!=NULL)   free((*vtx)->neigh);
-    if((*vtx)->tristar!=NULL) free((*vtx)->tristar);
-    if((*vtx)->bond!=NULL)    free((*vtx)->bond);
-    if((*vtx)->cell!=NULL)    free((*vtx)->cell);
-    free(*vtx);
+ts_bool vtx_data_free(ts_vertex_data *data){
+    if(data->neigh!=NULL)   free(data->neigh);
+    if(data->tristar!=NULL) free(data->tristar);
+    if(data->bond!=NULL)    free(data->bond);
+    if(data->cell!=NULL)    free(data->cell);
+    free(data);
+    return TS_SUCCESS;
+}
+
+ts_bool vtx_free(ts_vertex  *vtx){
+    vtx_data_free(vtx->data);
+    free(vtx);
     return TS_SUCCESS;
 }
 
 ts_bool vtx_list_free(ts_vertex_list *vlist){
     int i;
     for(i=0;i<vlist->n;i++){
-        vtx_free(VTX(vlist,i));
+        vtx_data_free(vlist->vtx[i].data);
     }
+    free(vlist->vtx);
     free(vlist);
     return TS_SUCCESS;
 }
 
 
 
-
-inline ts_double vtx_distance_sq(ts_vertex **vtx1, ts_vertex **vtx2){
+/* rewrite for additional structure in chain */
+/*inline ts_double vtx_distance_sq(ts_vertex *vtx1, ts_vertex *vtx2){
     ts_double dist;
 #ifdef TS_DOUBLE_DOUBLE
     dist=pow((*vtx1)->x-(*vtx2)->x,2) + pow((*vtx1)->y-(*vtx2)->y,2) + pow((*vtx1)->z-(*vtx2)->z,2);
@@ -109,4 +115,4 @@
 #endif
     return(dist);
 }
-
+*/
diff --git a/src/vertex.h b/src/vertex.h
index 8a2a99f..bceeb1c 100644
--- a/src/vertex.h
+++ b/src/vertex.h
@@ -14,8 +14,9 @@
  *	@returns ts_bool value 1 on success, 0 otherwise
 */
 ts_vertex_list *init_vertex_list(ts_uint N);
-ts_vertex *init_vertex(ts_uint idx);
-ts_bool vtx_add_neighbour(ts_vertex **vtx, ts_vertex **nvtx);
-ts_bool vtx_free(ts_vertex **vtx);
+ts_vertex_data *init_vertex_data(void);
+ts_bool vtx_add_neighbour(ts_vertex *vtx, ts_vertex *nvtx);
+ts_bool vtx_data_free(ts_vertex_data *data);
+ts_bool vtx_free(ts_vertex *vtx);
 ts_bool vtx_list_free(ts_vertex_list *vlist);
 #endif

--
Gitblit v1.9.3