]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeComparators.h
Starting version 2
[creaImageIO.git] / src2 / creaImageIOTreeComparators.h
1 #ifndef __creaImageIOTreeNodeComparators_h_INCLUDED__
2 #define __creaImageIOTreeNodeComparators_h_INCLUDED__
3
4 #include <vector>
5 #include <iostream>
6
7 namespace creaImageIO
8 {
9
10   namespace tree
11   {
12
13     
14     class Node;
15     
16     
17
18   //=====================================================================
19     /// Abstract definition of a comparator of Node
20   struct Comparator
21   {
22     virtual ~Comparator() {}
23     virtual bool operator() (Node* const & x, Node* const & y) = 0;
24   };
25   //=====================================================================
26
27
28
29   //=====================================================================
30     /// Abstract Comparator whose order can be reversed
31   struct ComparatorWithOrder : public Comparator
32   {
33     ComparatorWithOrder(bool reverse_order = false) 
34       : mReverseOrder(reverse_order) {}
35     virtual ~ComparatorWithOrder() {}
36
37
38     virtual bool compare(Node* const &, Node* const &) = 0;
39
40     virtual bool operator() (Node* const & x, Node* const & y)
41     {
42       if (mReverseOrder) return this->compare(y,x);
43       return this->compare(x,y);
44     };
45
46     bool mReverseOrder;
47   };
48   //=====================================================================
49
50
51
52   //=====================================================================
53     /// A Comparator which stores a vector of Comparators and 
54                                /// which performs lexicographical comparison
55   class LexicographicalComparator : public Comparator
56   {
57   public:
58     LexicographicalComparator(const std::string& name) 
59       : mName(name) {}
60     ~LexicographicalComparator() {}
61
62     const std::string& GetName() const { return mName; }
63     void SetName(const std::string& s) { mName = s; }
64     void Clear() { mComparator.clear(); }
65     void DeleteComparators() 
66     { 
67        std::vector<Comparator*>::iterator i;
68        for (i =mComparator.begin();
69             i!=mComparator.end();
70             ++i)
71          {
72            delete *i;
73          }
74        mComparator.clear(); 
75     }
76     void Add(Comparator* c) { mComparator.push_back(c); }
77     
78     bool operator() (Node* const & x, Node * const & y);
79
80   private:
81     std::string mName;
82     std::vector<Comparator*> mComparator;
83   };
84   //=====================================================================
85
86
87
88
89   //=================================================================== 
90     /// Comparator which compares the values of a given Attribute of the Nodes which is decoded as an int value
91   struct IntComparator : 
92     public ComparatorWithOrder
93   {    
94     IntComparator(const std::string& key,
95                                      bool reverse_order)
96       :
97       ComparatorWithOrder(reverse_order),
98       mKey(key)
99     {}
100     virtual bool compare(Node* const & x, Node* const & y);
101
102   private:
103     std::string mKey;
104   };
105   //===================================================================
106
107   //===================================================================
108     /// Comparator which compares the values of a given Attribute of the Nodes which is decoded as an float value
109   struct FloatComparator : 
110     public ComparatorWithOrder
111   {    
112     FloatComparator(const std::string& key,
113                                        bool reverse_order )
114       : 
115       ComparatorWithOrder(reverse_order),
116       mKey(key)
117     {}
118
119     virtual bool compare(Node* const & x, Node* const & y);
120
121   private:
122     std::string mKey;
123   };
124   //===================================================================
125
126   //===================================================================
127     /// Comparator which compares the values of a given Attribute of the Nodes which is decoded as a string value
128   struct StringComparator : 
129     public ComparatorWithOrder
130   {    
131     StringComparator(const std::string& key,
132                                    bool reverse_order )
133       : 
134       ComparatorWithOrder(reverse_order),
135       mKey(key)
136     {}
137     
138     virtual bool compare(Node* const & x, Node* const & y);
139     
140   private:
141     std::string mKey;
142   };
143   //===================================================================
144
145   //===================================================================
146 #define INT_FIELD_COMP(NAME,FIELD)      \
147   struct Node##NAME##Comparator :       \
148     public IntComparator        \
149   {                                             \
150     Node##NAME##Comparator(bool o = false) :    \
151       IntComparator(FIELD,o)    \
152     {}                                          \
153   }
154   //==================================================================
155   
156   //===================================================================
157 #define FLOAT_FIELD_COMP(NAME,FIELD)            \
158   struct Node##NAME##Comparator :       \
159     public FloatComparator      \
160   {                                             \
161     Node##NAME##Comparator(bool o = false) :    \
162       FloatComparator(FIELD,o)  \
163     {}                                          \
164   }
165   //===================================================================
166
167   //===================================================================
168 #define STRING_FIELD_COMP(NAME,FIELD)           \
169   struct Node##NAME##Comparator :       \
170     public StringComparator     \
171   {                                             \
172     Node##NAME##Comparator(bool o = false) :    \
173       StringComparator(FIELD,o) \
174     {}                                          \
175   }
176   //===================================================================
177
178
179
180   //===================================================================
181   // Patient comparators
182   STRING_FIELD_COMP(PatientName,"A0010_0010");
183   STRING_FIELD_COMP(PatientSex, "A0010_0040");
184   STRING_FIELD_COMP(PatientBirthday, "A0010_0030");
185   //===================================================================
186
187   //===================================================================
188   // Study comparators
189   STRING_FIELD_COMP(StudyDate,"A0008_0020");
190   STRING_FIELD_COMP(StudyDescription,"A0008_1030");
191   //===================================================================
192
193   //===================================================================
194   // Series comparators
195   STRING_FIELD_COMP(Modality,"A0008_0060");
196   STRING_FIELD_COMP(SeriesDescription,"A0008_103E");
197   STRING_FIELD_COMP(SeriesDate,"A0008_0021");
198   //===================================================================
199
200   //===================================================================
201   // Image comparators
202   INT_FIELD_COMP(ImageNumber,"A0020_0013");
203   FLOAT_FIELD_COMP(SliceLocation,"A0020_1041");
204   STRING_FIELD_COMP(FullFileName,"FullFileName");
205   //===================================================================
206   } // namespace tree
207
208 } // namespace creaImageIO
209
210
211
212 #endif // #ifndef __creaImageIOComparators_h_INCLUDED__