1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include <assert.h> |
---|
6 | #include <stdio.h> |
---|
7 | #include "simil_match.h" |
---|
8 | |
---|
9 | /** Creates an empty matching for two objects of specified size. |
---|
10 | @param Obj0Size Size of the first object. Must be positive. |
---|
11 | @param Obj1Size Size of the second object. Must be positive. |
---|
12 | */ |
---|
13 | SimilMatching::SimilMatching(int Obj0Size, int Obj1Size) |
---|
14 | { |
---|
15 | // assure that sizes of objects are positive |
---|
16 | assert(Obj0Size > 0); |
---|
17 | assert(Obj1Size > 0); |
---|
18 | |
---|
19 | // create necessary vectors |
---|
20 | m_apvMatched[0] = new std::vector<int>(Obj0Size); |
---|
21 | m_apvMatched[1] = new std::vector<int>(Obj1Size); |
---|
22 | |
---|
23 | // assure that vectors are created |
---|
24 | assert(m_apvMatched[0] != NULL); |
---|
25 | assert(m_apvMatched[1] != NULL); |
---|
26 | |
---|
27 | // fill vectors with "unmatched" indicator |
---|
28 | int i; |
---|
29 | for (i = 0; i < (int) m_apvMatched[0]->size(); i++) |
---|
30 | { |
---|
31 | m_apvMatched[0]->operator[](i) = -1; |
---|
32 | } |
---|
33 | for (i = 0; i < (int) m_apvMatched[1]->size(); i++) |
---|
34 | { |
---|
35 | m_apvMatched[1]->operator[](i) = -1; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | /** A copying constructor. |
---|
40 | @param Source The object to be copied. |
---|
41 | */ |
---|
42 | SimilMatching::SimilMatching(const SimilMatching &Source) |
---|
43 | { |
---|
44 | // copy the vectors of the actual matching |
---|
45 | m_apvMatched[ 0 ] = new std::vector<int>(* (Source.m_apvMatched[ 0 ])); |
---|
46 | m_apvMatched[ 1 ] = new std::vector<int>(* (Source.m_apvMatched[ 1 ])); |
---|
47 | |
---|
48 | // assure that vectors are created |
---|
49 | assert(m_apvMatched[0] != NULL); |
---|
50 | assert(m_apvMatched[1] != NULL); |
---|
51 | } |
---|
52 | |
---|
53 | /** Destroys a matching object. |
---|
54 | */ |
---|
55 | SimilMatching::~SimilMatching() |
---|
56 | { |
---|
57 | // delete vectors of matching |
---|
58 | delete m_apvMatched[0]; |
---|
59 | delete m_apvMatched[1]; |
---|
60 | } |
---|
61 | |
---|
62 | /** Gets size of the specified object. |
---|
63 | @param Index of an object (must be 0 or 1). |
---|
64 | @return Size of the object (in elements). |
---|
65 | */ |
---|
66 | int SimilMatching::GetObjectSize(int Obj) |
---|
67 | { |
---|
68 | // check parameter |
---|
69 | assert((Obj == 0) || (Obj == 1)); |
---|
70 | |
---|
71 | // return the result |
---|
72 | return m_apvMatched[ Obj ]->size(); |
---|
73 | } |
---|
74 | |
---|
75 | /** Matches elements given by indices in the given objects. |
---|
76 | @param Obj0 Index of the first object. Must be 0 or 1. |
---|
77 | @param index0 Index of element in the first object. Must be a valid index |
---|
78 | ( >= 0 and < size of the object). |
---|
79 | @param Obj1 Index of the second object. Must be 0 or 1 and different from Obj0. |
---|
80 | @param Index1 index of element in the second object. Must be a valid index |
---|
81 | ( >= 0 and < size of the object). |
---|
82 | |
---|
83 | */ |
---|
84 | void SimilMatching::Match(int Obj0, int Index0, int Obj1, int Index1) |
---|
85 | { |
---|
86 | // check parameters of object 0 |
---|
87 | assert((Obj0 == 0) || (Obj0 == 1)); |
---|
88 | assert((Index0 >= 0) && (Index0 < (int) m_apvMatched[Obj0]->size())); |
---|
89 | // check parameters of object 1 |
---|
90 | assert(((Obj1 == 0) || (Obj1 == 1)) && (Obj0 != Obj1)); |
---|
91 | assert((Index1 >= 0) && (Index1 < (int) m_apvMatched[Obj1]->size())); |
---|
92 | |
---|
93 | // match given elements |
---|
94 | // matching_Obj0(Index0) = Index1 |
---|
95 | m_apvMatched[ Obj0 ]->operator[](Index0) = Index1; |
---|
96 | // matching_Obj1(Index1) = Index0 |
---|
97 | m_apvMatched[ Obj1 ]->operator[](Index1) = Index0; |
---|
98 | } |
---|
99 | |
---|
100 | /** Checks if the given element in the given object is already matched. |
---|
101 | @param Obj Index of an object (must be 0 or 1). |
---|
102 | @param Index Index of an element in the given object. Must be a valid index |
---|
103 | ( >=0 and < size of the object). |
---|
104 | @return true if the given element is matched, false otherwise. |
---|
105 | */ |
---|
106 | bool SimilMatching::IsMatched(int Obj, int Index) |
---|
107 | { |
---|
108 | // check parameters |
---|
109 | assert((Obj == 0) || (Obj == 1)); |
---|
110 | assert((Index >= 0) && (Index < (int) m_apvMatched[ Obj ]->size())); |
---|
111 | |
---|
112 | // check if the element is matched |
---|
113 | if (m_apvMatched[ Obj ]->operator[](Index) >= 0) |
---|
114 | { |
---|
115 | return true; |
---|
116 | } |
---|
117 | else |
---|
118 | { |
---|
119 | return false; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | /** Gets index of the element thet is matched in the other object withe the element given |
---|
124 | by parameters. |
---|
125 | @param Obj Index of an object (must be 0 or 1). |
---|
126 | @param Index Index of checked element in the given object. |
---|
127 | @return Index of the element (in the other organism) that is matched with the given |
---|
128 | element. WARNING! If the given element is not matched, the result may be smaller than 0 |
---|
129 | (check IsMatched() before using GetMatchedIndex()). |
---|
130 | */ |
---|
131 | int SimilMatching::GetMatchedIndex(int Obj, int Index) |
---|
132 | { |
---|
133 | // check parameters |
---|
134 | assert((Obj == 0) || (Obj == 1)); |
---|
135 | assert((Index >= 0) && (Index < (int) m_apvMatched[ Obj ]->size())); |
---|
136 | |
---|
137 | // return the index of the matched element |
---|
138 | return m_apvMatched[ Obj ]->operator[](Index); |
---|
139 | } |
---|
140 | |
---|
141 | /** Checks if the matching is already full, i.e. if the smaller object already has all its |
---|
142 | elements matched. |
---|
143 | @return true if matching is full, false otherwise. |
---|
144 | */ |
---|
145 | bool SimilMatching::IsFull() |
---|
146 | { |
---|
147 | // assume that the matching is full |
---|
148 | bool bResult = true; |
---|
149 | // index of the smallest object |
---|
150 | int nObj; |
---|
151 | // index of an element |
---|
152 | int nElem; |
---|
153 | |
---|
154 | // find the smallest object (its index) |
---|
155 | if (m_apvMatched[ 0 ]->size() < m_apvMatched[ 1 ]->size()) |
---|
156 | { |
---|
157 | nObj = 0; |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | nObj = 1; |
---|
162 | } |
---|
163 | |
---|
164 | // check if all elements of the smallest object are matched |
---|
165 | for (nElem = 0; nElem < (int) m_apvMatched[ nObj ]->size(); nElem++) |
---|
166 | { |
---|
167 | if (m_apvMatched[ nObj ]->operator[](nElem) < 0) |
---|
168 | { |
---|
169 | // if any element is not matched, the result is false |
---|
170 | bResult = false; |
---|
171 | break; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | // return the result |
---|
176 | return bResult; |
---|
177 | } |
---|
178 | |
---|
179 | /** Checks if the matching is empty (i.e. none of elements is matched). |
---|
180 | @return true if matching is empty, otherwise - false. |
---|
181 | */ |
---|
182 | bool SimilMatching::IsEmpty() |
---|
183 | { |
---|
184 | // result - assume that matching is empty |
---|
185 | bool bResult = true; |
---|
186 | int nElem; |
---|
187 | |
---|
188 | // matching is empty if either of objects has only unmatched elements |
---|
189 | // so it may be first object |
---|
190 | int nObj = 0; |
---|
191 | for (nElem = 0; nElem < (int) m_apvMatched[ nObj ]->size(); nElem++) |
---|
192 | { |
---|
193 | if (m_apvMatched[ nObj ]->operator[](nElem) >= 0) |
---|
194 | { |
---|
195 | // if any element of the object is matched (unmatched objects have (-1)) |
---|
196 | bResult = false; |
---|
197 | break; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | // return the result from the loop |
---|
202 | return bResult; |
---|
203 | } |
---|
204 | |
---|
205 | /** Makes the matching completely empty. After a call to this method IsEmpty() should return true. |
---|
206 | */ |
---|
207 | void SimilMatching::Empty() |
---|
208 | { |
---|
209 | int iObj; // a counter of objects |
---|
210 | int iElem; // a counter of objects' elements |
---|
211 | for (iObj = 0; iObj < 2; iObj++) |
---|
212 | { |
---|
213 | // for each object in the matching |
---|
214 | for (iElem = 0; iElem < (int) m_apvMatched[ iObj ]->size(); iElem++) |
---|
215 | { |
---|
216 | // for each element iElem for the object iObj |
---|
217 | // set it as unmatched (marker: -1) |
---|
218 | m_apvMatched[ iObj ]->operator[](iElem) = -1; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | // the exit condition |
---|
223 | assert(IsEmpty() == true); |
---|
224 | } |
---|
225 | |
---|
226 | /** Prints the current state of the matching |
---|
227 | */ |
---|
228 | void SimilMatching::PrintMatching() |
---|
229 | { |
---|
230 | int i; |
---|
231 | int nBigger; |
---|
232 | |
---|
233 | // check which object is bigger |
---|
234 | if (m_apvMatched[ 0 ]->size() >= m_apvMatched[ 1 ]->size()) |
---|
235 | { |
---|
236 | nBigger = 0; |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | nBigger = 1; |
---|
241 | } |
---|
242 | |
---|
243 | // print first line - indices of objects |
---|
244 | printf("[ "); |
---|
245 | for (i = 0; i < (int) m_apvMatched[ nBigger ]->size(); i++) |
---|
246 | { |
---|
247 | printf("%2i ", i); |
---|
248 | } |
---|
249 | printf("]\n"); |
---|
250 | |
---|
251 | // print second line and third - indices of elements matched with elements of the objects |
---|
252 | for (int nObj = 0; nObj < 2; nObj++) |
---|
253 | { |
---|
254 | // for both objects - print out lines of matched elements |
---|
255 | printf("[ "); |
---|
256 | for (i = 0; i < (int) m_apvMatched[ nObj ]->size(); i++) |
---|
257 | { |
---|
258 | if (IsMatched(nObj, i)) |
---|
259 | { |
---|
260 | // if the element is matched - print the index |
---|
261 | printf("%2i ", GetMatchedIndex(nObj, i)); |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | // if the element is not matched - print "X" |
---|
266 | printf(" X "); |
---|
267 | } |
---|
268 | } |
---|
269 | printf("]\n"); |
---|
270 | } |
---|
271 | } |
---|