Commits

ariovistus committed afcab30

added MultiCompare.

  • Participants
  • Parent commits 8812734

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