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