DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkFunctorHashTable.h
Go to the documentation of this file.
1 
18 #ifndef __itkFunctorHashTable_h
19 #define __itkFunctorHashTable_h
20 
21 #include "itkFunctorTableBase.h"
22 #include "utlSTDHeaders.h"
23 
24 namespace itk
25 {
26 
34 template < class TFunctor, class TParameters, class TFunctorValue = double, class THash=utl_hash<TParameters> >
35 class ITK_EXPORT FunctorHashTable
36  : public FunctorTableBase<TFunctor, TParameters, TFunctorValue>
37 {
38 public:
42  typedef SmartPointer<Self> Pointer;
43  typedef SmartPointer<const Self> ConstPointer;
44 
46  itkNewMacro(Self);
47 
50 
54 
57 
58  typedef THash HashType;
59  typedef utl_unordered_map<ParametersType,FunctorValueType, HashType > HashTableType;
60  typedef utl_shared_ptr<HashTableType > HashTablePointer;
61  typedef typename HashTableType::iterator HashTableIterator;
62 
63 
64  // itkSetMacro(Hash, HashTablePointer);
65  itkGetMacro(Hash, HashTablePointer);
66 
67  unsigned long GetTableSize() const
68  {
69  return m_Hash->size();
70  }
71 
72  bool IsTableBuilt() const
73  {
74  return m_Hash->size()>0;
75  }
76 
77  FunctorValueType GetFunctionValue ( const ParametersType& param)
78  {
79  HashTableIterator iter = m_Hash->find(param);
80  if ( iter!= m_Hash->end() )
81  {
82  // if (this->GetDebug())
83  // std::cout << "parameter = " << param << ", is in the table" << std::endl << std::flush;
84  return iter->second;
85  }
86  else
87  {
88  // if (this->GetDebug())
89  // std::cout << "parameter = " << param << ", is not in the table" << std::endl << std::flush;
90  FunctorValueType val = m_Functor(param);
91  m_Hash->insert(std::pair<ParametersType, FunctorValueType> (param, val));
92  // (*m_Hash)[param]=val;
93  return val;
94  }
95  }
96 
97 protected:
98  FunctorHashTable() : Superclass(),
99  m_Hash(new HashTableType())
100  {
101  }
102  virtual ~FunctorHashTable() {};
103 
104  void PrintSelf(std::ostream& os, Indent indent) const ITK_OVERRIDE
105  {
106  Superclass::PrintSelf(os, indent);
107  }
108 
109  typename LightObject::Pointer InternalClone() const ITK_OVERRIDE
110  {
111  typename LightObject::Pointer loPtr = Superclass::InternalClone();
112  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
113  if(rval.IsNull())
114  {
115  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
116  }
117  rval->m_Hash = m_Hash;
118  return loPtr;
119  }
120 
121  FunctorType m_Functor;
122 
123  HashTablePointer m_Hash;
124 
125 
126 private:
127  FunctorHashTable(const Self&); //purposely not implemented
128  void operator=(const Self&); //purposely not implemented
129 };
130 
131 }
132 
133 #endif
134 
SmartPointer< const Self > ConstPointer
std::vector< double > STDVectorType
FunctorTableBase< TFunctor, TParameters, TFunctorValue > Superclass
use FunctorHashTable to accelerate evaluation of functions.
SmartPointer< Self > Pointer
Superclass::STDVectorPointer STDVectorPointer
Superclass::ParametersType ParametersType
#define ITK_OVERRIDE
Definition: utlITKMacro.h:46
Superclass::STDVectorType STDVectorType
Superclass::FunctorType FunctorType
utl_unordered_map< ParametersType, FunctorValueType, HashType > HashTableType
unsigned long GetTableSize() const
TFunctorValue FunctorValueType
LightObject::Pointer InternalClone() const ITK_OVERRIDE
FunctorValueType GetFunctionValue(const ParametersType &param)
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
HashTableType::iterator HashTableIterator
utl_shared_ptr< HashTableType > HashTablePointer
Superclass::FunctorValueType FunctorValueType
utl_shared_ptr< STDVectorType > STDVectorPointer
use FunctorTableBase to accelerate evaluation of functions.