annotate dwt/dwthelper/array.d @ 123:63a09873578e

Fixed compile errors
author Jacob Carlborg <doob@me.com>
date Thu, 15 Jan 2009 23:08:54 +0100
parents 2e671fa40eec
children 38807a925e24
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
123
63a09873578e Fixed compile errors
Jacob Carlborg <doob@me.com>
parents: 122
diff changeset
29 * Throws: AssertException if the element is null
60
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
123
63a09873578e Fixed compile errors
Jacob Carlborg <doob@me.com>
parents: 122
diff changeset
51 * Throws: AssertException if the element is null
60
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 */
122
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 99
diff changeset
64 T elementAt (T) (T[] arr, int index)
60
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);
122
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 99
diff changeset
68 assert(index > -1 && index < arr.length);
60
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 /**
123
63a09873578e Fixed compile errors
Jacob Carlborg <doob@me.com>
parents: 122
diff changeset
76 * Gets the element at the specified index
99
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
77 *
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
78 * Params:
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
79 * arr = the array to get the element from
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
80 * index = the index of the element to get
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
81 *
123
63a09873578e Fixed compile errors
Jacob Carlborg <doob@me.com>
parents: 122
diff changeset
82 * Returns: the element at the specified index
99
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
83 */
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
84 alias elementAt get;
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
85
d32621bf0f90 Ported dwt.dnd.DropTarget
Jacob Carlborg <doob@me.com>
parents: 60
diff changeset
86 /**
60
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
87 * 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
88 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
89 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
90 * 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
91 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
92 * 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
93 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
94 size_t size (T) (T[] arr)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
95 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
96 return arr.length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
97 }
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 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
100 * 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
101 * 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
102 * Shifts any subsequent elements to the left.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
103 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
104 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
105 * 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
106 * 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
107 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
108 * 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
109 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
110 * Throws: AssertException if the length of the array is 0
123
63a09873578e Fixed compile errors
Jacob Carlborg <doob@me.com>
parents: 122
diff changeset
111 * Throws: AssertException if the $(D_CODE index) argument is
60
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
112 * 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
113 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
114 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
115 in
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 assert(arr.length > 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
118 assert(index > -1 && index < arr.length);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
119 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
120 body
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 T ret = arr[index];
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 // 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
125 // not inline the reference-based version.
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
126 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
127 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
128 T t = arr[p1];
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
129 arr[p1] = arr[p2];
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
130 arr[p2] = t;
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
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
133 size_t cnt = 0;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
134
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
135 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
136 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
137 if (pos == index)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
138 ++cnt;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
139 else
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
140 exch(pos, pos - cnt);
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
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
143 arr = arr[0 .. $ - cnt];
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
144
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
145 return ret;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
146 }
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 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
149 * Removes the specified element from the array
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
150 * 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
151 * Shifts any subsequent elements to the left.
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 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
154 * 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
155 * element = the element to be removed
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 * 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
158 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
159 * 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
160 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
161 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
162 in
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
163 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
164 assert(arr.length > 0);
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 out (result)
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 assert(result is element || result is null);
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 body
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
171 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
172 int index = arr.indexOf(element);
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 if (index == -1)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
175 return null;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
176
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
177 return arr.remove(index);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
178 }
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 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
181 * Removes the specified element from the array
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
182 * 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
183 * Shifts any subsequent elements to the left.
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 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
186 * 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
187 * element = the element to be removed
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
188 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
189 * 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
190 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
191 * 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
192 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
193 alias remove removeElement;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
194
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
195 /**
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
196 * 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
197 * 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
198 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
199 * Params:
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
200 * 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
201 * element = the element to find
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
202 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
203 * 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
204 *
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
205 * 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
206 * 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
207 * 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
208 */
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
209 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
210 in
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
211 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
212 assert(arr.length > 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
213 assert(start >= 0);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
214 assert(element !is null);
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 out (result)
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 assert(result >= -1 && result < arr.length);
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
219 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
220 body
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
221 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
222 uint index;
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 version (Tango)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
225 index = tango.text.Util.locate(arr, element);
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 else
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 if (start > arr.length)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
230 start = arr.length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
231
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
232 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
233 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
234
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
235 if (index == arr.length)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
236 index = -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 return index;
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 version (Phobos)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
241 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
242 // tango.text.Util.locate
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
243 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
244 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
245 version (D_InlineAsm_X86)
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 static if (T.sizeof == 1)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
248 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
249 asm
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
250 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
251 mov EDI, str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
252 mov ECX, length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
253 movzx EAX, match;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
254 mov ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
255 and ESI, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
256 jz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
257
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
258 cld;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
259 repnz;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
260 scasb;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
261 jnz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
262 sub ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
263 dec ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
264 end:;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
265 mov EAX, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
266 }
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 else static if (T.sizeof == 2)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
269 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
270 asm
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
271 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
272 mov EDI, str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
273 mov ECX, length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
274 movzx EAX, match;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
275 mov ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
276 and ESI, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
277 jz end;
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 cld;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
280 repnz;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
281 scasw;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
282 jnz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
283 sub ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
284 dec ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
285 end:;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
286 mov EAX, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
287 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
288 }
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 else static if (T.sizeof == 4)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
291 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
292 asm
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
293 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
294 mov EDI, str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
295 mov ECX, length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
296 mov EAX, match;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
297 mov ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
298 and ESI, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
299 jz end;
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 cld;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
302 repnz;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
303 scasd;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
304 jnz end;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
305 sub ESI, ECX;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
306 dec ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
307 end:;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
308 mov EAX, ESI;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
309 }
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 else
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
313 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
314 auto len = length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
315
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
316 for (auto p = str - 1; len--; )
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
317 if (*++p is match)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
318 return p - str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
319
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
320 return length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
321 }
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 else
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
325 {
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
326 auto len = length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
327
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
328 for (auto p = str - 1; len--; )
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
329 if (*++p is match)
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
330 return p - str;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
331
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
332 return length;
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
333 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
334 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
335 }
62202ce0039f Updated and fixed many modules to 3.514
Jacob Carlborg <doob@me.com>
parents:
diff changeset
336 }