ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/g3flist.c
Revision: 2.1
Committed: Wed Aug 12 23:07:59 2015 UTC (8 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Added Jan Wienold's evalglare to distribution

File Contents

# Content
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "g3flist.h"
6
7
8 static int resize_list(g3FList* fl,int sz)
9 {
10 fl->list = (g3Float*) realloc(fl->list,(sz*fl->comp_size*sizeof(g3Float)));
11 if (!fl->list)
12 return 0;
13 fl->cap = sz;
14 if (sz < fl->size)
15 fl->size = sz;
16 if (sz == 0)
17 fl->list = NULL;
18 return 1;
19 }
20
21 int g3fl_init(g3FList* fl,int cs)
22 {
23 fl->cap = 0;
24 fl->size = 0;
25 fl->list = NULL;
26 fl->comp_size = cs;
27 fl->ipos = 0;
28 return resize_list(fl,G3FL_STARTCAP);
29 }
30
31 g3FList* g3fl_create(int cs)
32 {
33 g3FList* res = (g3FList*) malloc(sizeof(g3FList));
34 if (!g3fl_init(res,cs))
35 return NULL;
36 return res;
37 }
38
39 void g3fl_free(g3FList* fl)
40 {
41 free(fl->list);
42 free(fl);
43 }
44
45 g3Float* g3fl_append_new(g3FList* fl)
46 {
47 if (fl->size == fl->cap) {
48 if (!resize_list(fl,fl->cap*2)) {
49 fprintf(stderr,"Out of memory in g3fl_append_new\n");
50 return NULL;
51 }
52 }
53 fl->size++;
54 return (&fl->list[G3FL_ID(fl,(fl->size-1))]);
55 }
56
57 int g3fl_append(g3FList* fl,g3Float* el)
58 {
59 g3Float* ne;
60
61 if (!(ne = g3fl_append_new(fl)))
62 return 0;
63 memcpy(ne,el,(fl->comp_size*sizeof(g3Float)));
64 return 1;
65 }
66
67 g3Float* g3fl_get(g3FList* fl,int id)
68 {
69 return &(fl->list[G3FL_ID(fl,id)]);
70 }
71
72 g3Float* g3fl_begin(g3FList* fl)
73 {
74 if (fl->size == 0)
75 return NULL;
76 fl->ipos = 1;
77 return g3fl_get(fl,0);
78 }
79
80 void g3fl_rewind(g3FList* fl)
81 {
82 fl->ipos = 0;
83 }
84
85 static int comp_sort(const g3Float* v1,const g3Float* v2)
86 {
87 if (v1< v2)
88 return -1;
89 if (v1 > v2)
90 return 1;
91 return 0;
92 }
93
94 int g3fl_sort(g3FList* fl,int cnum)
95 {
96 if (cnum >= fl->comp_size)
97 return 0;
98 qsort((fl->list + cnum),fl->size,fl->comp_size*sizeof(g3Float),(int (*)(const void* v1,const void* v2)) comp_sort);
99 return 1;
100 }
101
102 g3Float* g3fl_next(g3FList* fl)
103 {
104 if (fl->ipos >= fl->size)
105 return NULL;
106 return g3fl_get(fl,fl->ipos++);
107 }
108
109 g3Float* g3fl_get_copy(g3FList* fl,int id)
110 {
111 g3Float* ne;
112
113 if (!(ne = (g3Float*) malloc(fl->comp_size*sizeof(g3Float))))
114 return NULL;
115 memcpy(ne,&(fl->list[G3FL_ID(fl,id)]),(fl->comp_size*sizeof(g3Float)));
116 return ne;
117 }
118
119 int g3fl_remove(g3FList* fl,int id)
120 {
121 if ((id < 0) || (id >= fl->size))
122 return 0;
123 if (id < (fl->size - 1))
124 memmove(&(fl->list[G3FL_ID(fl,id)]),&(fl->list[G3FL_ID(fl,id+1)]),
125 fl->comp_size*sizeof(g3Float));
126 fl->size--;
127 return 1;
128 }
129
130 int g3fl_remove_last(g3FList* fl)
131 {
132 fl->size--;
133 if (fl->size < (fl->cap/2))
134 return resize_list(fl,(fl->cap/2));
135 return 1;
136 }
137
138
139 #ifdef TEST_FLIST
140
141 int main(int argc,char** argv)
142 {
143 int i;
144 g3FList* fl = g3fl_create(2);
145 g3Float* n;
146
147 for(i=0;i<10;i++) {
148 n = g3fl_append_new(fl);
149 n[0] = i;
150 n[1] = i;
151 }
152 for(n=g3fl_begin(fl);n != NULL;n=g3fl_next(fl))
153 printf("%f %f\n",n[0],n[1]);
154 g3fl_free(fl);
155 return EXIT_SUCCESS;
156 }
157
158
159 #endif