ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/g3flist.c
Revision: 2.3
Committed: Thu Jul 14 17:32:12 2016 UTC (7 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R2, rad5R3, rad5R1, HEAD
Changes since 2.2: +20 -10 lines
Log Message:
Changes to evalglare by Jan Wienold

File Contents

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