1 /**********
2 *
3 * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
4 * University of Manchester, UK. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * . Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * . Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * . Neither the name of the University of Manchester nor the names of its
17 * contributors may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 **********
34 *
35 * Program : TINA
36 * File : $Source: /home/tina/cvs/tina-libs/tina/sys/sysLst_match.c,v $
37 * Date : $Date: 2003/09/22 16:09:02 $
38 * Version : $Revision: 1.6 $
39 * CVS Id : $Id: sysLst_match.c,v 1.6 2003/09/22 16:09:02 tony Exp $
40 *
41 * Author : Legacy TINA
42 */
43 /**
44 * @file A genericly defined matching structure for storing correspondence between any two
45 * data strucutres.
46 *
47 * @brief typedef struct match { int type; int label; float weight; void *to1;
48 * void *to2; struct list *props; } Match;
49 *
50 *********
51 */
52
53 #include "sysLst_match.h"
54
55 #if HAVE_CONFIG_H
56 #include <config.h>
57 #endif
58
59 #include <stdio.h>
60
61 #if STDC_HEADERS
62 # include <string.h>
63 #elif __GNUC__
64 void *memcpy();
65 void *memset();
66 #else
67 char *memcpy();
68 char *memset();
69 #endif
70
71 #ifdef _PCC
72 #include <memory.h>
73 #endif
74
75 #include <tina/sys/sys_LstDef.h>
76 #include <tina/sys/sys_MemDef.h>
77 #include <tina/sys/sysLst_prop.h>
78 #include <tina/sys/sysLst_list.h>
79 #include <tina/sys/sysMem_ralloc.h>
80
81
82 /* static variable for unique match label generation */
83 static int label = 0; /* static data! */
84
85 /* allocate match structure body of specified type */
86 Match *match_alloc(int type)
87 {
88 Match *match = ts_ralloc(Match);
89
90 match->type = type;
91 match->label = label++;
92 match->weight = (float)1.0;
93 match->to1 = match->to2 = NULL;
94 match->props = NULL;
95 return (match);
96 }
97
98 /* allocate match structure and make it reference generic pointers of
99 * specified type
100 *
101 * note that the match structure is for matches between things of the same
102 * tyoe */
103 Match *match_make(void *ptr1, void *ptr2, int type)
104 {
105 Match *match = ts_ralloc(Match);
106
107 match->type = type;
108 match->label = label++;
109 match->weight = (float)1.0;
110 match->to1 = ptr1;
111 match->to2 = ptr2;
112 match->props = NULL;
113 return (match);
114 }
115
116 /* copy the match data structure and make a copy of its property list
117 *
118 * changes to the properties will be shared */
119 Match *match_copy(Match * match)
120 {
121 Match *copy;
122
123 if (match == NULL)
124 return (NULL);
125
126 copy = ts_ralloc(Match);
127 (void) memcpy((char *) copy, (char *) match, sizeof(Match));
128 copy->props = proplist_copy(match->props);
129 return (copy);
130 }
131
132 /* find the mostr strongly weighted match in a list */
133 double mlist_strongest(List * mlist, double (*cost_func) ( /* ??? */ ))
134 {
135 double maxs = 0;
136
137 while (mlist != NULL)
138 {
139 Match *m = (Match *) mlist->to;
140 double s;
141
142 s = (cost_func == NULL) ? m->weight : cost_func(m);
143
144 if (s > maxs)
145 maxs = s;
146 mlist = mlist->next;
147 }
148 return (maxs);
149 }
150
151 /* check if to2 is referenced as a ->to2 of any element of the match
152 * list */
153 Bool mlist_to2_exists(List * mlist, void *to2)
154 {
155 List *ptr;
156
157 for (ptr = mlist; ptr != NULL; ptr = ptr->next)
158 if (((Match *) ptr->to)->to2 == to2)
159 return (true);
160 return (false);
161 }
162
163 /* generate a list of the second reference (->to2) of a match list */
164 List *mlist_to2_list(List * mlist)
165 {
166 List *mptr;
167 List *list = NULL;
168
169 for (mptr = mlist; mptr != NULL; mptr = mptr->next)
170 {
171 Match *m = (Match *) mptr->to;
172
173 list = ref_addtostart(list, (void *) m->to2, m->type);
174 }
175 return (list_reverse(list));
176 }
177
178 void match_free(Match * match)
179 {
180 if (match == NULL)
181 return;
182 proplist_freelist(match->props);
183 match->props = NULL;
184 rfree((void *) match);
185 }
186
187 void match_copy_free(Match * match)
188 /* don't free up the proplist as this was not allocated
189 NAT 22/12/99 */
190 {
191 if (match == NULL)
192 return;
193 list_rm_links(match->props);
194 match->props = NULL;
195 rfree((void *) match);
196 }
197
198 void mlist_free(List * mlist)
199 /* list of matches */
200 {
201 list_rm(mlist, match_free);
202 }
203
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.