From 73f967e943c8d7a77cb21411de990904f09baba6 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo@andromeda>
Date: Sat, 27 Nov 2010 18:26:06 +0000
Subject: [PATCH] Before adding another data structure to the chain.

---
 src/main.c    |   12 ++----
 src/vertex.c  |   40 ++++++++++++--------
 src/general.h |    9 ++++
 src/vertex.h  |    3 +
 4 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/src/general.h b/src/general.h
index f0b42e8..5b4b491 100644
--- a/src/general.h
+++ b/src/general.h
@@ -135,6 +135,13 @@
 };
 typedef struct ts_vertex ts_vertex;
 
+typedef struct {
+    ts_uint n;
+    ts_vertex **vtx;
+
+} ts_vertex_list;
+
+
 /** ts_bond is a structure that describes a bond */
 typedef struct {
 	ts_vertex *vtx1;
@@ -204,4 +211,6 @@
 
 //ts_uint ts_fprintf(FILE *fd, char *fmt, va_list ap);
 
+#define VTX(vlist,n) &(vlist->vtx[n])
+
 #endif
diff --git a/src/main.c b/src/main.c
index 38ec4be..bc994df 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,20 +12,16 @@
 */
 
 int main(int argv, char *argc[]){
-ts_int i;
 ts_bool retval;
-ts_vertex **vlist=init_vertex_list(5);
+ts_vertex_list *vlist=init_vertex_list(5);
 
 
-retval=vtx_add_neighbour(&vlist[0],&vlist[1]);
+retval=vtx_add_neighbour(VTX(vlist,1),VTX(vlist,0));
 if(retval==TS_FAIL) printf("1. already a member or vertex is null!\n");
-retval=vtx_add_neighbour(&vlist[1],&vlist[0]);
+retval=vtx_add_neighbour(VTX(vlist,0),VTX(vlist,1));
 if(retval==TS_FAIL) printf("2. already a member or vertex is null!\n");
 
-for(i=0;i<5;i++){
-vtx_free(&vlist[i]);
-}
-free(vlist);
+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 54ae9b7..60123ab 100644
--- a/src/vertex.c
+++ b/src/vertex.c
@@ -5,25 +5,22 @@
 #include "vertex.h"
 #include<stdio.h>
 
-/* Argument je struktura vlist in stevilo vertexov, ki jih zelimo dati v
- * vlist je lahko NULL, N mora biti vecje od 0. Ce vlist obstaja, potem resizamo
- * vlist na stevilo N.
- */
-ts_vertex **init_vertex_list(ts_uint N){	
+ts_vertex_list *init_vertex_list(ts_uint N){	
 	ts_int i;
-    ts_vertex **vlist;
+    ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list *));
+    
 	if(N==0){
 		err("Initialized vertex list with zero elements. Pointer set to NULL");
-		vlist=NULL;
-		return NULL;
+        vlist->n=0;
+		vlist->vtx=NULL;
+		return vlist;
 	}
-		vlist=(ts_vertex **)malloc(N*sizeof(ts_vertex *));
-        if(vlist==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[i]=init_vertex(i); 
-        }
+	
+    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);
+    vlist->n=N;
 	return vlist;
 }
 
@@ -87,8 +84,19 @@
     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));
+    }
+    free(vlist);
+    return TS_SUCCESS;
+}
 
-inline ts_double vertex_distance_sq(ts_vertex **vtx1, ts_vertex **vtx2){
+
+
+
+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);
diff --git a/src/vertex.h b/src/vertex.h
index dbe1b6e..8a2a99f 100644
--- a/src/vertex.h
+++ b/src/vertex.h
@@ -13,8 +13,9 @@
  *      indexing the vertexes 0..N-1.
  *	@returns ts_bool value 1 on success, 0 otherwise
 */
-ts_vertex **init_vertex_list(ts_uint N);
+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_bool vtx_list_free(ts_vertex_list *vlist);
 #endif

--
Gitblit v1.9.3