ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/g3flist.c
Revision: 2.2
Committed: Tue Aug 18 15:02:53 2015 UTC (8 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 2.1: +3 -0 lines
Log Message:
Added RCCS id's to new source files (forgotten during initial check-in)

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 g3Float* v1,const g3Float* v2)
89 {
90 if (v1< v2)
91 return -1;
92 if (v1 > v2)
93 return 1;
94 return 0;
95 }
96
97 int g3fl_sort(g3FList* fl,int cnum)
98 {
99 if (cnum >= fl->comp_size)
100 return 0;
101 qsort((fl->list + cnum),fl->size,fl->comp_size*sizeof(g3Float),(int (*)(const void* v1,const void* v2)) comp_sort);
102 return 1;
103 }
104
105 g3Float* g3fl_next(g3FList* fl)
106 {
107 if (fl->ipos >= fl->size)
108 return NULL;
109 return g3fl_get(fl,fl->ipos++);
110 }
111
112 g3Float* g3fl_get_copy(g3FList* fl,int id)
113 {
114 g3Float* ne;
115
116 if (!(ne = (g3Float*) malloc(fl->comp_size*sizeof(g3Float))))
117 return NULL;
118 memcpy(ne,&(fl->list[G3FL_ID(fl,id)]),(fl->comp_size*sizeof(g3Float)));
119 return ne;
120 }
121
122 int g3fl_remove(g3FList* fl,int id)
123 {
124 if ((id < 0) || (id >= fl->size))
125 return 0;
126 if (id < (fl->size - 1))
127 memmove(&(fl->list[G3FL_ID(fl,id)]),&(fl->list[G3FL_ID(fl,id+1)]),
128 fl->comp_size*sizeof(g3Float));
129 fl->size--;
130 return 1;
131 }
132
133 int g3fl_remove_last(g3FList* fl)
134 {
135 fl->size--;
136 if (fl->size < (fl->cap/2))
137 return resize_list(fl,(fl->cap/2));
138 return 1;
139 }
140
141
142 #ifdef TEST_FLIST
143
144 int main(int argc,char** argv)
145 {
146 int i;
147 g3FList* fl = g3fl_create(2);
148 g3Float* n;
149
150 for(i=0;i<10;i++) {
151 n = g3fl_append_new(fl);
152 n[0] = i;
153 n[1] = i;
154 }
155 for(n=g3fl_begin(fl);n != NULL;n=g3fl_next(fl))
156 printf("%f %f\n",n[0],n[1]);
157 g3fl_free(fl);
158 return EXIT_SUCCESS;
159 }
160
161
162 #endif