public void removeValue(Integer index) { assertAssignment(); int i = index.getValue(); if (i < 0) { i += _list.Count; // if "-1" -> last element of the list } if (i < _list.Count) { _list.RemoveAt(i); } else { throw new AquilaExceptions.InvalidIndexException($"Index {index} out of bounds"); } trace("removeValue", new dynamic[] { index.getRawValue() }); }
public void insertValue(Variable x, Integer index) { assertAssignment(); _list.Insert(index.getValue(), x); trace("insertValue", new dynamic[] { index.getRawValue(), x.getRawValue() }); }
/// <summary> /// Default function /// </summary> /// <para> /// Swaps the elements at index a and b in a list /// </para> /// <param name="list_expr"> the target <see cref="DynamicList"/> (as an <see cref="Expression"/>)</param> /// <param name="a_expr"> index of the first element</param> /// <param name="b_expr"> index of the second element</param> /// <returns> <see cref="NullVar"/> (equivalent of null/void)</returns> private static NullVar swapFunction(Expression list_expr, Expression a_expr, Expression b_expr) { // evaluate every expression DynamicList list = list_expr.evaluate() as DynamicList; Integer a = a_expr.evaluate() as Integer; Integer b = b_expr.evaluate() as Integer; // check indexes list.validateIndex(a); list.validateIndex(b); // extract both values Variable var_a = list.atIndex(a); Variable var_b = list.atIndex(b); // freeze the context Context.freeze(); // change a list.removeValue(a); list.insertValue(var_b, a); // change b list.removeValue(b); list.insertValue(var_a, b); // unfreeze the context Context.unfreeze(); // update manually (void) if (list.isTraced()) { list.tracer.update(new Event( new Alteration("swap", list, list.getRawValue(), new dynamic[] { a.getRawValue(), b.getRawValue() }))); } return(new NullVar()); }