ariovistus avatar ariovistus committed afcab30

added MultiCompare.

Comments (0)

Files changed (1)

 	</ul>
 	</div>
 
-	<div id="lastupdate">Last update Fri Jul 20 19:46:47 2012
+	<div id="lastupdate">Last update Sun Jul 22 16:35:33 2012
 </div>
 </div>
 -->
 <p></p>
 
 </dd>
+<dt><div class="d_decl">struct <a name="ComparisonEx"></a><u>ComparisonEx</u>(alias _key,alias _less);
+</div></dt>
+<dd>For use with MultiCompare<p></p>
+
+</dd>
+<dt><div class="d_decl">struct <a name="DefaultComparison"></a><u>DefaultComparison</u>(alias _less);
+</div></dt>
+<dd>For use with MultiCompare<p></p>
+
+</dd>
+<dt><div class="d_decl">template <a name="MultiCompare"></a><u>MultiCompare</u>(F...)</div></dt>
+<dd>Convenience template to compose comparison of a sequence of items.
+Consider when comparison of an object is dependent on more than one field:
+<pre class="d_code"><span class="d_keyword">struct</span> A {
+    <span class="d_keyword">int</span> x;
+    <span class="d_keyword">int</span> y;
+
+    <span class="d_keyword">int</span> opCmp(A other) {
+        <span class="d_keyword">if</span>(x == other.x) {
+            <span class="d_keyword">if</span>(y == other.y) {
+                <span class="d_keyword">return</span> 0;
+            }<span class="d_keyword">else</span> {
+                <span class="d_keyword">return</span> y &lt; other.y ? -1 : 1;
+            }
+        }<span class="d_keyword">else</span>{
+            <span class="d_keyword">return</span> x &lt; other.x ? -1 : 1;
+        }
+    }
+}
+</pre>
+Manual translation to a <span class="d_inlinecode">less</span> function usable by appropriate indeces
+is kind of nasty:
+<pre class="d_code"><span class="d_keyword">alias</span> binaryFun!<span class="d_string">"a.x == b.x ? a.y &lt; b.y : a.x &lt; b.x"</span> less;
+</pre>
+and gets progressively worse with more fields. An equvalent <span class="d_inlinecode">less</span>
+using <a name="MultiCompare"></a><u>MultiCompare</u>:
+<pre class="d_code"><span class="d_keyword">alias</span> <span class="d_psymbol">MultiCompare</span>!(<span class="d_string">"a.x"</span>, <span class="d_string">"a.y"</span>) less;
+</pre>
+The actual comparison operator used can be controlled on a per-field basis:
+<pre class="d_code"><span class="d_keyword">alias</span> <span class="d_psymbol">MultiCompare</span>!(<span class="d_string">"a.x"</span>, ComparisonEx!(<span class="d_string">"a.y"</span>, <span class="d_string">"a&gt;b"</span>)) less1;
+</pre>
+Or on all subsequent fields:
+<pre class="d_code"><span class="d_comment">// equivalent to less1
+</span><span class="d_keyword">alias</span> <span class="d_psymbol">MultiCompare</span>!(<span class="d_string">"a.x"</span>, DefaultComparison!<span class="d_string">"a&gt;b"</span>,<span class="d_string">"a.y"</span>) less2;
+</pre>
+By default, <a name="MultiCompare"></a><u>MultiCompare</u> uses the 'a&lt;b' less than operator.<p></p>
+
+<dl><dt><div class="d_decl">bool <a name="MultiCompare"></a><u>MultiCompare</u>(T)(T <i>a</i>, T <i>b</i>);
+</div></dt>
+<dd><p></p>
+
+</dd>
+</dl>
+</dd>
 <dt><div class="d_decl">struct <a name="ValueChangedSlots"></a><u>ValueChangedSlots</u>(L...);
 </div></dt>
 <dd>Specifies how to hook up value signals to indeces with the semantics that
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.