added system.del; delete; insert

This commit is contained in:
Andreas Rumpf 2010-02-02 19:20:16 +01:00
commit 6ecbed7284
5 changed files with 93 additions and 34 deletions

View file

@ -698,6 +698,31 @@ proc add *[T](x: var seq[T], y: openArray[T]) {.noSideEffect.} =
setLen(x, xl + y.len)
for i in 0..high(y): x[xl+i] = y[i]
proc del* [T](x: var seq[T], i: int) {.noSideEffect.} =
## deletes the item at index `i` by putting ``x[high(x)]`` into position `i`.
## This is an O(1) operation.
var xl = x.len
x[i] = x[xl-1]
setLen(x, xl-1)
proc delete*[T](x: var seq[T], i: int) {.noSideEffect.} =
## deletes the item at index `i` by moving ``x[i+1..]`` by one position.
## This is an O(n) operation.
var xl = x.len
for j in i..xl-2: x[j] = x[j+1]
setLen(x, xl-1)
proc insert*[T](x: var seq[T], item: T, i = 0) {.noSideEffect.} =
## inserts `item` into `x` at position `i`.
var xl = x.len
setLen(x, xl+1)
var j = xl-1
while j >= i:
x[j+1] = x[j]
inc(j)
x[i] = item
proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}
## takes any Nimrod variable and returns its string representation. It
## works even for complex data graphs with cycles. This is a great