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