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