Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions compiler/src/dmd/backend/barray.d
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ struct Barray(T)
return p;
}

/********************************
* Insert element at index i, rippling every element
* after it to the right.
* Params:
* i = indext to insert element at
* e = element to insert
*/
void insert(T e, size_t i)
{
setLength(array.length + 1);
for (size_t j = array.length - 1; i < j; --j)
{
array[j] = array[j - 1];
}
array[i] = e;
}

/**********************
* Move the last element from the array into [i].
* Reduce the array length by one.
Expand Down Expand Up @@ -151,6 +168,51 @@ struct Barray(T)
return false; // t is not in array
}

/******************************
* Reverse contents of array.
*/
void reverse()
{
size_t i = 0;
size_t j = array.length;
if (j)
while (i < --j)
{
auto t = array[i];
array[i] = array[j];
array[j] = t;
++i;
}
}

/******************************
* Returns: true if arrays have equal contents
*/
bool equals(ref const Barray rhs)
{
if (array.length != rhs.length)
return false;
foreach (i, ref t; array)
{
if (t != rhs.array[i])
return false;
}
return true;
}

/***********************
* Move contents of `rhs` to `this`.
*/
void move(ref Barray rhs)
{
dtor();
array = rhs.array;
capacity = rhs.capacity;

rhs.array = null;
rhs.capacity = 0;
}

/******************
* Release all memory used.
*/
Expand Down Expand Up @@ -189,6 +251,50 @@ unittest
assert(a[0] == 50);
a.dtor();
assert(a.length == 0);

a.reverse();
a.push(1);
a.push(2);
a.reverse();
assert(a[0] == 2);
assert(a[1] == 1);
a.push(3);
a.reverse();
assert(a[0] == 3);
assert(a[1] == 1);
assert(a[2] == 2); // 3,1,2

assert(a.equals(a));
a.subtract(3); // 1,2
assert(a.length == 2);
assert(a[0] == 1);
assert(a[1] == 2);

Barray!(int) b;
b.move(a);
assert(b.length == 2);
assert(a.length == 0);
assert(b[1] == 2);
a.move(b);

a[0] = 3;
a[1] = 1;
a.insert(5,0); // 5,3,1
a.insert(4,1); // 5,4,3,1
a.insert(6,4); // 5,4,3,1,6
assert(a.length == 5);
assert(a[0] == 5);
assert(a[1] == 4);
assert(a[2] == 3);
assert(a[3] == 1);
assert(a[4] == 6);

a.remove(3); // 5,4,3,6
assert(a.length == 4);
assert(a[0] == 5);
assert(a[1] == 4);
assert(a[2] == 3);
assert(a[3] == 6);
}

/**************************************
Expand Down
Loading