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