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 }
25 
26 struct DefaultPredicates
27 {
28     struct IsEqual ( T )
29     {
30         static bool opCall( in T p1, in T p2 )
31         {
32             // FIXME_IN_D2: avoid forcing const methods on objects
33             auto _p1 = cast(T) p1;
34             auto _p2 = cast(T) p2;
35 
36             return !!(_p1 == _p2);
37         }
38     }
39 
40 
41     struct IsLess ( T )
42     {
43         static bool opCall( in T p1, in T p2 )
44         {
45             // FIXME_IN_D2: avoid forcing const methods on objects
46             auto _p1 = cast(T) p1;
47             auto _p2 = cast(T) p2;
48             return _p1 < _p2;
49         }
50     }
51 }
52 
53 // Test to enforce that IsEqual work with both
54 // value and reference types
55 unittest
56 {
57     class C
58     {
59         int x;
60 
61         override equals_t opEquals(Object rhs)
62         {
63             auto o = cast(typeof(this)) rhs;
64             if (o is null) return false;
65             return (this.x == o.x);
66         }
67     }
68 
69     struct S
70     {
71         int x;
72 
73         bool opEquals(const typeof(this) rhs) const
74         {
75             return this.x == rhs.x;
76         }
77     }
78 
79     auto c1 = new C;
80     auto c2 = new C;
81     S s1, s2;
82 
83     auto r1 = DefaultPredicates.IsEqual!(C)(c1, c2);
84     auto r2 = DefaultPredicates.IsEqual!(S)(s1, s2);
85 
86     test!("==")(r1, true);
87     test!("==")(r2, true);
88 }