Skip to content

Commit 1bfff5e

Browse files
authored
convert Bpred from linked list to Barray (#23180)
1 parent f15039b commit 1bfff5e

12 files changed

Lines changed: 203 additions & 131 deletions

File tree

compiler/src/dmd/backend/barray.d

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,45 @@ struct Barray(T)
112112
setLength(len);
113113
}
114114

115+
/**********************
116+
* Subtract element at index i from the array.
117+
* Ripple the elements after i to the left 1 place.
118+
* Reduce the array length by one.
119+
* Params:
120+
* i = index of element to subtract
121+
*/
122+
void subtracti(size_t i)
123+
{
124+
while (i + 1 < array.length)
125+
{
126+
array[i] = array[i + 1];
127+
++i;
128+
}
129+
setLength(array.length - 1);
130+
}
131+
132+
/**********************
133+
* Subtract element from the array.
134+
* Ripple the elements after it to the left 1 place.
135+
* Reduce the array length by one.
136+
* Params:
137+
* t = element to subtract
138+
* Returns:
139+
* true if element was found and subtracted
140+
*/
141+
bool subtract(T t)
142+
{
143+
foreach (i, ref e; array)
144+
{
145+
if (t == e)
146+
{
147+
subtracti(i);
148+
return true; // t was found at index i
149+
}
150+
}
151+
return false; // t is not in array
152+
}
153+
115154
/******************
116155
* Release all memory used.
117156
*/
@@ -146,6 +185,8 @@ unittest
146185
a.push(50);
147186
a.remove(1);
148187
assert(a[1] == 50);
188+
a.subtracti(0);
189+
assert(a[0] == 50);
149190
a.dtor();
150191
assert(a.length == 0);
151192
}

0 commit comments

Comments
 (0)