| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkOneWayEquivalencyTable.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:43 $ |
| 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 |
|
=========================================================================*/ |
| 17 |
|
#ifndef __itkOneWayEquivalencyTable_h |
| 18 |
|
#define __itkOneWayEquivalencyTable_h |
| 19 |
|
|
| 20 |
|
#if defined(_MSC_VER) |
| 21 |
|
#pragma warning ( disable : 4786 ) |
| 22 |
|
#endif |
| 23 |
|
|
| 24 |
|
#include "itkObjectFactory.h" |
| 25 |
|
#include "itkDataObject.h" |
| 26 |
|
#include "itkProcessObject.h" |
| 27 |
|
#include "itk_hash_map.h" |
| 28 |
|
|
| 29 |
|
namespace itk |
| 30 |
|
{ |
| 31 |
|
/** \class OneWayEquivalencyTable |
| 32 |
LEN |
* \brief Hash table to manage integral label equivalencies that are order dependent. |
| 33 |
|
* |
| 34 |
|
* OneWayEquivalencyTable is a variation on itk::EquivalencyTable that |
| 35 |
|
* preserves the order of equivalencies. For example, the entries { 5 |
| 36 |
IND |
** = 4 } and {4 = 5} are not equivalent in this table, yet are the |
| 37 |
|
* treated as the same entry in EquivalencyTable. Because of the |
| 38 |
|
* one-way logic of the table, reflexive equivalencies will result in |
| 39 |
|
* cycling from recursive lookups or flattening of the table. The |
| 40 |
|
* responsibility is on the user for preventing recursive cycling. |
| 41 |
|
* |
| 42 |
|
* \par |
| 43 |
|
* See itk::EquivalencyTable for more information |
| 44 |
|
* \ingroup WatershedSegmentation |
| 45 |
|
* |
| 46 |
|
* \sa EquivalencyTable */ |
| 47 |
|
class ITKCommon_EXPORT OneWayEquivalencyTable : public DataObject |
| 48 |
|
{ |
| 49 |
|
public: |
| 50 |
|
/** Standard typedefs and smart pointer declarations. */ |
| 51 |
|
typedef OneWayEquivalencyTable Self; |
| 52 |
TDA |
typedef DataObject Superclass; |
| 53 |
TDA |
typedef SmartPointer<Self> Pointer; |
| 54 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 55 |
|
itkNewMacro(Self); |
| 56 |
|
itkTypeMacro(OneWayEquivalencyTable, DataObject); |
| 57 |
|
|
| 58 |
|
/** Define the container type for this table */ |
| 59 |
|
typedef itk::hash_map<unsigned long, unsigned long, |
| 60 |
|
itk::hash<unsigned long> > HashTableType; |
| 61 |
TDA |
typedef HashTableType::iterator Iterator; |
| 62 |
TDA |
typedef HashTableType::const_iterator ConstIterator; |
| 63 |
TDA |
typedef HashTableType::value_type ValueType; |
| 64 |
|
|
| 65 |
|
/** ``Flattens'' the equivalency table by eliminating all redundant |
| 66 |
|
* and recursive equivalencies. I.e. the set { 2=1; 3=2; 4=3 } is |
| 67 |
|
* converted to {4=1; 3=1; 2=1}. */ |
| 68 |
|
void Flatten(); |
| 69 |
|
|
| 70 |
|
/** Insert an equivalency into the table. A return value of TRUE |
| 71 |
IND |
*** indicates that the equivalency did not previously exist in the |
| 72 |
IND |
*** table and was successfully added. A FALSE return value indicates |
| 73 |
IND |
*** that the equivalency was not added to the table because a conflict |
| 74 |
IND |
*** with an existing entry occurred (most likely, the equivalency was |
| 75 |
|
* already recorded directly or indirectly). |
| 76 |
IND |
***/ |
| 77 |
|
bool Add(unsigned long a, unsigned long b); |
| 78 |
|
|
| 79 |
|
/** Lookup an equivalency in the table. If no entry is found in the |
| 80 |
|
* table, the method returns its the value of the argument. Does |
| 81 |
|
* not recursively descent through equivalencies. */ |
| 82 |
|
unsigned long Lookup(const unsigned long a) const |
| 83 |
IND |
**{ |
| 84 |
|
ConstIterator result = m_HashMap.find(a); |
| 85 |
|
if ( result == m_HashMap.end() ) return a; |
| 86 |
|
else return (*result).second; |
| 87 |
IND |
**} |
| 88 |
|
|
| 89 |
|
/** Lookup an equivalency in the table by recursing through all |
| 90 |
|
* successive equivalencies. For example, if the follow entries |
| 91 |
|
* exist in the table {8=7, 7=6, 6=5}, then RecursiveLookup(8) |
| 92 |
|
* returns 5. */ |
| 93 |
|
unsigned long RecursiveLookup(const unsigned a) const; |
| 94 |
|
|
| 95 |
|
/** Returns TRUE if the label is found in the table and FALSE is the |
| 96 |
|
* label is not found in the table. */ |
| 97 |
|
bool IsEntry(const unsigned long a) const |
| 98 |
IND |
**{ |
| 99 |
|
if ( m_HashMap.find(a) == m_HashMap.end() ) return false; |
| 100 |
|
else return true; |
| 101 |
IND |
**} |
| 102 |
|
|
| 103 |
|
/** Erases the entry with key a. */ |
| 104 |
|
void Erase(const unsigned long a) |
| 105 |
IND |
**{ m_HashMap.erase(a); } |
| 106 |
|
|
| 107 |
|
/** Erases all the entries in the table. */ |
| 108 |
|
void Clear() |
| 109 |
IND |
**{ m_HashMap.clear(); } |
| 110 |
|
|
| 111 |
|
/** Returns TRUE if the table is empty, FALSE if it is not empty. */ |
| 112 |
|
bool Empty() const |
| 113 |
IND |
**{ return m_HashMap.empty(); } |
| 114 |
|
|
| 115 |
|
/** Returns an iterator pointing to the first element of the (unordered) |
| 116 |
IND |
******table. */ |
| 117 |
|
Iterator Begin() { return m_HashMap.begin(); } |
| 118 |
|
|
| 119 |
|
/** Returns and iterator pointing to one position past the last |
| 120 |
|
* element of the (unordered) table. */ |
| 121 |
|
Iterator End() { return m_HashMap.end(); } |
| 122 |
|
|
| 123 |
|
/** Convenience method for debugging. */ |
| 124 |
|
// void PrintHashTable(); |
| 125 |
|
|
| 126 |
|
protected: |
| 127 |
|
OneWayEquivalencyTable() {} |
| 128 |
|
virtual ~OneWayEquivalencyTable() {} |
| 129 |
|
OneWayEquivalencyTable(const Self&); // purposely not implemented |
| 130 |
|
void operator=(const Self&); // purposely not implemented |
| 131 |
|
void PrintSelf(std::ostream& os, Indent indent) const; |
| 132 |
|
|
| 133 |
|
HashTableType m_HashMap; |
| 134 |
|
}; |
| 135 |
|
|
| 136 |
|
}// end namespace itk |
| 137 |
|
|
| 138 |
|
#endif |
| 139 |
|
|
| 140 |
EOF |
|