Skip to content

Commit

Permalink
Merge 88d3208 into 1a754cf
Browse files Browse the repository at this point in the history
  • Loading branch information
deavmi committed May 10, 2024
2 parents 1a754cf + 88d3208 commit d303978
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions source/niknaks/arrays.d
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,73 @@ unittest
assert(numbas == [1, 2]);
}

/**
* Inserts the given value into
* the array at the provided index
*
* Params:
* array = the array to insert
* into
* position = the position to
* insert at
* value = the value to insert
* Returns: the updated array
*/
public T[] insertAt(T)(T[] array, size_t position, T value)
{
if(position > array.length)
{
return array;
}

// Case: Right at end
if(position == array.length)
{
array ~= value;
return array;
}
// Anywhere else
else
{
// Make space for a single new element
array.length++;

// Cha-cha to the right
for(size_t i = array.length-1; i > position; i--)
{
array[i] = array[i-1];
}

// Overwrite
array[position] = value;
return array;
}
}

/**
* Tests inserting into an array
* at the given index
*/
unittest
{
int[] vals = [];
vals = vals.insertAt(0, 1);
assert(vals == [1]);

vals = vals.insertAt(0, 69);
assert(vals == [69, 1]);

vals = vals.insertAt(1, 68);
assert(vals == [69, 68, 1]);

vals = vals.insertAt(3, 420);
assert(vals == [69, 68, 1, 420]);

// Failure to insert (array stays the same)
vals = vals.insertAt(5, 421);
assert(vals == [69, 68, 1, 420]);
}

/**
* Returns a version of
* the input array with
Expand Down

0 comments on commit d303978

Please sign in to comment.