1. ariovistus
  2. ariovistus.bitbucket.org


ariovistus  committed afcab30

added MultiCompare.

  • Participants
  • Parent commits 8812734
  • Branches default

Comments (0)

Files changed (1)

File multi_index.html

View file
-	<div id="lastupdate">Last update Fri Jul 20 19:46:47 2012
+	<div id="lastupdate">Last update Sun Jul 22 16:35:33 2012
+<dt><div class="d_decl">struct <a name="ComparisonEx"></a><u>ComparisonEx</u>(alias _key,alias _less);
+<dd>For use with MultiCompare<p></p>
+<dt><div class="d_decl">struct <a name="DefaultComparison"></a><u>DefaultComparison</u>(alias _less);
+<dd>For use with MultiCompare<p></p>
+<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;
+        }
+    }
+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;
+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;
+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;
+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;
+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>);
 <dt><div class="d_decl">struct <a name="ValueChangedSlots"></a><u>ValueChangedSlots</u>(L...);
 <dd>Specifies how to hook up value signals to indeces with the semantics that