commit | author | age
|
d7639a
|
1 |
#include<stdlib.h> |
SP |
2 |
#include "general.h" |
a10dd5
|
3 |
#include "vertex.h" |
d7639a
|
4 |
|
a10dd5
|
5 |
ts_bond_list *init_bond_list(){ |
SP |
6 |
ts_bond_list *blist=(ts_bond_list *)malloc(sizeof(ts_bond_list)); |
d7639a
|
7 |
blist->n=0; |
SP |
8 |
blist->bond=NULL; |
a10dd5
|
9 |
return blist; |
d7639a
|
10 |
} |
SP |
11 |
|
a10dd5
|
12 |
ts_bond *bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){ |
b01cc1
|
13 |
ts_uint i; |
a10dd5
|
14 |
/* no vertices must be null! */ |
SP |
15 |
if(vtx1==NULL || vtx2==NULL) return NULL; |
b01cc1
|
16 |
/* Verify if the bond already exists... Don't do multiple bonds */ |
SP |
17 |
for(i=0;i<blist->n;i++){ |
|
18 |
if((blist->bond[i]->data->vtx1==vtx1 || blist->bond[i]->data->vtx1==vtx2) |
|
19 |
&&(blist->bond[i]->data->vtx2==vtx1 || blist->bond[i]->data->vtx2==vtx2)) |
|
20 |
return blist->bond[i]; |
|
21 |
} |
d7639a
|
22 |
blist->n++; |
a10dd5
|
23 |
blist->bond=(ts_bond **)realloc(blist->bond,blist->n*sizeof(ts_bond *)); |
SP |
24 |
if(blist->bond==NULL) fatal("Cannot reallocate memory for additional **ts_bond.",100); |
|
25 |
blist->bond[blist->n-1]=(ts_bond *)malloc(sizeof(ts_bond)); |
|
26 |
if(blist->bond[blist->n-1]==NULL) fatal("Cannot allocate memory for additional *ts_bond.",100); |
|
27 |
blist->bond[blist->n-1]->data=(ts_bond_data *)malloc(sizeof(ts_bond_data)); |
|
28 |
|
|
29 |
//NOW insert vertices into data! |
|
30 |
blist->bond[blist->n - 1]->data->vtx1=vtx1; |
|
31 |
blist->bond[blist->n - 1]->data->vtx2=vtx2; |
|
32 |
|
|
33 |
//Should we calculate bond length NOW? |
|
34 |
|
|
35 |
return blist->bond[blist->n-1]; |
d7639a
|
36 |
} |
SP |
37 |
|
|
38 |
ts_bool bond_list_free(ts_bond_list *blist){ |
a10dd5
|
39 |
ts_uint i; |
SP |
40 |
for(i=0;i<blist->n;i++){ |
|
41 |
free(blist->bond[i]->data); |
|
42 |
free(blist->bond[i]); |
|
43 |
} |
d7639a
|
44 |
free(blist->bond); |
a10dd5
|
45 |
free(blist); |
d7639a
|
46 |
return TS_SUCCESS; |
SP |
47 |
} |