File: group__ComparatorUtils.html

package info (click to toggle)
openms 1.11.1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 436,688 kB
  • ctags: 150,907
  • sloc: cpp: 387,126; xml: 71,547; python: 7,764; ansic: 2,626; php: 2,499; sql: 737; ruby: 342; sh: 325; makefile: 128
file content (260 lines) | stat: -rw-r--r-- 19,797 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<HTML>
<HEAD>
<TITLE>ComparatorUtils</TITLE>
<LINK HREF="doxygen.css" REL="stylesheet" TYPE="text/css">
<LINK HREF="style_ini.css" REL="stylesheet" TYPE="text/css">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<A href="index.html">Home</A> &nbsp;&middot;
<A href="classes.html">Classes</A> &nbsp;&middot;
<A href="annotated.html">Annotated Classes</A> &nbsp;&middot;
<A href="modules.html">Modules</A> &nbsp;&middot;
<A href="functions_func.html">Members</A> &nbsp;&middot;
<A href="namespaces.html">Namespaces</A> &nbsp;&middot;
<A href="pages.html">Related Pages</A>
<HR style="height:1px; border:none; border-top:1px solid #c0c0c0;">
<!-- Generated by Doxygen 1.8.5 -->
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">ComparatorUtils<div class="ingroups"><a class="el" href="group__Kernel.html">Kernel</a></div></div>  </div>
</div><!--header-->
<div class="contents">

<p>A collection of utilities for comparators.  
<a href="#details">More...</a></p>
<p>A collection of utilities for comparators. </p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="ComparatorUtils_8h.html">OpenMS/KERNEL/ComparatorUtils.h</a>&gt;</span></div>
</div><!-- fragment --><p>This file contains some lightweight class templates which simplify the (re-)usage and composition of <b>comparator classes</b>:</p>
<ul>
<li>ReverseComparator (reverse the direction of comparison)</li>
<li>LexicographicComparator (combine comparators lexicographically)</li>
<li>PointerComparator (compare pointers like the type they point to)</li>
</ul>
<p>We provide corresponding "make-functions" so that you will not need to write out the type names in the template instantiation.</p>
<p>We explain this with a simple example. First a few prerequisites.</p>
 <div class="fragment"><div class="line"><span class="comment">// --------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">//                   OpenMS -- Open-Source Mass Spectrometry</span></div>
<div class="line"><span class="comment">// --------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,</span></div>
<div class="line"><span class="comment">// ETH Zurich, and Freie Universitaet Berlin 2002-2013.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// This software is released under a three-clause BSD license:</span></div>
<div class="line"><span class="comment">//  * Redistributions of source code must retain the above copyright</span></div>
<div class="line"><span class="comment">//    notice, this list of conditions and the following disclaimer.</span></div>
<div class="line"><span class="comment">//  * Redistributions in binary form must reproduce the above copyright</span></div>
<div class="line"><span class="comment">//    notice, this list of conditions and the following disclaimer in the</span></div>
<div class="line"><span class="comment">//    documentation and/or other materials provided with the distribution.</span></div>
<div class="line"><span class="comment">//  * Neither the name of any author or any participating institution</span></div>
<div class="line"><span class="comment">//    may be used to endorse or promote products derived from this software</span></div>
<div class="line"><span class="comment">//    without specific prior written permission.</span></div>
<div class="line"><span class="comment">// For a full list of authors, refer to the file AUTHORS.</span></div>
<div class="line"><span class="comment">// --------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot;</span></div>
<div class="line"><span class="comment">// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span></div>
<div class="line"><span class="comment">// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span></div>
<div class="line"><span class="comment">// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING</span></div>
<div class="line"><span class="comment">// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,</span></div>
<div class="line"><span class="comment">// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,</span></div>
<div class="line"><span class="comment">// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;</span></div>
<div class="line"><span class="comment">// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,</span></div>
<div class="line"><span class="comment">// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR</span></div>
<div class="line"><span class="comment">// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF</span></div>
<div class="line"><span class="comment">// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="ComparatorUtils_8h.html">OpenMS/KERNEL/ComparatorUtils.h</a>&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="StandardTypes_8h.html">OpenMS/KERNEL/StandardTypes.h</a>&gt;</span></div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__Concept.html#ga7cc214a236ad3bb6ad435bdcf5262a3f">OpenMS::Int</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__Concept.html#gaba0996d26f7be2572973245b51852757">OpenMS::UInt</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__Concept.html#gaf9ecec2d692138fab9167164a457cbd4">OpenMS::Size</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__Concept.html#gab685845059e8a2c92743427d9a698c70">OpenMS::Real</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="classOpenMS_1_1String.html">OpenMS::String</a>;</div>
</div><!-- fragment --></p>
<p>The class IntRealString has three data members. The class IntRealStringVector is a vector of IntRealString. We add some print() methods for convenience. <div class="fragment"><div class="line"></div>
<div class="line"><span class="comment">// Simple class with three data members</span></div>
<div class="line"><span class="keyword">class </span>IntRealString</div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">  IntRealString(<a class="code" href="classInt.html">Int</a> i, <a class="code" href="classReal.html">Real</a> r, String s) :</div>
<div class="line">    i_(i), r_(r), s_(s) {}</div>
<div class="line">  IntRealString(<span class="keyword">const</span> IntRealString &amp; rhs) :</div>
<div class="line">    i_(rhs.i_), r_(rhs.r_), s_(rhs.s_) {}</div>
<div class="line"></div>
<div class="line">  <span class="keywordtype">void</span> print()<span class="keyword"> const</span></div>
<div class="line"><span class="keyword">  </span>{</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;(&quot;</span> &lt;&lt; i_ &lt;&lt; <span class="stringliteral">&quot;, &quot;</span> &lt;&lt; r_ &lt;&lt; <span class="stringliteral">&quot;, &quot;</span> &lt;&lt; s_ &lt;&lt; <span class="stringliteral">&quot;)&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  }</div>
<div class="line"></div>
<div class="line">  <a class="code" href="classInt.html">Int</a> i_;</div>
<div class="line">  <a class="code" href="classReal.html">Real</a> r_;</div>
<div class="line">  String s_;</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="comment">// A vector of IntRealString objects</span></div>
<div class="line"><span class="keyword">class </span>IntRealStringVector :</div>
<div class="line">  <span class="keyword">public</span> std::vector&lt;IntRealString&gt;</div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">  <span class="keywordtype">void</span> print()<span class="keyword"> const</span></div>
<div class="line"><span class="keyword">  </span>{</div>
<div class="line">    <span class="keywordflow">for</span> (Size i = 0; i &lt; size(); ++i) (*<span class="keyword">this</span>)[i].print();</div>
<div class="line">    std::cout &lt;&lt; std::endl;</div>
<div class="line">  }</div>
<div class="line"></div>
<div class="line">};</div>
</div><!-- fragment --></p>
<p>Now we will exercise various ways of sorting such a vector.</p>
<p>Of course, we could use the std::sort algorithm with a comparison function like the following. <div class="fragment"><div class="line"></div>
<div class="line"><span class="comment">// Comparison function</span></div>
<div class="line"><span class="keywordtype">bool</span> lessByInt(IntRealString left, IntRealString right)</div>
<div class="line">{</div>
<div class="line">  <span class="keywordflow">return</span> left.i_ &lt; right.i_;</div>
<div class="line">}</div>
</div><!-- fragment --></p>
<p>This is straightforward but does not generalize well. Instead we introduce three <b>comparator classes</b>: <div class="fragment"><div class="line"></div>
<div class="line"><span class="comment">// Comparator class</span></div>
<div class="line"><span class="keyword">struct </span>LessByInt :</div>
<div class="line">  std::binary_function&lt;IntRealString, IntRealString, bool&gt;</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">bool</span> operator()(IntRealString left, IntRealString right)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> left.i_ &lt; right.i_; }</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="comment">// Comparator class</span></div>
<div class="line"><span class="keyword">struct </span>LessByReal :</div>
<div class="line">  std::binary_function&lt;IntRealString, IntRealString, bool&gt;</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">bool</span> operator()(IntRealString left, IntRealString right)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> left.r_ &lt; right.r_; }</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="comment">// Comparator class</span></div>
<div class="line"><span class="keyword">struct </span>LessByString :</div>
<div class="line">  std::binary_function&lt;IntRealString, IntRealString, bool&gt;</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">bool</span> operator()(IntRealString left, IntRealString right)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> left.s_ &lt; right.s_; }</div>
<div class="line">};</div>
</div><!-- fragment --> Note how the std::binary_function template provides the necessary type information (sometimes this is called "reflection").</p>
<p>Now we show various uses of the reverseComparator and lexicographicComparator function templates. <div class="fragment"><div class="line"></div>
<div class="line"><a class="code" href="classInt.html">Int</a> <a class="code" href="RNPxl_8C.html#a217dbf8b442f20279ea00b898af96f52">main</a>()</div>
<div class="line">{</div>
<div class="line">  IntRealStringVector vec;</div>
<div class="line">  vec.push_back(IntRealString(1, 4.5f, <span class="stringliteral">&quot;paul&quot;</span>));</div>
<div class="line">  vec.push_back(IntRealString(2, 4.5f, <span class="stringliteral">&quot;josie&quot;</span>));</div>
<div class="line">  vec.push_back(IntRealString(1, 4.5f, <span class="stringliteral">&quot;john&quot;</span>));</div>
<div class="line">  vec.push_back(IntRealString(2, 3.9f, <span class="stringliteral">&quot;kim&quot;</span>));</div>
<div class="line"></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;After initialization:&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  vec.print();</div>
<div class="line"></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Sorted using lessByInt function:&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  std::sort(vec.begin(), vec.end(), lessByInt);</div>
<div class="line">  vec.print();</div>
<div class="line"></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Sorted using LessByInt comparator class:&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  std::sort(vec.begin(), vec.end(), LessByInt());</div>
<div class="line">  vec.print();</div>
<div class="line"></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Sorted using reversed LessByInt comparator class:&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  std::sort(vec.begin(), vec.end(), <a class="code" href="namespaceOpenMS.html#a74e80d32be63268d8a46039297e18cc6">OpenMS::reverseComparator</a>(LessByInt()));</div>
<div class="line">  vec.print();</div>
<div class="line"></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Sorted using lexicographic order: 1. LessByInt, 2. LessByReal&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  std::sort(vec.begin(), vec.end(), <a class="code" href="namespaceOpenMS.html#ac4e734db514973176bfe65e0fa9905ae">OpenMS::lexicographicComparator</a>(LessByInt(), LessByReal()));</div>
<div class="line">  vec.print();</div>
<div class="line"></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Sorted using lexicographic order: 1. reversed LessByInt, 2. LessByReal, 3. LessByString&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  std::sort(vec.begin(), vec.end(),</div>
<div class="line">            <a class="code" href="namespaceOpenMS.html#ac4e734db514973176bfe65e0fa9905ae">OpenMS::lexicographicComparator</a></div>
<div class="line">              (<a class="code" href="namespaceOpenMS.html#ac4e734db514973176bfe65e0fa9905ae">OpenMS::lexicographicComparator</a></div>
<div class="line">              (</div>
<div class="line">                <a class="code" href="namespaceOpenMS.html#a74e80d32be63268d8a46039297e18cc6">OpenMS::reverseComparator</a>(LessByInt()),</div>
<div class="line">                LessByReal()</div>
<div class="line">              ),</div>
<div class="line">              LessByString()</div>
<div class="line">              )</div>
<div class="line">            );</div>
<div class="line">  vec.print();</div>
</div><!-- fragment --></p>
<p>And here is an application of the pointerComparator function template: <div class="fragment"><div class="line"></div>
<div class="line">  <span class="comment">// vector of pointers into vec</span></div>
<div class="line">  std::vector&lt;const IntRealString *&gt; ptr_vec;</div>
<div class="line">  <span class="keywordflow">for</span> (Size i = 0; i &lt; vec.size(); ++i)</div>
<div class="line">  {</div>
<div class="line">    ptr_vec.push_back(&amp;vec[i]);</div>
<div class="line">  }</div>
<div class="line"></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;ptr_vec before sorting&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  <span class="keywordflow">for</span> (Size i = 0; i &lt; ptr_vec.size(); ++i)</div>
<div class="line">    ptr_vec[i]-&gt;print();</div>
<div class="line">  std::cout &lt;&lt; std::endl;</div>
<div class="line"></div>
<div class="line">  std::sort(ptr_vec.begin(), ptr_vec.end(), <a class="code" href="namespaceOpenMS.html#aeb071107569224c672893cd945dff7fe">OpenMS::pointerComparator</a>(LessByString()));</div>
<div class="line"></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;ptr_vec after sorting with pointerComparator(LessByString())&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  <span class="keywordflow">for</span> (Size i = 0; i &lt; ptr_vec.size(); ++i)</div>
<div class="line">    ptr_vec[i]-&gt;print();</div>
<div class="line">  std::cout &lt;&lt; std::endl;</div>
<div class="line"></div>
<div class="line">  <span class="keywordflow">return</span> 0;</div>
<div class="line">} <span class="comment">//end of main</span></div>
</div><!-- fragment --></p>
<p>The output of the example program is: </p>
<div class="fragment"><div class="line">After initialization:</div>
<div class="line">  (1, 4.5, paul)</div>
<div class="line">  (2, 4.5, josie)</div>
<div class="line">  (1, 4.5, john)</div>
<div class="line">  (2, 3.9, kim)</div>
<div class="line"></div>
<div class="line">Sorted <span class="keyword">using</span> lessByInt <span class="keyword">function</span>:</div>
<div class="line">  (1, 4.5, paul)</div>
<div class="line">  (1, 4.5, john)</div>
<div class="line">  (2, 4.5, josie)</div>
<div class="line">  (2, 3.9, kim)</div>
<div class="line"></div>
<div class="line">Sorted <span class="keyword">using</span> LessByInt comparator <span class="keyword">class</span>:</div>
<div class="line">  (1, 4.5, paul)</div>
<div class="line">  (1, 4.5, john)</div>
<div class="line">  (2, 4.5, josie)</div>
<div class="line">  (2, 3.9, kim)</div>
<div class="line"></div>
<div class="line">Sorted <span class="keyword">using</span> reversed LessByInt comparator <span class="keyword">class</span>:</div>
<div class="line">  (2, 4.5, josie)</div>
<div class="line">  (2, 3.9, kim)</div>
<div class="line">  (1, 4.5, paul)</div>
<div class="line">  (1, 4.5, john)</div>
<div class="line"></div>
<div class="line">Sorted <span class="keyword">using</span> lexicographic order: 1. LessByInt, 2. LessByReal</div>
<div class="line">  (1, 4.5, paul)</div>
<div class="line">  (1, 4.5, john)</div>
<div class="line">  (2, 3.9, kim)</div>
<div class="line">  (2, 4.5, josie)</div>
<div class="line"></div>
<div class="line">Sorted <span class="keyword">using</span> lexicographic order: 1. reversed LessByInt, 2. LessByReal, 3. LessByString</div>
<div class="line">  (2, 3.9, kim)</div>
<div class="line">  (2, 4.5, josie)</div>
<div class="line">  (1, 4.5, john)</div>
<div class="line">  (1, 4.5, paul)</div>
<div class="line"></div>
<div class="line">ptr_vec before sorting</div>
<div class="line">  (2, 3.9, kim)</div>
<div class="line">  (2, 4.5, josie)</div>
<div class="line">  (1, 4.5, john)</div>
<div class="line">  (1, 4.5, paul)</div>
<div class="line"></div>
<div class="line">ptr_vec after sorting with <a class="code" href="structOpenMS_1_1PointerComparator.html#aeb071107569224c672893cd945dff7fe">pointerComparator</a>(LessByString())</div>
<div class="line">  (1, 4.5, john)</div>
<div class="line">  (2, 4.5, josie)</div>
<div class="line">  (2, 3.9, kim)</div>
<div class="line">  (1, 4.5, paul)</div>
</div><!-- fragment --><p>Note that pointerComparator can also be used with full-blown iterator classes. (It should work with everything that provides an <a class="el" href="namespaceOpenMS.html#a938a33df5856a90299d3d7fbf9a4c02c" title="Scalar multiplication (a bit inefficient) ">operator*()</a>, but the typedefs will always be pointers.)</p>
<p>Note that these templates can also be used with different types for "left" and "right". </p>
</div><!-- contents -->
<HR style="height:1px; border:none; border-top:1px solid #c0c0c0;">
<TABLE width="100%" border="0">
<TR>
<TD><font color="#c0c0c0">OpenMS / TOPP release 1.11.1</font></TD>
<TD align="right"><font color="#c0c0c0">Documentation generated on Thu Nov 14 2013 11:19:25 using doxygen 1.8.5</font></TD>
</TR>
</TABLE>
</BODY>
</HTML>