digitalmars.D.learn - Best nogc method to insert element into the middle of an std.container
- Red Frog (9/9) Aug 21 2015 I know inserting into the middle of arrays isn't the most
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (16/23) Aug 21 2015 only() and chain() come to mind. The followin does not produce an
I know inserting into the middle of arrays isn't the most efficient thing to do, but I have my reasons... I could increase the length by 1 and then shuffle all the values back one at a time... but I assume it'd be better to rewrite the back half as a single chunk? I don't really know how to get ranges to play nice with container arrays, and even if I can I don't know if they allocate in GC memory... Can anyone help me out with a good way to do this?
Aug 21 2015
On 08/21/2015 08:51 AM, Red Frog wrote:I know inserting into the middle of arrays isn't the most efficient thing to do, but I have my reasons... I could increase the length by 1 and then shuffle all the values back one at a time... but I assume it'd be better to rewrite the back half as a single chunk? I don't really know how to get ranges to play nice with container arrays, and even if I can I don't know if they allocate in GC memory... Can anyone help me out with a good way to do this?only() and chain() come to mind. The followin does not produce an "array" but you can call .array the whole thing later, e.g. in main() after the nogc function returns: import std.algorithm; import std.range; nogc auto use(int[] a, int[] b) { return chain(a, 42.only, b); } void main() { auto inserted = use([ 1, 2 ], [ 3, 4 ]); assert(inserted.equal([ 1, 2, 42, 3, 4 ])); } Ali
Aug 21 2015