KWStyle - itkEquivalencyTable.cxx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkEquivalencyTable.cxx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:35 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 DEF =========================================================================*/
17 #include "itkEquivalencyTable.h"
18
19 namespace itk
20 {
21   
22 bool EquivalencyTable::Add(unsigned long a, unsigned long b)
23 {
24   unsigned long temp;
25   std::pair<Iterator, bool> result;
26   if (a == b) return false;
27   else if (a < b)
28     {  // swap a, b
29     temp = a;
30     a = b;
31     b = temp;
32     }
33   result = m_HashMap.insert( ValueType(a, b) );
34   
35   if (result.second == false)
36     { // Stop endless loops.
37     if ( (*(result.first)).second  == b ) return false;
38     else return (this->Add((*(result.first)).second, b));
39     }
40   else return true;
41 }
42
43
44 bool EquivalencyTable::AddAndFlatten(unsigned long a, unsigned long b)
45 {
46   unsigned long temp;
47   std::pair<Iterator, bool> result;
48   if (a == b) return false;
49   else if (a < b)
50     {  // swap a, b
51     temp = a;
52     a = b;
53     b = temp;
54     }
55
56   unsigned long bFlattened = this->RecursiveLookup(b);
57   result = m_HashMap.insert( ValueType(a, bFlattened) );  
58   
59   if (result.second == false)
60     { // Stop endless loops.
61     if ( (*(result.first)).second  == bFlattened ) return false;
62     else return (this->Add((*(result.first)).second, bFlattened));
63     }
64   else
65     {
66     if (b != bFlattened)
67       {
68       // flatten b as well
69       m_HashMap.insert( ValueType(b, bFlattened) );
70       return true;
71       }
72     }
73   
74   return false;
75 }
76
77
78 //void EquivalencyTable::PrintHashTable()
79 //{
80 IND //  ConstIterator it = this->Begin();
81 IND //  while (it != this->End() )
82 IND //    {
83 IND //      std::cout << (*it).first << " = " << (*it).second << std::endl;
84 //      it++;
85 //    }
86 //}
87
88 void EquivalencyTable::Flatten()
89 {
90   Iterator it = this->Begin();
91   while ( it != this->End() )
92     {
93     (*it).second = this->RecursiveLookup((*it).second);
94     it++;
95     }
96 }
97
98 unsigned long EquivalencyTable::RecursiveLookup(const unsigned a) const
99 {
100   unsigned long ans = a;
101   unsigned long last_ans=a;
102
103   ConstIterator it;
104   ConstIterator hashEnd = m_HashMap.end();
105   while ( (it = m_HashMap.find(ans)) != hashEnd )
106     {
107     ans = (*it).second;
108     if (ans == a ) return last_ans; // about to cycle again.
109     last_ans = ans;
110     }
111
112   return ans;
113 }
114
115
116 void EquivalencyTable
117 ::PrintSelf(std::ostream& os, Indent indent) const
118 {
119   Superclass::PrintSelf(os,indent);
120 }
121
122   
123 }// end namespace itk
124
125 EOF

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:06PM
© Kitware Inc.