comparison d2/qtd/Array.d @ 311:8674fd5f34f4 lifetime

Added d1/d2 top directories
author maxter <spambox@d-coding.com>
date Wed, 23 Dec 2009 16:17:22 +0200
parents
children 80b52f5e97b6
comparison
equal deleted inserted replaced
310:5bcfe9e7db7f 311:8674fd5f34f4
1 /**
2 *
3 * Copyright: Copyright QtD Team, 2008-2009
4 * Authors: Max Samukha
5 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>
6 *
7 * Copyright QtD Team, 2008-2009
8 * Distributed under the Boost Software License, Version 1.0.
9 * (See accompanying file boost-license-1.0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 *
11 */
12 module qt.Array;
13
14 import
15 core.stdc.string,
16 qt.Memory;
17
18 void append(alias alloc = GCAlloc, T)(ref T[] array, T elem)
19 {
20 auto newLen = array.length + 1;
21 alloc.realloc(array.ptr, newLen * T.sizeof);
22 array = array.ptr[0..newLen];
23 array[$ - 1] = elem;
24 }
25
26 void remove(alias alloc = GCAlloc, T)(ref T[] haystack, T needle)
27 {
28 foreach (i, e; haystack)
29 {
30 if (e == needle)
31 {
32 if (haystack.length > 1)
33 {
34 memmove(haystack.ptr + i, haystack.ptr + i + 1, (haystack.length - i - 1) * T.sizeof);
35 auto newLen = haystack.length - 1;
36 alloc.realloc(haystack.ptr, newLen * T.sizeof);
37 haystack = haystack[0..newLen];
38 }
39 else
40 {
41 alloc.realloc(haystack.ptr, 0);
42 haystack = haystack[0..0];
43 }
44
45 break;
46 }
47 }
48 }
49
50 void free(alias alloc = GCAlloc)(ref T[] array)
51 {
52 free(array.ptr, array.length * T.sizeof);
53 }