| 1 |
/** |
| 2 |
* This module contains compiler support determining equality of arrays. |
| 3 |
* |
| 4 |
* Copyright: Copyright Digital Mars 2000 - 2020. |
| 5 |
* License: Distributed under the |
| 6 |
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 7 |
* (See accompanying file LICENSE) |
| 8 |
* Source: $(DRUNTIMESRC core/internal/_array/_equality.d) |
| 9 |
*/ |
| 10 |
|
| 11 |
module core.internal.array.equality; |
| 12 |
|
| 13 |
// The compiler lowers `lhs == rhs` to `__equals(lhs, rhs)` for |
| 14 |
// * dynamic arrays, |
| 15 |
// * (most) arrays of different (unqualified) element types, and |
| 16 |
// * arrays of structs with custom opEquals. |
| 17 |
|
| 18 |
// The scalar-only overload takes advantage of known properties of scalars to |
| 19 |
// reduce template instantiation. This is expected to be the most common case. |
| 20 |
bool __equals(T1, T2)(scope const T1[] lhs, scope const T2[] rhs) |
| 21 |
@nogc nothrow pure @trusted |
| 22 |
if (__traits(isScalar, T1) && __traits(isScalar, T2)) |
| 23 |
{ |
| 24 |
const length = lhs.length; |
| 25 |
|
| 26 |
static if (T1.sizeof == T2.sizeof |
| 27 |
// Signedness needs to match for types that promote to int. |
| 28 |
// (Actually it would be okay to memcmp bool[] and byte[] but that is |
| 29 |
// probably too uncommon to be worth checking for.) |
| 30 |
&& (T1.sizeof >= 4 || __traits(isUnsigned, T1) == __traits(isUnsigned, T2)) |
| 31 |
&& !__traits(isFloating, T1) && !__traits(isFloating, T2)) |
| 32 |
{ |
| 33 |
if (__ctfe) |
| 34 |
return length == rhs.length && isEqual(lhs.ptr, rhs.ptr, length); |
| 35 |
else |
| 36 |
{ |
| 37 |
// This would improperly allow equality of integers and pointers |
| 38 |
// but the CTFE branch will stop this function from compiling then. |
| 39 |
import core.stdc.string : memcmp; |
| 40 |
return length == rhs.length && |
| 41 |
(!length || 0 == memcmp(cast(const void*) lhs.ptr, cast(const void*) rhs.ptr, length * T1.sizeof)); |
| 42 |
} |
| 43 |
} |
| 44 |
else |
| 45 |
{ |
| 46 |
return length == rhs.length && isEqual(lhs.ptr, rhs.ptr, length); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
bool __equals(T1, T2)(scope T1[] lhs, scope T2[] rhs) |
| 51 |
if (!__traits(isScalar, T1) || !__traits(isScalar, T2)) |
| 52 |
{ |
| 53 |
if (lhs.length != rhs.length) |
| 54 |
return false; |
| 55 |
|
| 56 |
if (lhs.length == 0) |
| 57 |
return true; |
| 58 |
|
| 59 |
static if (useMemcmp!(T1, T2)) |
| 60 |
{ |
| 61 |
if (!__ctfe) |
| 62 |
{ |
| 63 |
static bool trustedMemcmp(scope T1[] lhs, scope T2[] rhs) @trusted @nogc nothrow pure |
| 64 |
{ |
| 65 |
pragma(inline, true); |
| 66 |
import core.stdc.string : memcmp; |
| 67 |
return memcmp(cast(void*) lhs.ptr, cast(void*) rhs.ptr, lhs.length * T1.sizeof) == 0; |
| 68 |
} |
| 69 |
return trustedMemcmp(lhs, rhs); |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
foreach (const i; 0 .. lhs.length) |
| 74 |
{ |
| 75 |
if (at(lhs, i) != at(rhs, i)) |
| 76 |
return false; |
| 77 |
} |
| 78 |
return true; |
| 79 |
} |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
foreach (const i; 0 .. lhs.length) |
| 84 |
{ |
| 85 |
if (at(lhs, i) != at(rhs, i)) |
| 86 |
return false; |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/****************************** |
| 93 |
* Helper function for __equals(). |
| 94 |
* Outlined to enable __equals() to be inlined, as dmd cannot inline loops. |
| 95 |
*/ |
| 96 |
private |
| 97 |
bool isEqual(T1, T2)(scope const T1* t1, scope const T2* t2, size_t length) |
| 98 |
{ |
| 99 |
foreach (const i; 0 .. length) |
| 100 |
if (t1[i] != t2[i]) |
| 101 |
return false; |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
@safe unittest |
| 106 |
{ |
| 107 |
assert(__equals([], [])); |
| 108 |
assert(!__equals([1, 2], [1, 2, 3])); |
| 109 |
} |
| 110 |
|
| 111 |
@safe unittest |
| 112 |
{ |
| 113 |
auto a = "hello"c; |
| 114 |
|
| 115 |
assert(a != "hel"); |
| 116 |
assert(a != "helloo"); |
| 117 |
assert(a != "betty"); |
| 118 |
assert(a == "hello"); |
| 119 |
assert(a != "hxxxx"); |
| 120 |
|
| 121 |
float[] fa = [float.nan]; |
| 122 |
assert(fa != fa); |
| 123 |
} |
| 124 |
|
| 125 |
@safe unittest |
| 126 |
{ |
| 127 |
struct A |
| 128 |
{ |
| 129 |
int a; |
| 130 |
} |
| 131 |
|
| 132 |
auto arr1 = [A(0), A(2)]; |
| 133 |
auto arr2 = [A(0), A(1)]; |
| 134 |
auto arr3 = [A(0), A(1)]; |
| 135 |
|
| 136 |
assert(arr1 != arr2); |
| 137 |
assert(arr2 == arr3); |
| 138 |
} |
| 139 |
|
| 140 |
@safe unittest |
| 141 |
{ |
| 142 |
struct A |
| 143 |
{ |
| 144 |
int a; |
| 145 |
int b; |
| 146 |
|
| 147 |
bool opEquals(const A other) |
| 148 |
{ |
| 149 |
return this.a == other.b && this.b == other.a; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
auto arr1 = [A(1, 0), A(0, 1)]; |
| 154 |
auto arr2 = [A(1, 0), A(0, 1)]; |
| 155 |
auto arr3 = [A(0, 1), A(1, 0)]; |
| 156 |
|
| 157 |
assert(arr1 != arr2); |
| 158 |
assert(arr2 == arr3); |
| 159 |
} |
| 160 |
|
| 161 |
// https://issues.dlang.org/show_bug.cgi?id=18252 |
| 162 |
@safe unittest |
| 163 |
{ |
| 164 |
string[int][] a1, a2; |
| 165 |
assert(__equals(a1, a2)); |
| 166 |
assert(a1 == a2); |
| 167 |
a1 ~= [0: "zero"]; |
| 168 |
a2 ~= [0: "zero"]; |
| 169 |
assert(__equals(a1, a2)); |
| 170 |
assert(a1 == a2); |
| 171 |
a2[0][1] = "one"; |
| 172 |
assert(!__equals(a1, a2)); |
| 173 |
assert(a1 != a2); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
private: |
| 178 |
|
| 179 |
// - Recursively folds static array types to their element type, |
| 180 |
// - maps void to ubyte, and |
| 181 |
// - pointers to size_t. |
| 182 |
template BaseType(T) |
| 183 |
{ |
| 184 |
static if (__traits(isStaticArray, T)) |
| 185 |
alias BaseType = BaseType!(typeof(T.init[0])); |
| 186 |
else static if (is(immutable T == immutable void)) |
| 187 |
alias BaseType = ubyte; |
| 188 |
else static if (is(T == E*, E)) |
| 189 |
alias BaseType = size_t; |
| 190 |
else |
| 191 |
alias BaseType = T; |
| 192 |
} |
| 193 |
|
| 194 |
// Use memcmp if the element sizes match and both base element types are integral. |
| 195 |
// Due to int promotion, disallow small integers of diverging signed-ness though. |
| 196 |
template useMemcmp(T1, T2) |
| 197 |
{ |
| 198 |
static if (T1.sizeof != T2.sizeof) |
| 199 |
enum useMemcmp = false; |
| 200 |
else |
| 201 |
{ |
| 202 |
alias B1 = BaseType!T1; |
| 203 |
alias B2 = BaseType!T2; |
| 204 |
enum useMemcmp = __traits(isIntegral, B1) && __traits(isIntegral, B2) |
| 205 |
&& !( (B1.sizeof < 4 || B2.sizeof < 4) && __traits(isUnsigned, B1) != __traits(isUnsigned, B2) ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
unittest |
| 210 |
{ |
| 211 |
enum E { foo, bar } |
| 212 |
|
| 213 |
static assert(useMemcmp!(byte, byte)); |
| 214 |
static assert(useMemcmp!(ubyte, ubyte)); |
| 215 |
static assert(useMemcmp!(void, const void)); |
| 216 |
static assert(useMemcmp!(void, immutable bool)); |
| 217 |
static assert(useMemcmp!(void, inout char)); |
| 218 |
static assert(useMemcmp!(void, shared ubyte)); |
| 219 |
static assert(!useMemcmp!(void, byte)); // differing signed-ness |
| 220 |
static assert(!useMemcmp!(char[8], byte[8])); // ditto |
| 221 |
|
| 222 |
static assert(useMemcmp!(short, short)); |
| 223 |
static assert(useMemcmp!(wchar, ushort)); |
| 224 |
static assert(!useMemcmp!(wchar, short)); // differing signed-ness |
| 225 |
|
| 226 |
static assert(useMemcmp!(int, uint)); // no promotion, ignoring signed-ness |
| 227 |
static assert(useMemcmp!(dchar, E)); |
| 228 |
|
| 229 |
static assert(useMemcmp!(immutable void*, size_t)); |
| 230 |
static assert(useMemcmp!(double*, ptrdiff_t)); |
| 231 |
static assert(useMemcmp!(long[2][3], const(ulong)[2][3])); |
| 232 |
|
| 233 |
static assert(!useMemcmp!(float, float)); |
| 234 |
static assert(!useMemcmp!(double[2], double[2])); |
| 235 |
static assert(!useMemcmp!(Object, Object)); |
| 236 |
static assert(!useMemcmp!(int[], int[])); |
| 237 |
} |
| 238 |
|
| 239 |
// https://issues.dlang.org/show_bug.cgi?id=21094 |
| 240 |
unittest |
| 241 |
{ |
| 242 |
static class C |
| 243 |
{ |
| 244 |
int a; |
| 245 |
} |
| 246 |
static struct S |
| 247 |
{ |
| 248 |
bool isValid; |
| 249 |
C fib; |
| 250 |
|
| 251 |
inout(C) get() pure @safe @nogc nothrow inout |
| 252 |
{ |
| 253 |
return isValid ? fib : C.init; |
| 254 |
} |
| 255 |
T opCast(T : C)() const { return null; } |
| 256 |
|
| 257 |
alias get this; |
| 258 |
} |
| 259 |
|
| 260 |
auto foo(S[] lhs, S[] rhs) |
| 261 |
{ |
| 262 |
return lhs == rhs; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// Returns a reference to an array element, eliding bounds check and |
| 267 |
// casting void to ubyte. |
| 268 |
pragma(inline, true) |
| 269 |
ref at(T)(T[] r, size_t i) @trusted |
| 270 |
// exclude opaque structs due to https://issues.dlang.org/show_bug.cgi?id=20959 |
| 271 |
if (!(is(T == struct) && !is(typeof(T.sizeof)))) |
| 272 |
{ |
| 273 |
static if (is(immutable T == immutable void)) |
| 274 |
return (cast(ubyte*) r.ptr)[i]; |
| 275 |
else |
| 276 |
return r.ptr[i]; |
| 277 |
} |