Permalink
Browse files

[dotnet] implement push, pop, unshift, shift in NQPArray

add some tests in 54-array.t
  • Loading branch information...
1 parent ee58ecd commit e7e4edb43213931c55ff48952554ebe344fd71cf @diakopter diakopter committed Nov 1, 2010
Showing with 127 additions and 1 deletion.
  1. +12 −0 common/NQP/NQPSetting.pm
  2. +96 −1 dotnet/runtime/Runtime/Ops.cs
  3. +19 −0 t/nqp/54-array.t
View
@@ -113,6 +113,18 @@ knowhow NQPArray is repr('P6list') {
method elems() {
nqp::lllist_elems(self)
}
+ method pop() {
+ nqp::lllist_pop(self)
+ }
+ method push($item) {
+ nqp::lllist_push(self, $item)
+ }
+ method shift() {
+ nqp::lllist_shift(self)
+ }
+ method unshift($item) {
+ nqp::lllist_unshift(self, $item)
+ }
method at_pos($idx) {
nqp::vivify(nqp::lllist_get_at_pos(self, $idx.Int), Any)
}
@@ -850,7 +850,102 @@ public static RakudoObject lllist_elems(ThreadContext TC, RakudoObject LLList)
{
throw new Exception("Cannot use lllist_elems if representation is not P6list");
}
- }
+ }
+
+ /// <summary>
+ /// Pushes a value to a low level list (something that
+ /// uses the P6list representation).
+ /// </summary>
+ /// <param name="TC"></param>
+ /// <param name="LLList"></param>
+ /// <returns></returns>
+ public static RakudoObject lllist_push(ThreadContext TC, RakudoObject LLList, RakudoObject item)
+ {
+ if (LLList is P6list.Instance)
+ {
+ ((P6list.Instance)LLList).Storage.Add(item);
+ return item;
+ }
+ else
+ {
+ throw new Exception("Cannot use lllist_push if representation is not P6list");
+ }
+ }
+
+ /// <summary>
+ /// Pops a value from a low level list (something that
+ /// uses the P6list representation).
+ /// </summary>
+ /// <param name="TC"></param>
+ /// <param name="LLList"></param>
+ /// <returns></returns>
+ public static RakudoObject lllist_pop(ThreadContext TC, RakudoObject LLList)
+ {
+ if (LLList is P6list.Instance)
+ {
+ List<RakudoObject> store = ((P6list.Instance)LLList).Storage;
+ int idx = store.Count - 1;
+ if (idx < 0)
+ {
+ throw new ArgumentOutOfRangeException("Cannot pop from an empty list");
+ }
+ RakudoObject item = store[idx];
+ store.RemoveAt(idx);
+ return item;
+ }
+ else
+ {
+ throw new Exception("Cannot use lllist_pop if representation is not P6list");
+ }
+ }
+
+ /// <summary>
+ /// Shifts a value from a low level list (something that
+ /// uses the P6list representation).
+ /// </summary>
+ /// <param name="TC"></param>
+ /// <param name="LLList"></param>
+ /// <returns></returns>
+ public static RakudoObject lllist_shift(ThreadContext TC, RakudoObject LLList)
+ {
+ if (LLList is P6list.Instance)
+ {
+ List<RakudoObject> store = ((P6list.Instance)LLList).Storage;
+ int idx = store.Count - 1;
+ if (idx < 0)
+ {
+ throw new ArgumentOutOfRangeException("Cannot shift from an empty list");
+ }
+ RakudoObject item = store[0];
+ store.RemoveAt(0);
+ return item;
+ }
+ else
+ {
+ throw new Exception("Cannot use lllist_shift if representation is not P6list");
+ }
+ }
+
+ /// <summary>
+ /// Unshifts a value to a low level list (something that
+ /// uses the P6list representation).
+ /// </summary>
+ /// <param name="TC"></param>
+ /// <param name="LLList"></param>
+ /// <returns></returns>
+ public static RakudoObject lllist_unshift(ThreadContext TC, RakudoObject LLList, RakudoObject item)
+ {
+ if (LLList is P6list.Instance)
+ {
+ List<RakudoObject> store = ((P6list.Instance)LLList).Storage;
+ store.Insert(0, item);
+ return item;
+ }
+ else
+ {
+ throw new Exception("Cannot use lllist_unshift if representation is not P6list");
+ }
+ }
/// <summary>
/// Gets a value at a given key from a low level mapping (something that
View
@@ -0,0 +1,19 @@
+#! nqp
+
+plan(6);
+
+# test push, pop, unshift, shift
+
+my $arr := [];
+$arr.unshift(6);
+$arr.push(5);
+$arr.unshift(4);
+$arr.push(3);
+$arr.unshift(2);
+$arr.push(1);
+print("ok "); say($arr.pop);
+print("ok "); say($arr.shift);
+print("ok "); say($arr.pop);
+print("ok "); say($arr.shift);
+print("ok "); say($arr.pop);
+print("ok "); say($arr.shift);

0 comments on commit e7e4edb

Please sign in to comment.