/// <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()); }
public void Start() { GraphicalList graphList = gameObject.AddComponent <GraphicalList>(); List <int> testList = new List <int>() { 5, 4, 3, 2, 1 }; Parser.DynamicList list_test = Parser.VariableUtils.createDynamicList(testList); (list_test.atIndex(new Parser.Integer(0))).setName("int1"); (list_test.atIndex(new Parser.Integer(1))).setName("int2"); (list_test.atIndex(new Parser.Integer(2))).setName("int3"); (list_test.atIndex(new Parser.Integer(3))).setName("int4"); (list_test.atIndex(new Parser.Integer(4))).setName("int5"); Dictionary <string, GameObject> list_dict_test = new Dictionary <string, GameObject>(); graphList.list_dict = list_dict_test; graphList.list = list_test; graphList.drawObject(); // To update the dictionary /* INSERT DEMO * Parser.Integer insert_numb = new Parser.Integer(5); * insert_numb.setName("int6"); * * graphList.insert(new Parser.Integer(2), insert_numb); // tester avec les index qui se trouvent a l'extremite */ /* REMOVE DEMO * graphList.remove(new Parser.Integer(1)); */ /* SWAP DEMO * graphList.swap(new Parser.Integer(1), new Parser.Integer(4)); */ /*float t = Time.time; * * while (t < Time.time - 2) * { * * }*/ //Thread.Sleep(1000); //Debug.Log("Animation finished!"); StartCoroutine(testTimeDelay()); /* BUBBLE SORT DEMO * StartCoroutine(bubbleSort()); */ IEnumerator testTimeDelay() { graphList.swap(new Parser.Integer(1), new Parser.Integer(4)); yield return(new WaitForSeconds(10f)); graphList.remove(new Parser.Integer(1)); } IEnumerator bubbleSort() { for (int j = 0; j <= list_test.length().getValue() - 2; j++) { for (int i = 0; i <= list_test.length().getValue() - 2; i++) { if (list_test.atIndex(new Parser.Integer(i)).getValue() > list_test.atIndex(new Parser.Integer(i + 1)).getValue()) { graphList.swap(new Parser.Integer(i), new Parser.Integer(i + 1)); yield return(new WaitForSeconds(2.5f)); } } } } /*IEnumerator TimeDelayFunction() * { * * for (int j = 0; j <= list_test.length().getValue() - 2; j++) * { * for (int i = 0; i <= list_test.length().getValue() - 2; i++) * { * if (list_test.atIndex(new Parser.Integer(i)).getValue() > list_test.atIndex(new Parser.Integer(i + 1)).getValue()) * { * graphList.swap(new Parser.Integer(i), new Parser.Integer(i + 1)); * yield return new WaitForSeconds(1f); * } * } * } * /* * int smallest; * * for (int i = 0; i < list_test.length().getValue() - 1; i++) * { * smallest = i; * for (int j = i + 1; j < list_test.length().getValue(); j++) * { * if (list_test.atIndex(new Parser.Integer(j)).getValue() < list_test.atIndex(new Parser.Integer(smallest)).getValue()) * { * smallest = j; * } * } * * graphList.swap(new Parser.Integer(smallest), new Parser.Integer(i)); * yield return new WaitForSeconds(1f); * } * * Debug.Log("List is sorted !"); * } */ }
public void insert(Parser.Integer index, Parser.Variable value) { list.insertValue(value, index); float time_animation = 2f; GameObject[] obj_list = new GameObject[list.length().getValue() - index.getValue() - 1]; for (int i = index.getValue() + 1, u = 0; i < list.length().getValue(); i++, u++) { obj_list[u] = list_dict[list.atIndex(new Parser.Integer(i)).getName()]; } StartCoroutine(Animation.insertAnimation(obj_list, value.getName(), new Vector3(1, 0, 0), time_animation, 1, this)); }