PageRenderTime 41ms CodeModel.GetById 9ms app.highlight 29ms RepoModel.GetById 2ms app.codeStats 0ms

/src/rt/typeinfo/ti_Aint.d

http://github.com/AlexeyProkhin/druntime
D | 138 lines | 99 code | 24 blank | 15 comment | 16 complexity | a0510c87dbdd849f9f49e5850bb35ad8 MD5 | raw file
  1/**
  2 * TypeInfo support code.
  3 *
  4 * Copyright: Copyright Digital Mars 2004 - 2009.
  5 * License:   <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
  6 * Authors:   Walter Bright
  7 */
  8
  9/*          Copyright Digital Mars 2004 - 2009.
 10 * Distributed under the Boost Software License, Version 1.0.
 11 *    (See accompanying file LICENSE or copy at
 12 *          http://www.boost.org/LICENSE_1_0.txt)
 13 */
 14module rt.typeinfo.ti_Aint;
 15
 16private import core.stdc.string;
 17private import rt.util.hash;
 18
 19// int[]
 20
 21class TypeInfo_Ai : TypeInfo_Array
 22{
 23    override bool opEquals(Object o) { return TypeInfo.opEquals(o); }
 24
 25    @trusted:
 26    const:
 27    pure:
 28    nothrow:
 29
 30    override string toString() const pure nothrow @safe { return "int[]"; }
 31
 32    override size_t getHash(in void* p)
 33    {
 34        int[] s = *cast(int[]*)p;
 35        return hashOf(s.ptr, s.length * int.sizeof);
 36    }
 37
 38    override bool equals(in void* p1, in void* p2)
 39    {
 40        int[] s1 = *cast(int[]*)p1;
 41        int[] s2 = *cast(int[]*)p2;
 42
 43        return s1.length == s2.length &&
 44               memcmp(cast(void *)s1, cast(void *)s2, s1.length * int.sizeof) == 0;
 45    }
 46
 47    override int compare(in void* p1, in void* p2)
 48    {
 49        int[] s1 = *cast(int[]*)p1;
 50        int[] s2 = *cast(int[]*)p2;
 51        size_t len = s1.length;
 52
 53        if (s2.length < len)
 54            len = s2.length;
 55        for (size_t u = 0; u < len; u++)
 56        {
 57            int result = s1[u] - s2[u];
 58            if (result)
 59                return result;
 60        }
 61        if (s1.length < s2.length)
 62            return -1;
 63        else if (s1.length > s2.length)
 64            return 1;
 65        return 0;
 66    }
 67
 68    override @property const(TypeInfo) next() nothrow pure
 69    {
 70        return typeid(int);
 71    }
 72}
 73
 74unittest
 75{
 76    int[][] a = [[5,3,8,7], [2,5,3,8,7]];
 77    a.sort;
 78    assert(a == [[2,5,3,8,7], [5,3,8,7]]);
 79
 80    a = [[5,3,8,7], [5,3,8]];
 81    a.sort;
 82    assert(a == [[5,3,8], [5,3,8,7]]);
 83}
 84
 85// uint[]
 86
 87class TypeInfo_Ak : TypeInfo_Ai
 88{
 89    @trusted:
 90    const:
 91    pure:
 92    nothrow:
 93
 94    override string toString() const pure nothrow @safe { return "uint[]"; }
 95
 96    override int compare(in void* p1, in void* p2)
 97    {
 98        uint[] s1 = *cast(uint[]*)p1;
 99        uint[] s2 = *cast(uint[]*)p2;
100        size_t len = s1.length;
101
102        if (s2.length < len)
103            len = s2.length;
104        for (size_t u = 0; u < len; u++)
105        {
106            int result = s1[u] - s2[u];
107            if (result)
108                return result;
109        }
110        if (s1.length < s2.length)
111            return -1;
112        else if (s1.length > s2.length)
113            return 1;
114        return 0;
115    }
116
117    override @property const(TypeInfo) next() nothrow pure
118    {
119        return typeid(uint);
120    }
121}
122
123// dchar[]
124
125class TypeInfo_Aw : TypeInfo_Ak
126{
127    @trusted:
128    const:
129    pure:
130    nothrow:
131
132    override string toString() const pure nothrow @safe { return "dchar[]"; }
133
134    override @property const(TypeInfo) next() nothrow pure
135    {
136        return typeid(dchar);
137    }
138}