annotate dwt/dwthelper/array.d @ 60:62202ce0039f

Updated and fixed many modules to 3.514
author Jacob Carlborg <doob@me.com>
date Mon, 22 Dec 2008 15:10:19 +0100
parents
children d32621bf0f90
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
1 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
2 * Copyright: Copyright (c) 2008 Jacob Carlborg. All rights reserved.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
3 * Authors: Jacob Carlborg
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
4 * Version: Initial created: 2008
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
5 * License: $(LINK2 http://opensource.org/licenses/bsd-license.php, BSD Style)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
6 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
7 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
8 module dwt.dwthelper.array;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
9
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
10 version (Tango)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
11 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
12 static import tango.text.Util;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
13 static import tango.core.Array;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
14 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
15
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
16 else
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
17 version = Phobos;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
18
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
19 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
20 * Appends the specified element to the end of the array.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
21 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
22 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
23 * arr = the array to add the element to
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
24 * element = element to be appended to this list
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
25 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
26 * Returns: the modified array
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
27 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
28 * Throws: AssertException if the length of the array is 0
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
29 * Throws: AssertException if the element is null
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
30 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
31 T[] add (T) (ref T[] arr, T element)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
32 in
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
33 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
34 assert(arr.length > 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
35 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
36 body
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
37 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
38 return arr ~= element;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
39 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
40
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
41 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
42 * Appends the specified element to the end of the array.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
43 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
44 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
45 * arr = the array to add the element to
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
46 * element = element to be appended to this list
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
47 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
48 * Returns: the modified array
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
49 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
50 * Throws: AssertException if the length of the array is 0
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
51 * Throws: AssertException if the element is null
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
52 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
53 alias add addElement;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
54
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
55 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
56 * Gets the element at the specified index
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
57 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
58 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
59 * arr = the array to get the element from
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
60 * index = the index of the element to get
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
61 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
62 * Returns: the element at the specified index
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
63 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
64 T elementAt (T) (T[] arr, int index)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
65 in
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
66 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
67 assert(arr.length > 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
68 assert(index > -1 && index < arr.length);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
69 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
70 body
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
71 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
72 return arr[index];
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
73 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
74
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
75 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
76 * Returns the number of elements in the specified array.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
77 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
78 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
79 * arr = the array to get the number of elements from
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
80 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
81 * Returns: the number of elements in this list
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
82 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
83 size_t size (T) (T[] arr)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
84 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
85 return arr.length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
86 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
87
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
88 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
89 * Removes the element at the specified position in the array
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
90 * if it could find it and returns it.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
91 * Shifts any subsequent elements to the left.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
92 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
93 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
94 * arr = the array to remove the element from
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
95 * index = the index of the element to be removed
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
96 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
97 * Returns: the element that was removed or $(D_CODE null)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
98 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
99 * Throws: AssertException if the length of the array is 0
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
100 * Throws: AssertException if the $(D_CODE index) argument is
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
101 * negative or not less than the length of this array.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
102 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
103 T remove (T) (ref T[] arr, int index)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
104 in
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
105 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
106 assert(arr.length > 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
107 assert(index > -1 && index < arr.length);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
108 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
109 body
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
110 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
111 T ret = arr[index];
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
112
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
113 // NOTE: Indexes are passed instead of references because DMD does
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
114 // not inline the reference-based version.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
115 void exch (size_t p1, size_t p2)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
116 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
117 T t = arr[p1];
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
118 arr[p1] = arr[p2];
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
119 arr[p2] = t;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
120 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
121
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
122 size_t cnt = 0;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
123
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
124 for (size_t pos = 0, len = arr.length; pos < len; ++pos)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
125 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
126 if (pos == index)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
127 ++cnt;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
128 else
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
129 exch(pos, pos - cnt);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
130 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
131
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
132 arr = arr[0 .. $ - cnt];
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
133
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
134 return ret;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
135 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
136
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
137 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
138 * Removes the specified element from the array
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
139 * if it could find it and returns it.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
140 * Shifts any subsequent elements to the left.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
141 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
142 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
143 * arr = the array to remove the element from
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
144 * element = the element to be removed
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
145 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
146 * Returns: the element that was removed or $(null null)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
147 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
148 * Throws: AssertException if the length of the array is 0
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
149 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
150 T remove (T) (ref T[] arr, T element)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
151 in
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
152 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
153 assert(arr.length > 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
154 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
155 out (result)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
156 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
157 assert(result is element || result is null);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
158 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
159 body
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
160 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
161 int index = arr.indexOf(element);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
162
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
163 if (index == -1)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
164 return null;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
165
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
166 return arr.remove(index);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
167 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
168
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
169 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
170 * Removes the specified element from the array
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
171 * if it could find it and returns it.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
172 * Shifts any subsequent elements to the left.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
173 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
174 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
175 * arr = the array to remove the element from
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
176 * element = the element to be removed
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
177 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
178 * Returns: the element that was removed or $(null null)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
179 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
180 * Throws: AssertException if the length of the array is 0
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
181 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
182 alias remove removeElement;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
183
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
184 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
185 * Returns the index of the first occurrence of the specified element
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
186 * in the array, or -1 if the array does not contain the element.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
187 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
188 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
189 * arr = the array to get the index of the element from
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
190 * element = the element to find
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
191 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
192 * Returns: the index of the element or -1 if it's not in the array
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
193 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
194 * Throws: AssertException if the length of the array is 0
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
195 * Throws: AssertException if the return value is less than -1 or
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
196 * greater than the length of the array - 1.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
197 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
198 size_t indexOf (T, U = uint) (T[] arr, T element, U start = 0)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
199 in
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
200 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
201 assert(arr.length > 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
202 assert(start >= 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
203 assert(element !is null);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
204 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
205 out (result)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
206 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
207 assert(result >= -1 && result < arr.length);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
208 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
209 body
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
210 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
211 uint index;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
212
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
213 version (Tango)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
214 index = tango.text.Util.locate(arr, element);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
215
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
216 else
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
217 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
218 if (start > arr.length)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
219 start = arr.length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
220
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
221 index = privateIndexOf(arr.ptr, element, arr.length - start) + start;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
222 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
223
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
224 if (index == arr.length)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
225 index = -1;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
226
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
227 return index;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
228
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
229 version (Phobos)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
230 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
231 // tango.text.Util.locate
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
232 uint privateIndexOf (T* str, T match, uint length)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
233 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
234 version (D_InlineAsm_X86)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
235 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
236 static if (T.sizeof == 1)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
237 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
238 asm
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
239 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
240 mov EDI, str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
241 mov ECX, length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
242 movzx EAX, match;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
243 mov ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
244 and ESI, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
245 jz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
246
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
247 cld;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
248 repnz;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
249 scasb;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
250 jnz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
251 sub ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
252 dec ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
253 end:;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
254 mov EAX, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
255 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
256 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
257 else static if (T.sizeof == 2)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
258 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
259 asm
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
260 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
261 mov EDI, str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
262 mov ECX, length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
263 movzx EAX, match;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
264 mov ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
265 and ESI, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
266 jz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
267
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
268 cld;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
269 repnz;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
270 scasw;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
271 jnz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
272 sub ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
273 dec ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
274 end:;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
275 mov EAX, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
276 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
277 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
278
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
279 else static if (T.sizeof == 4)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
280 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
281 asm
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
282 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
283 mov EDI, str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
284 mov ECX, length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
285 mov EAX, match;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
286 mov ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
287 and ESI, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
288 jz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
289
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
290 cld;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
291 repnz;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
292 scasd;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
293 jnz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
294 sub ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
295 dec ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
296 end:;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
297 mov EAX, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
298 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
299 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
300
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
301 else
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
302 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
303 auto len = length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
304
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
305 for (auto p = str - 1; len--; )
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
306 if (*++p is match)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
307 return p - str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
308
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
309 return length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
310 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
311 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
312
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
313 else
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
314 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
315 auto len = length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
316
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
317 for (auto p = str - 1; len--; )
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
318 if (*++p is match)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
319 return p - str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
320
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
321 return length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
322 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
323 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
324 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
325 }