1 /******************************************************************************* 2 3 Implementation of default predicate function objects used by algorithms 4 in `ocean.core.array` package. 5 6 Based on `tango.core.Array` module from Tango library. 7 8 Copyright: 9 Copyright (C) 2005-2006 Sean Kelly. 10 Some parts copyright (c) 2009-2016 dunnhumby Germany GmbH. 11 All rights reserved. 12 13 License: 14 Tango Dual License: 3-Clause BSD License / Academic Free License v3.0. 15 See LICENSE_TANGO.txt for details. 16 17 *******************************************************************************/ 18 19 module ocean.core.array.DefaultPredicates; 20 21 version (UnitTest) 22 { 23 import ocean.core.Test; 24 import ocean.transition; 25 } 26 27 struct DefaultPredicates 28 { 29 struct IsEqual ( T ) 30 { 31 static bool opCall( in T p1, in T p2 ) 32 { 33 // FIXME_IN_D2: avoid forcing const methods on objects 34 auto _p1 = cast(T) p1; 35 auto _p2 = cast(T) p2; 36 37 return !!(_p1 == _p2); 38 } 39 } 40 41 42 struct IsLess ( T ) 43 { 44 static bool opCall( in T p1, in T p2 ) 45 { 46 // FIXME_IN_D2: avoid forcing const methods on objects 47 auto _p1 = cast(T) p1; 48 auto _p2 = cast(T) p2; 49 return _p1 < _p2; 50 } 51 } 52 } 53 54 // Test to enforce that IsEqual work with both 55 // value and reference types 56 unittest 57 { 58 class C 59 { 60 int x; 61 62 mixin (genOpEquals( 63 `{ 64 auto o = cast(typeof(this)) rhs; 65 if (o is null) return false; 66 return (this.x == o.x); 67 }`)); 68 } 69 70 struct S 71 { 72 int x; 73 74 mixin (genOpEquals(" 75 { 76 return this.x == rhs.x; 77 } 78 ")); 79 } 80 81 auto c1 = new C; 82 auto c2 = new C; 83 S s1, s2; 84 85 auto r1 = DefaultPredicates.IsEqual!(C)(c1, c2); 86 auto r2 = DefaultPredicates.IsEqual!(S)(s1, s2); 87 88 test!("==")(r1, true); 89 test!("==")(r2, true); 90 }