You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

114 lines
3.5 KiB

  1. /*
  2. This file is part of BioD.
  3. Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a
  5. copy of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. DEALINGS IN THE SOFTWARE.
  19. */
  20. module bio.bam.utils.array;
  21. import std.c.string;
  22. import std.traits;
  23. /// Modifies array in-place so that $(D slice) is replaced by
  24. /// $(D replacement[]).
  25. ///
  26. /// WARNING: it's you who is responsible that $(D slice) is indeed
  27. /// a slice of $(D s).
  28. void replaceSlice(T, U)(ref T[] s, in U[] slice, in T[] replacement)
  29. if (is(Unqual!U == T))
  30. {
  31. auto offset = slice.ptr - s.ptr;
  32. auto slicelen = slice.length;
  33. auto replen = replacement.length;
  34. auto newlen = s.length - slicelen + replen;
  35. if (slicelen == replen) {
  36. s[offset .. offset + slicelen] = replacement;
  37. return;
  38. }
  39. if (replen < slicelen) {
  40. // overwrite piece of slice
  41. s[offset .. offset + replen] = replacement;
  42. // and then move the remainder
  43. memmove(s.ptr + (offset + replen),
  44. s.ptr + (offset + slicelen),
  45. (newlen - offset - replen) * T.sizeof);
  46. s.length = newlen;
  47. return;
  48. }
  49. // replen > slicelen
  50. s.length = newlen;
  51. // here, first move the remainder
  52. memmove(s.ptr + (offset + replen),
  53. s.ptr + (offset + slicelen),
  54. (newlen - offset - replen) * T.sizeof);
  55. // and then overwrite
  56. s[offset .. offset + replen] = replacement;
  57. }
  58. /// Does almost the same, but does not require $(D replacement),
  59. /// instead only its length, $(D n) bytes. This is useful for
  60. /// avoiding memory allocations.
  61. void prepareSlice(T, U)(ref T[] s, in U[] slice, size_t n)
  62. if (is(Unqual!U == T))
  63. {
  64. auto offset = slice.ptr - s.ptr;
  65. auto slicelen = slice.length;
  66. auto replen = n;
  67. auto newlen = s.length - slicelen + replen;
  68. if (slicelen == replen) {
  69. return;
  70. }
  71. if (replen < slicelen) {
  72. memmove(s.ptr + (offset + replen),
  73. s.ptr + (offset + slicelen),
  74. (newlen - offset - replen) * T.sizeof);
  75. s.length = newlen;
  76. return;
  77. }
  78. // replen > slicelen
  79. s.length = newlen;
  80. memmove(s.ptr + (offset + replen),
  81. s.ptr + (offset + slicelen),
  82. (newlen - offset - replen) * T.sizeof);
  83. }
  84. unittest {
  85. auto array = [1, 2, 3, 4, 5];
  86. replaceSlice(array, array[2 .. 4], [-1, -2, -5]);
  87. assert(array == [1, 2, -1, -2, -5, 5]);
  88. replaceSlice(array, array[1 .. 4], cast(int[])[]);
  89. assert(array == [1, -5, 5]);
  90. replaceSlice(array, array[0 .. 1], [3]);
  91. assert(array == [3, -5, 5]);
  92. }