Orfeo Toolbox  4.0
itkEquivalencyTable.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #include "itkEquivalencyTable.h"
19 
20 namespace itk
21 {
22 bool EquivalencyTable::Add(unsigned long a, unsigned long b)
23 {
24  std::pair< Iterator, bool > result;
25  if ( a == b ) { return false; }
26  else if ( a < b )
27  { // swap a, b
28  unsigned long temp = a;
29  a = b;
30  b = temp;
31  }
32  result = m_HashMap.insert( ValueType(a, b) );
33 
34  if ( result.second == false )
35  { // Stop endless loops.
36  if ( ( *( result.first ) ).second == b ) { return false; }
37  else { return ( this->Add( ( *( result.first ) ).second, b ) ); }
38  }
39  else { return true; }
40 }
41 
42 bool EquivalencyTable::AddAndFlatten(unsigned long a, unsigned long b)
43 {
44  std::pair< Iterator, bool > result;
45  if ( a == b ) { return false; }
46  else if ( a < b )
47  { // swap a, b
48  unsigned long temp = a;
49  a = b;
50  b = temp;
51  }
52 
53  unsigned long bFlattened = this->RecursiveLookup(b);
54  result = m_HashMap.insert( ValueType(a, bFlattened) );
55 
56  if ( result.second == false )
57  { // Stop endless loops.
58  if ( ( *( result.first ) ).second == bFlattened ) { return false; }
59  else { return ( this->Add( ( *( result.first ) ).second, bFlattened ) ); }
60  }
61  else
62  {
63  if ( b != bFlattened )
64  {
65  // flatten b as well
66  m_HashMap.insert( ValueType(b, bFlattened) );
67  return true;
68  }
69  }
70 
71  return false;
72 }
73 
74 //void EquivalencyTable::PrintHashTable()
75 //{
76 // ConstIterator it = this->Begin();
77 // while (it != this->End() )
78 // {
79 // std::cout << (*it).first << " = " << (*it).second << std::endl;
80 // it++;
81 // }
82 //}
83 
85 {
86  Iterator it = this->Begin();
87 
88  while ( it != this->End() )
89  {
90  ( *it ).second = this->RecursiveLookup( ( *it ).second );
91  it++;
92  }
93 }
94 
95 unsigned long EquivalencyTable::RecursiveLookup(const unsigned long a) const
96 {
97  unsigned long ans = a;
98  unsigned long last_ans = a;
99 
100  ConstIterator it;
101  ConstIterator hashEnd = m_HashMap.end();
102 
103  while ( ( it = m_HashMap.find(ans) ) != hashEnd )
104  {
105  ans = ( *it ).second;
106  if ( ans == a )
107  {
108  return last_ans; // about to cycle again.
109  }
110  last_ans = ans;
111  }
112 
113  return ans;
114 }
115 
116 void EquivalencyTable
117 ::PrintSelf(std::ostream & os, Indent indent) const
118 {
119  Superclass::PrintSelf(os, indent);
120 }
121 } // end namespace itk

Generated at Sat Mar 8 2014 14:34:40 for Orfeo Toolbox with doxygen 1.8.3.1