Mercurial > projects > orange
annotate orange/util/collection/Array.d @ 27:fc315d786f24 experimental
Added unit testing.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Fri, 19 Nov 2010 11:14:55 +0100 |
parents | 99c52d46822a |
children | 511d1ef4e299 |
rev | line source |
---|---|
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
1 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
2 * Copyright: Copyright (c) 2008-2009 Jacob Carlborg. All rights reserved. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
3 * Authors: Jacob Carlborg |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
4 * Version: Initial created: 2008 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
6 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
7 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
8 module orange.util.collection.Array; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
9 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
10 version (Tango) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
11 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
12 static import tango.core.Array; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
13 import tango.stdc.string : memmove; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
14 static import tango.text.Util; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
15 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
16 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
17 else |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
18 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
19 version = Phobos; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
20 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
21 import std.c.string : memmove; |
27 | 22 } |
23 | |
24 import orange.core.string; | |
25 import orange.util.Traits; | |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
26 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
27 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
28 * Inserts the specified element at the specified position in the array. Shifts the |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
29 * element currently at that position (if any) and any subsequent elements to the right. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
30 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
31 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
32 * arr = the array to insert the element into |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
33 * index = the index at which the specified element is to be inserted |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
34 * element = the element to be inserted |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
35 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
36 * Returns: the modified array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
37 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
38 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
39 * Throws: AssertException if the $(D_PARAM index) argument is |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
40 * greater than the length of this array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
41 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
42 T[] insert (T, U = size_t) (ref T[] arr, U index, T element) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
43 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
44 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
45 assert(arr.length > 0, "mambo.collection.Array.insert: The length of the array was 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
46 assert(index <= arr.length, "mambo.collection.Array.insert: The index was greater than the length of the array"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
47 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
48 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
49 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
50 if (index == arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
51 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
52 arr ~= element; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
53 return arr; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
54 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
55 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
56 else if (index == 0) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
57 arr = element ~ arr; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
58 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
59 else |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
60 arr = arr[0 .. index] ~ element ~ arr[index .. $]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
61 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
62 return arr; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
63 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
64 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
65 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
66 * Inserts the specified elements at the specified position in the array. Shifts the |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
67 * element currently at that position (if any) and any subsequent elements to the right. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
68 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
69 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
70 * arr = the array to insert the element into |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
71 * index = the index at which the specified element is to be inserted |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
72 * element = the element to be inserted |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
73 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
74 * Returns: the modified array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
75 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
76 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
77 * Throws: AssertException if the $(D_PARAM index) argument is |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
78 * not less or equal than the length of this array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
79 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
80 T[] insert (T, U = size_t) (ref T[] arr, U index, T[] element) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
81 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
82 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
83 assert(arr.length > 0, "mambo.collection.Array.insert: The length of the array was 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
84 assert(index <= arr.length, "mambo.collection.Array.insert: The index was greater than the length of the array"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
85 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
86 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
87 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
88 if (index == arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
89 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
90 arr ~= element; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
91 return arr; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
92 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
93 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
94 else if (index == 0) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
95 arr = element ~ arr; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
96 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
97 else |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
98 arr = arr[0 .. index] ~ element ~ arr[index .. $]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
99 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
100 return arr; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
101 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
102 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
103 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
104 * Inserts the specified element at the specified position in the array. Shifts the |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
105 * element currently at that position (if any) and any subsequent elements to the right. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
106 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
107 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
108 * arr = the array to add the element to |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
109 * index = the index at which the specified element is to be inserted |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
110 * element = the element to be inserted |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
111 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
112 * Returns: the modified array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
113 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
114 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
115 * Throws: AssertException if the $(D_PARAM index) argument is |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
116 * not less than the length of this array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
117 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
118 T[] add (T, U = size_t) (ref T[] arr, U index, T element) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
119 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
120 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
121 assert(arr.length > 0, "mambo.collection.Array.add: The length of the array was 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
122 assert(index <= arr.length, "mambo.collection.Array.add: The index was greater than the length of the array"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
123 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
124 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
125 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
126 return insert(arr, index, element); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
127 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
128 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
129 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
130 * Appends the specified element to the end of the array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
131 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
132 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
133 * arr = the array to add the element to |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
134 * element = element to be appended to this list |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
135 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
136 * Returns: the modified array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
137 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
138 T[] add (T) (ref T[] arr, T element) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
139 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
140 return arr ~= element; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
141 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
142 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
143 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
144 * Removes the element at the specified position in the array if it could find it and |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
145 * returns it. Shifts any subsequent elements to the left. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
146 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
147 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
148 * arr = the array to remove the element from |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
149 * index = the index of the element to be removed |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
150 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
151 * Returns: the element that was removed |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
152 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
153 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
154 * Throws: AssertException if the $(D_PARAM index) argument is |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
155 * not less than the length of this array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
156 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
157 T remove (T, U = size_t) (ref T[] arr, U index) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
158 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
159 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
160 assert(arr.length > 0, "mambo.collection.Array.remove: The length of the array was 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
161 assert(index < arr.length, "mambo.collection.Array.remove: The index was greater than the length of the array"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
162 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
163 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
164 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
165 T ret = arr[index]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
166 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
167 if (index == 0) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
168 arr = arr[1 .. $]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
169 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
170 else if (index == arr.length - 1) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
171 arr = arr[0 .. $ - 1]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
172 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
173 else |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
174 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
175 if (index < arr.length - 1) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
176 memmove(&arr[index], &arr[index + 1], T.sizeof * (arr.length - index - 1)); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
177 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
178 arr.length = arr.length - 1; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
179 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
180 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
181 return ret; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
182 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
183 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
184 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
185 * Removes the specified element from the array if it could find it and returns it. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
186 * Shifts any subsequent elements to the left. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
187 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
188 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
189 * arr = the array to remove the element from |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
190 * element = the element to be removed |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
191 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
192 * Returns: the element that was removed or T.max |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
193 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
194 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
195 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
196 T remove (T) (ref T[] arr, T element) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
197 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
198 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
199 assert(arr.length > 0, "mambo.collection.Array.remove: The length of the array was 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
200 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
201 out (result) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
202 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
203 assert(result is element); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
204 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
205 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
206 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
207 size_t index = arr.indexOf(element); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
208 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
209 if (index == size_t.max) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
210 return T.max; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
211 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
212 return arr.remove(index); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
213 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
214 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
215 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
216 * Returns the index of the first occurrence of the specified element in the array, or |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
217 * U.max if the array does not contain the element. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
218 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
219 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
220 * arr = the array to get the index of the element from |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
221 * element = the element to find |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
222 * start = the index where to begin the search |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
223 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
224 * Returns: the index of the element or U.max if it's not in the array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
225 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
226 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
227 * Throws: AssertException if the return value is greater or |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
228 * equal to the length of the array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
229 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
230 U indexOf (T, U = size_t) (T[] arr, T element, U start = 0) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
231 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
232 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
233 assert(start >= 0, "mambo.collection.Array.indexOf: The start index was less than 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
234 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
235 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
236 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
237 version (Tango) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
238 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
239 U index = tango.text.Util.locate(arr, element, start); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
240 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
241 if (index == arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
242 index = U.max; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
243 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
244 return index; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
245 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
246 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
247 else |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
248 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
249 if (start > arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
250 start = arr.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
251 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
252 for (U i = start; i < arr.length; i++) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
253 if (arr[i] == element) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
254 return i; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
255 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
256 return U.max; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
257 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
258 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
259 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
260 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
261 * Returns $(D_KEYWORD true) if the array contains the specified element. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
262 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
263 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
264 * arr = the array to check if it contains the element |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
265 * element = the element whose presence in the array is to be tested |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
266 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
267 * Returns: $(D_KEYWORD true) if the array contains the specified element |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
268 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
269 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
270 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
271 bool contains (T) (T[] arr, T element) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
272 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
273 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
274 assert(arr.length > 0, "mambo.collection.Array.contains: The length of the array was 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
275 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
276 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
277 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
278 return arr.indexOf!(T, size_t)(element) < size_t.max; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
279 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
280 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
281 /** |
27 | 282 * Returns $(D_KEYWORD true) if the array contains the given pattern. |
283 * | |
284 * Params: | |
285 * arr = the array to check if it contains the element | |
286 * pattern = the pattern whose presence in the array is to be tested | |
287 * | |
288 * Returns: $(D_KEYWORD true) if the array contains the given pattern | |
289 */ | |
290 bool contains (T) (T[] arr, T[] pattern) | |
291 { | |
292 static if (isChar!(T)) | |
293 { | |
294 version (Tango) | |
295 return tango.text.Util.containsPattern(arr, pattern); | |
296 | |
297 else | |
298 return stdString.indexOf(arr, element) != -1; | |
299 } | |
300 | |
301 else | |
302 { | |
303 version (Tango) | |
304 return tango.core.Array.contains(arr, pattern); | |
305 | |
306 else | |
307 return !algorithm.find(arr, pattern).empty; | |
308 } | |
309 } | |
310 | |
311 /** | |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
312 * Returns $(D_KEYWORD true) if this array contains no elements. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
313 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
314 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
315 * arr = the array to check if it's empty |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
316 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
317 * Returns: $(D_KEYWORD true) if this array contains no elements |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
318 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
319 bool isEmpty (T) (T[] arr) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
320 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
321 return arr.length == 0; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
322 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
323 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
324 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
325 * Returns $(D_KEYWORD true) if this array contains no elements. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
326 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
327 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
328 * arr = the array to check if it's empty |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
329 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
330 * Returns: $(D_KEYWORD true) if this array contains no elements |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
331 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
332 alias isEmpty empty; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
333 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
334 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
335 * Removes all of the elements from this array. The array will be empty after this call |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
336 * returns. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
337 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
338 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
339 * arr = the array to clear |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
340 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
341 * Returns: the cleared array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
342 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
343 * Throws: AssertException if length of the return array isn't 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
344 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
345 T[] clear (T) (T[] arr) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
346 out (result) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
347 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
348 assert(result.length == 0, "mambo.collection.Array.clear: The length of the resulting array was not 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
349 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
350 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
351 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
352 return arr.length = 0; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
353 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
354 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
355 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
356 * Returns the element at the specified position in the array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
357 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
358 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
359 * arr = the array to get the element from |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
360 * index = index of the element to return |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
361 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
362 * Returns: the element at the specified position in the array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
363 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
364 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
365 * Throws: AssertException if the $(D_PARAM index) argument is |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
366 * not less than the length of this array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
367 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
368 T get (T, U = size_t) (T[] arr, U index) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
369 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
370 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
371 assert(arr.length > 0, "mambo.collection.Array.get: The length of the array was 0"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
372 assert(index < arr.length, "mambo.collection.Array.get: The index was greater than the length of the array"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
373 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
374 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
375 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
376 return arr[index]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
377 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
378 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
379 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
380 * Returns the index of the last occurrence of the specifed element |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
381 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
382 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
383 * arr = the array to get the index of the element from |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
384 * element = the element to find the index of |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
385 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
386 * Returns: the index of the last occurrence of the element in the |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
387 * specified array, or U.max |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
388 * if the element does not occur. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
389 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
390 * Throws: AssertException if the length of the array is 0 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
391 * Throws: AssertException if the return value is less than -1 or |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
392 * greater than the length of the array - 1. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
393 */ |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
394 version (Tango) |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
395 { |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
396 U lastIndexOf (T, U = size_t) (in T[] arr, T element) |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
397 in |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
398 { |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
399 assert(arr.length > 0, "mambo.collection.Array.lastIndexOf: The length of the array was 0"); |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
400 } |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
401 body |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
402 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
403 U index = tango.text.Util.locatePrior(arr, element); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
404 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
405 if (index == arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
406 return U.max; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
407 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
408 return index; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
409 } |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
410 } |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
411 |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
412 else |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
413 { |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
414 U lastIndexOf (T, U = size_t) (in T[] arr, dchar element) |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
415 in |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
416 { |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
417 assert(arr.length > 0, "mambo.collection.Array.lastIndexOf: The length of the array was 0"); |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
418 } |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
419 body |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
420 { |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
421 foreach_reverse (i, dchar e ; arr) |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
422 if (e is element) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
423 return i; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
424 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
425 return U.max; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
426 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
427 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
428 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
429 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
430 * Returns the number of elements in the specified array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
431 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
432 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
433 * arr = the array to get the number of elements from |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
434 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
435 * Returns: the number of elements in this list |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
436 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
437 U size (T, U = size_t) (T[] arr) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
438 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
439 return arr.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
440 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
441 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
442 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
443 * Finds the first occurence of element in arr |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
444 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
445 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
446 * arr = the array to find the element in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
447 * element = the element to find |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
448 * start = at which position to start finding |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
449 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
450 * Returns: the index of the first match or U.max if no match was found. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
451 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
452 alias indexOf find; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
453 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
454 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
455 * Replaces a section of $(D_PARAM arr) with elements starting at $(D_PARAM pos) ending |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
456 * $(D_PARAM n) elements after |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
457 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
458 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
459 * arr = the array to do the replace in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
460 * pos = position within the array of the first element of the section to be replaced |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
461 * n = length of the section to be replaced within the array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
462 * elements = the elements to replace with |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
463 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
464 * Throws: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
465 * AssertException if pos is greater than the length of the array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
466 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
467 * Returns: the array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
468 */ |
27 | 469 /*T[] replace (T, U = size_t) (ref T[] arr, U pos, U n, T[] elements) |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
470 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
471 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
472 assert(pos <= arr.length, "mambo.collection.Array.replace: The position was greter than the length of the array"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
473 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
474 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
475 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
476 U i; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
477 U nr = n; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
478 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
479 if (nr > arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
480 nr = arr.length - 1; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
481 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
482 if (nr == elements.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
483 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
484 U eIndex; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
485 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
486 for (i = pos, eIndex = 0; i <= nr; i++, eIndex++) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
487 arr[i] = elements[eIndex]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
488 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
489 return arr; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
490 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
491 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
492 else if (elements.length == 0) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
493 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
494 U index = pos + n; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
495 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
496 if (index >= arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
497 index = arr.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
498 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
499 return arr = arr[0 .. pos] ~ arr[index .. $]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
500 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
501 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
502 else |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
503 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
504 U eIndex; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
505 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
506 for (i = pos, eIndex = 0; eIndex < nr && i < arr.length && eIndex < elements.length; i++, eIndex++) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
507 arr[i] = elements[eIndex]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
508 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
509 // no positions left and elements are left in elements, insert elements |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
510 if (eIndex < elements.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
511 return arr = arr[0 .. i] ~ elements[eIndex .. $] ~ arr[i .. $]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
512 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
513 // positions left to replace but no elements, remove those positions |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
514 else if (nr > eIndex) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
515 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
516 U index = pos + nr; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
517 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
518 if (index >= arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
519 index = arr.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
520 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
521 return arr = arr[0 .. pos + eIndex] ~ arr[index .. $]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
522 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
523 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
524 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
525 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
526 return arr; |
27 | 527 }*/ |
528 | |
529 /** | |
530 * Replaces all the occurences of $(D_PARAM pattern) with $(D_PARAM replacement) | |
531 * | |
532 * Params: | |
533 * arr = the array to do the raplce in | |
534 * pattern = the pattern to match | |
535 * replacement = the values to subsitute | |
536 * | |
537 * Returns: the passed in array with all the patterns subsituted | |
538 */ | |
539 T[] replace (T : wchar) (T[] arr, dchar pattern, dchar replacement) | |
540 { | |
541 foreach (i, dchar e ; arr) | |
542 if (e == pattern) | |
543 arr[i] = replacement; | |
544 | |
545 return arr; | |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
546 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
547 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
548 /** |
27 | 549 * Replaces all the occurences of $(D_PARAM pattern) with $(D_PARAM replacement) |
550 * | |
551 * Params: | |
552 * arr = the array to do the raplce in | |
553 * pattern = the pattern to match | |
554 * replacement = the values to subsitute | |
555 * | |
556 * Returns: the passed in array with all the patterns subsituted | |
557 */ | |
558 /*T[] replace (T) (T[] arr, T pattern, T replacement) | |
559 { | |
560 version (Tango) | |
561 tango.core.Array.replace(arr, pattern, replacement); | |
562 | |
563 | |
564 else | |
565 { | |
566 foreach (ref e ; arr) | |
567 if (e == pattern) | |
568 e = replacement; | |
569 } | |
570 | |
571 return arr; | |
572 }*/ | |
573 | |
574 /** | |
1
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
575 * Erases a part of the array content, shortening the length of the array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
576 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
577 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
578 * arr = the array to erase elements from |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
579 * start = the position within the array of the first element to be erased |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
580 * n = amount of elements to be removed. It may remove less elements if the |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
581 * end of the array is reached before the n elements have been erased. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
582 * The default value of n indicates that all the elements until the end |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
583 * of the array should be erased. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
584 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
585 * Throws: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
586 * AssertException if pos is greater than the length of the array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
587 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
588 * Returns: the array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
589 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
590 T[] erase (T, U = size_t) (ref T[] arr, U start = 0, U n = U.max) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
591 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
592 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
593 assert(start <= arr.length, "mambo.collection.Array.erase: The start position was greater than the length of the array"); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
594 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
595 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
596 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
597 U end; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
598 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
599 if (n == U.max) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
600 end = arr.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
601 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
602 else |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
603 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
604 end = start + n; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
605 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
606 if (end > arr.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
607 end = arr.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
608 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
609 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
610 return arr = arr[0 .. start] ~ arr[end .. $]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
611 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
612 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
613 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
614 * Compares to arrays. Returns 0 if the content matches, less than zero |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
615 * if a is "less" than b, or greater than zero where a is "bigger". |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
616 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
617 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
618 * a = the first array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
619 * b = the second array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
620 * end = the index where the comparision will end |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
621 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
622 * Returns: Returns 0 if the content matches, less than zero if a is |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
623 * "less" than b, or greater than zero where a is "bigger". |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
624 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
625 int compare (T, U = size_t) (T[] a, T[] b, U end = U.max) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
626 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
627 U ia = end; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
628 U ib = end; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
629 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
630 if (ia > a.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
631 ia = a.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
632 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
633 if (ib > b.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
634 ib = b.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
635 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
636 return typeid(T[]).compare(&a[0 .. ia], &b[0 .. ib]); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
637 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
638 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
639 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
640 * Compares the content of the given array to the content of a comparing |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
641 * array, which is formed according to the arguments passed. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
642 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
643 * The function returns 0 if all the elements in the compared contents compare |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
644 * equal, a negative value if the first element that does not match compares to |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
645 * less in the array than in the comparing array, and a positive value in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
646 * the opposite case. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
647 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
648 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
649 * a = the first array to compare with |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
650 * pos = position of the beginning of the compared slice, i.e. the first element in the array (in a) to be compared against the comparing array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
651 * n = length of the compared slice. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
652 * b = array with the content to be used as comparing array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
653 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
654 * Returns: 0 if the compared array are equal, otherwise a number different from 0 is returned, with its sign indicating whether the array is considered greater than the comparing array passed as parameter (positive sign), or smaller (negative sign). |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
655 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
656 * Throws: AssertException if pos is specified with a position greater than the length of the corresponding array |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
657 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
658 int compare (T, U = size_t) (T[] a, size_t pos, size_t n, T[] b) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
659 in |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
660 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
661 assert(pos <= b.length); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
662 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
663 body |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
664 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
665 U end = pos + n; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
666 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
667 if (end > b.length) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
668 end = b.length; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
669 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
670 return typeid(T[]).compare(&b[pos .. end], &a[0 .. $]); |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
671 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
672 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
673 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
674 * Reserves the given amount of allocated storage for the given array. |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
675 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
676 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
677 * a = the array to reserve allocated storage for |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
678 * capacity = the amount of allocated storage to be reserved |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
679 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
680 void reserve (T) (ref T[] a, size_t amount = 0) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
681 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
682 a = (new T[amount])[0 .. 0]; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
683 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
684 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
685 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
686 * Returns true if a begins with b |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
687 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
688 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
689 * a = the array to |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
690 * b = |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
691 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
692 * Returns: true if a begins with b, otherwise false |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
693 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
694 bool beginsWith (T) (T[] a, T[] b) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
695 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
696 return a.length > b.length && a[0 .. b.length] == b; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
697 } |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
698 |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
699 /** |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
700 * Returns true if a ends with b |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
701 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
702 * Params: |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
703 * a = the array to |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
704 * b = |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
705 * |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
706 * Returns: true if a ends with b, otherwise false |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
707 */ |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
708 bool endsWith (T) (T[] a, T[] b) |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
709 { |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
710 return a.length > b.length && a[$ - b.length .. $] == b; |
11a31bd929f9
Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff
changeset
|
711 } |