annotate orange/util/collection/Array.d @ 1:11a31bd929f9

Removed dependency on private library
author Jacob Carlborg <doob@me.com>
date Mon, 31 May 2010 16:06:36 +0200
parents
children 99c52d46822a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
22 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
23
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
24 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
25 * 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
26 * 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
27 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
28 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
29 * arr = the array to insert the element into
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
30 * 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
31 * element = the element to be inserted
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
32 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
33 * Returns: the modified array
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
34 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
35 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
36 * Throws: AssertException if the $(D_PARAM index) argument is
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
37 * greater than the length of this array.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
38 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
39 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
40 in
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 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
43 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
44 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
45 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
46 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
47 if (index == arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
48 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
49 arr ~= element;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
50 return arr;
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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
53 else if (index == 0)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
54 arr = element ~ arr;
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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
57 arr = arr[0 .. index] ~ element ~ arr[index .. $];
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 return arr;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
60 }
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 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
63 * 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
64 * 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
65 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
66 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
67 * arr = the array to insert the element into
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
68 * 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
69 * element = the element to be inserted
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
70 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
71 * Returns: the modified array
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
72 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
73 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
74 * Throws: AssertException if the $(D_PARAM index) argument is
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
75 * not less or equal than the length of this array.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
76 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
77 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
78 in
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 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
81 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
82 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
83 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
84 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
85 if (index == arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
86 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
87 arr ~= element;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
88 return arr;
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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
91 else if (index == 0)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
92 arr = element ~ arr;
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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
95 arr = arr[0 .. index] ~ element ~ arr[index .. $];
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 return arr;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
98 }
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 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
101 * 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
102 * 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
103 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
104 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
105 * arr = the array to add the element to
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
106 * 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
107 * element = the element to be inserted
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
108 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
109 * Returns: the modified array
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
110 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
111 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
112 * Throws: AssertException if the $(D_PARAM index) argument is
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
113 * not less than the length of this array.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
114 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
115 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
116 in
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 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
119 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
120 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
121 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
122 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
123 return insert(arr, index, element);
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
124 }
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 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
127 * Appends the specified element to the end of the array.
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 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
130 * arr = the array to add the element to
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
131 * element = element to be appended to this list
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
132 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
133 * Returns: the modified array
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
134 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
135 T[] add (T) (ref T[] arr, T element)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
136 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
137 return arr ~= element;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
138 }
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 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
141 * 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
142 * returns it. Shifts any subsequent elements to the left.
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 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
145 * arr = the array to remove the element from
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
146 * index = the index of the element to be removed
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
147 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
148 * Returns: the element that was removed
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
149 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
150 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
151 * Throws: AssertException if the $(D_PARAM index) argument is
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
152 * not less than the length of this array.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
153 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
154 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
155 in
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 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
158 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
159 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
160 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
161 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
162 T ret = arr[index];
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
163
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
164 if (index == 0)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
165 arr = arr[1 .. $];
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 else if (index == arr.length - 1)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
168 arr = arr[0 .. $ - 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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
171 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
172 if (index < arr.length - 1)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
173 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
174
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
175 arr.length = arr.length - 1;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
176 }
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 return ret;
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 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
182 * 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
183 * Shifts any subsequent elements to the left.
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 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
186 * arr = the array to remove the element from
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
187 * element = the element to be removed
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
188 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
189 * Returns: the element that was removed or T.max
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
190 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
191 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
192 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
193 T remove (T) (ref T[] arr, T element)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
194 in
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 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
197 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
198 out (result)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
199 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
200 assert(result is element);
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
201 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
202 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
203 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
204 size_t index = arr.indexOf(element);
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
205
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
206 if (index == size_t.max)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
207 return T.max;
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 return arr.remove(index);
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
210 }
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 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
213 * 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
214 * U.max if the array does not contain the element.
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 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
217 * 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
218 * element = the element to find
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
219 * start = the index where to begin the search
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
220 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
221 * 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
222 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
223 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
224 * Throws: AssertException if the return value is greater or
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
225 * equal to the length of the array.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
226 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
227 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
228 in
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 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
231 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
232 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
233 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
234 version (Tango)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
235 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
236 U index = tango.text.Util.locate(arr, element, start);
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
237
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
238 if (index == arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
239 index = U.max;
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 return index;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
242 }
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 else
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 if (start > arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
247 start = arr.length;
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 for (U i = start; i < arr.length; i++)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
250 if (arr[i] == element)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
251 return i;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
252
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
253 return U.max;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
254 }
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
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 * 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
259 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
260 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
261 * arr = the array to check if it contains the element
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
262 * 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
263 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
264 * 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
265 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
266 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
267 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
268 bool contains (T) (T[] arr, T element)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
269 in
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 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
272 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
273 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
274 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
275 return arr.indexOf!(T, size_t)(element) < size_t.max;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
276 }
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 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
279 * Returns $(D_KEYWORD true) if this array contains no elements.
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 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
282 * arr = the array to check if it's empty
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
283 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
284 * Returns: $(D_KEYWORD true) if this array contains no elements
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
285 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
286 bool isEmpty (T) (T[] arr)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
287 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
288 return arr.length == 0;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
289 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
290
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
291 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
292 * Returns $(D_KEYWORD true) if this array contains no elements.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
293 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
294 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
295 * arr = the array to check if it's empty
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
296 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
297 * Returns: $(D_KEYWORD true) if this array contains no elements
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
298 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
299 alias isEmpty empty;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
300
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
301 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
302 * 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
303 * returns.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
304 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
305 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
306 * arr = the array to clear
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
307 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
308 * Returns: the cleared array
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
309 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
310 * 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
311 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
312 T[] clear (T) (T[] arr)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
313 out (result)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
314 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
315 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
316 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
317 body
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 return arr.length = 0;
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
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 * Returns the element at the specified position in the array.
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 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
326 * arr = the array to get the element from
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
327 * index = index of the element to return
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
328 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
329 * Returns: the element at the specified position in the array
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
330 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
331 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
332 * Throws: AssertException if the $(D_PARAM index) argument is
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
333 * not less than the length of this array.
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 T get (T, U = size_t) (T[] arr, U index)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
336 in
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 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
339 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
340 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
341 body
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 return arr[index];
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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
346 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
347 * 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
348 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
349 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
350 * 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
351 * element = the element to find the index of
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
352 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
353 * 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
354 * specified array, or U.max
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
355 * if the element does not occur.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
356 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
357 * Throws: AssertException if the length of the array is 0
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
358 * 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
359 * greater than the length of the array - 1.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
360 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
361 U lastIndexOf (T, U = size_t) (T[] arr, T element)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
362 in
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 assert(arr.length > 0, "mambo.collection.Array.lastIndexOf: The length of the array was 0");
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
365 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
366 body
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 version (Tango)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
369 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
370 U index = tango.text.Util.locatePrior(arr, element);
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
371
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
372 if (index == arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
373 return U.max;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
374
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
375 return index;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
376 }
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 else
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 foreach_reverse (i, e ; arr)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
381 if (e is element)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
382 return i;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
383
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
384 return U.max;
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 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
387
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
388 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
389 * Returns the number of elements in the specified array.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
390 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
391 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
392 * arr = the array to get the number of elements from
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
393 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
394 * Returns: the number of elements in this list
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
395 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
396 U size (T, U = size_t) (T[] arr)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
397 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
398 return arr.length;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
399 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
400
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
401 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
402 * Finds the first occurence of element in arr
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
403 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
404 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
405 * arr = the array to find the element in
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
406 * element = the element to find
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
407 * start = at which position to start finding
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
408 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
409 * 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
410 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
411 alias indexOf find;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
412
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
413 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
414 * 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
415 * $(D_PARAM n) elements after
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
416 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
417 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
418 * arr = the array to do the replace in
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
419 * 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
420 * 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
421 * elements = the elements to replace with
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
422 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
423 * Throws:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
424 * 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
425 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
426 * Returns: the array
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 T[] replace (T, U = size_t) (ref T[] arr, U pos, U n, T[] elements)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
429 in
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
430 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
431 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
432 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
433 body
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 U i;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
436 U nr = n;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
437
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
438 if (nr > arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
439 nr = arr.length - 1;
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 if (nr == elements.length)
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 U eIndex;
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 for (i = pos, eIndex = 0; i <= nr; i++, eIndex++)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
446 arr[i] = elements[eIndex];
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
447
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
448 return arr;
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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
451 else if (elements.length == 0)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
452 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
453 U index = pos + n;
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 if (index >= arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
456 index = arr.length;
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 return arr = arr[0 .. pos] ~ arr[index .. $];
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
459 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
460
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
461 else
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
462 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
463 U eIndex;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
464
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
465 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
466 arr[i] = elements[eIndex];
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
467
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
468 // 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
469 if (eIndex < elements.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
470 return arr = arr[0 .. i] ~ elements[eIndex .. $] ~ arr[i .. $];
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 // positions left to replace but no elements, remove those positions
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
473 else if (nr > eIndex)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
474 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
475 U index = pos + nr;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
476
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
477 if (index >= arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
478 index = arr.length;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
479
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
480 return arr = arr[0 .. pos + eIndex] ~ arr[index .. $];
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
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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
485 return arr;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
486 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
487
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 * 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
490 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
491 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
492 * arr = the array to erase elements from
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
493 * 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
494 * 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
495 * 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
496 * 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
497 * of the array should be erased.
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 * Throws:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
500 * 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
501 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
502 * Returns: the array
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 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
505 in
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
506 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
507 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
508 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
509 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
510 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
511 U end;
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 if (n == U.max)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
514 end = arr.length;
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 else
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 end = start + n;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
519
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
520 if (end > arr.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
521 end = arr.length;
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 return arr = arr[0 .. start] ~ arr[end .. $];
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
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
527 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
528 * 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
529 * 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
530 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
531 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
532 * a = the first array
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
533 * b = the second array
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
534 * end = the index where the comparision will end
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
535 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
536 * 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
537 * "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
538 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
539 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
540 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
541 U ia = end;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
542 U ib = end;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
543
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
544 if (ia > a.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
545 ia = a.length;
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 if (ib > b.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
548 ib = b.length;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
549
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
550 return typeid(T[]).compare(&a[0 .. ia], &b[0 .. ib]);
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
551 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
552
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
553 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
554 * 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
555 * array, which is formed according to the arguments passed.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
556 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
557 * 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
558 * 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
559 * 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
560 * the opposite case.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
561 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
562 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
563 * a = the first array to compare with
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
564 * 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
565 * n = length of the compared slice.
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
566 * 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
567 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
568 * 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
569 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
570 * 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
571 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
572 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
573 in
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
574 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
575 assert(pos <= b.length);
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 body
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
578 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
579 U end = pos + n;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
580
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
581 if (end > b.length)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
582 end = b.length;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
583
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
584 return typeid(T[]).compare(&b[pos .. end], &a[0 .. $]);
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
585 }
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
586
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 * 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
589 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
590 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
591 * a = the array to reserve allocated storage for
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
592 * capacity = the amount of allocated storage to be reserved
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
593 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
594 void reserve (T) (ref T[] a, size_t amount = 0)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
595 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
596 a = (new T[amount])[0 .. 0];
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
597 }
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 /**
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
600 * Returns true if a begins with b
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 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
603 * a = the array to
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
604 * b =
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 * Returns: true if a begins with b, otherwise false
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
607 */
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
608 bool beginsWith (T) (T[] a, T[] b)
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 a.length > b.length && a[0 .. b.length] == b;
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 * Returns true if a ends with b
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
615 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
616 * Params:
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
617 * a = the array to
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
618 * b =
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
619 *
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
620 * Returns: true if a ends with b, otherwise false
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 bool endsWith (T) (T[] a, T[] b)
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
623 {
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
624 return a.length > b.length && a[$ - b.length .. $] == b;
11a31bd929f9 Removed dependency on private library
Jacob Carlborg <doob@me.com>
parents:
diff changeset
625 }