static bool TestCase_Copy() //Fahrngruber Samuel { int capacity = 100; int numberOfElements = 50; bool result = false; SimpleStack s1 = new SimpleStack(capacity); SimpleStack s2; int idx; for (idx = 0; idx < numberOfElements; idx++) { s1.Push(idx); } s2 = s1.Copy(); if (s1 != s2) { result = true; } while (result == true && idx > 0) { try { if (s1.Pop() != s2.Pop()) { result = false; } } catch { result = false; } idx--; } return(result); }
public void TC_SimpleStack() { var stack = new SimpleStack <double>(); var input = GetDoubleTestList(); foreach (var item in input) { stack.Add(item, item); } Assert.AreEqual(input.Count, stack.GetSize()); List <double> poppedItems = new List <double>(); while (stack.GetSize() > 0) { poppedItems.Add(stack.RemoveMin()); } input.Reverse(); Assert.IsTrue(CollectionsEquality.Equals(input, poppedItems)); stack.Clear(); Assert.AreEqual(0, stack.GetSize()); Assert.AreEqual("Simple Stack", stack.GetName()); }
public static void Main() { var source = new SimpleStack <int>(); source.Push(31); source.Push(52); source.Push(63); source.Push(74); source.Push(45); source.Push(26); source.Push(17); /* * //imperatively selection squares of odd values in source * foreach(int n in source) * { * if((n % 2) == 1) * Console.WriteLine(n * n); * } */ //declaratively selection squares of odd values in source //var selection = source.Where(n => (n % 2) == 1).Select(n => n * n); var selection = from n in source where (n % 2) == 1 select n * n; foreach (int n in selection) { Console.WriteLine(n); } }
/// <summary> /// Computes (number of nonterminals, number of terminals) in the underlying directed acyclic graph. /// </summary> public Tuple <int, int> GetSize() { int nrNodes = 0; int nrLeaves = 0; HashSet <BDG <T, S> > done = new HashSet <BDG <T, S> >(); SimpleStack <BDG <T, S> > stack = new SimpleStack <BDG <T, S> >(); stack.Push(this); done.Add(this); while (stack.IsNonempty) { var bdg = stack.Pop(); if (bdg.IsLeaf) { nrLeaves += 1; } else { if (done.Add(bdg.TrueCase)) { stack.Push(bdg.TrueCase); } if (done.Add(bdg.FalseCase)) { stack.Push(bdg.FalseCase); } nrNodes += 1; } } return(new Tuple <int, int>(nrNodes, nrLeaves)); }
// Driver code public static void Main() { // Calling Arraylist ArraylistSample arr = new ArraylistSample(); arr.arrayListTest(); // Calling Queue SimpleQueue nq = new SimpleQueue(); nq.QueueD(); // Calling Stack SimpleStack sta = new SimpleStack(); sta.StackD(); SimpleHashTable HT = new SimpleHashTable(); HT.HashT(); Array coll = new Array(); coll.CollectionsS(); }
static void Main(string[] args) { Pryamougolnik pryam = new Pryamougolnik(5, 6); Kvadrat kvad = new Kvadrat(4); krug kru = new krug(7); ArrayList al = new ArrayList(); al.Add(kru); al.Add(pryam); al.Add(kvad); Console.WriteLine("Коллекция:"); foreach (var x in al) { Console.WriteLine(x); } Console.WriteLine(); Console.WriteLine("Сортировка:"); al.Sort(); foreach (var x in al) { Console.WriteLine(x); } Console.WriteLine(); Console.WriteLine("Коллекция класса List<Figure>:"); List <GeoFig> fl = new List <GeoFig>(); fl.Add(kru); fl.Add(pryam); fl.Add(kvad); foreach (var x in fl) { Console.WriteLine(x); } Console.WriteLine(); Console.WriteLine("Сортировка:"); fl.Sort(); foreach (var x in fl) { Console.WriteLine(x); } Console.WriteLine(); Console.WriteLine("Матрица:"); Matrix <GeoFig> cube = new Matrix <GeoFig>(3, 3, 3, null); cube[0, 0, 0] = pryam; cube[1, 1, 1] = kvad; cube[2, 2, 2] = kru; Console.WriteLine(cube.ToString()); Console.WriteLine("Стек:"); SimpleStack <GeoFig> Stack1 = new SimpleStack <GeoFig>(); Stack1.Push(pryam); Stack1.Push(kru); Stack1.Push(kvad); while (Stack1.Count > 0) { GeoFig fig = Stack1.Pop(); Console.WriteLine(fig); } Console.ReadKey(); }
public void first_test() { var stack = new SimpleStack <Item>(); stack.Count.Should().Be(0); Enumerable .Range(0, 10) .Select(x => new Item { Index = x }) .ToList() .ForEach(item => stack.Push(item)); stack.Count.Should().Be(10); for (var i = 9; i > -1; i--) { var current = stack.Pop(); current.Index.Should().Be(i); } Action fail = () => stack.Pop(); fail.Should().Throw <Exception>().Where(_ => _.Message.Contains("empty")); }
public int[] FindOrder(int numCourses, int[,] prerequisites) { List<int>[] adj = new List<int>[numCourses]; for (int r = 0; r < prerequisites.GetLength (0); r++) { if (adj [prerequisites [r, 1]] == null) { adj [prerequisites [r, 1]] = new List<int> (); } adj [prerequisites [r, 1]].Add(prerequisites [r, 0]); } var stack = new SimpleStack<int> (); bool[] visited = new bool[numCourses]; int i = 0; for (; i < numCourses; i++) { if (!visited [i]) { DFS (i, adj, stack, visited); } } var result = new int[numCourses]; i = 0; while (!stack.Empty ()) { result [i++] = stack.Pop (); } return result; }
private static bool TestCase_Resize() //Melanie Bugelnig { int newCapacity = 5; SimpleStack s1 = new SimpleStack(newCapacity); bool result = true; int counter; for (counter = 0; counter < newCapacity; counter++) { s1.Push(counter); } newCapacity = 10; s1.Resize(newCapacity); for (; counter < newCapacity; counter++) { s1.Push(counter); } counter--; if (s1.Pop() != counter) { result = false; } counter--; return(result); }
static bool TestCase_Find() //Breschan Moritz { //rgw gibt an wie viele Elemente gefunden wurde bool rgw = true; SimpleStack s1 = new SimpleStack(20); int counter = 0; for (counter = 0; counter < 10; counter++) { s1.Push(counter); } for (counter = 9; counter >= 0; counter--) { s1.Push(counter); } counter = 0; while (counter < 10 && rgw) { if (s1.Find(counter) != 2) { rgw = false; } counter++; } return(rgw); }
static bool TestCase_Pop() //Hebein Fabian { SimpleStack Stack1 = new SimpleStack(10); bool status = true; int zahl = 5; Stack1.Push(zahl); if (Stack1.Pop() != zahl) { status = false; } else { if (Stack1.IsEmpty()) { status = false; try { Stack1.Pop(); } catch { status = true; } } } return(status); }
private void PushToStack(TreeNode root, SimpleStack<TreeNode> stack) { while (root != null) { stack.Push (root); root = root.left; } }
internal static int Main(string[] args) { Rectangle rect = new Rectangle(10, 20); Circle circle = new Circle(10); Square square = new Square(10); ArrayList array_shapes = new ArrayList() { rect, circle, square }; List <Shape> shapes = new List <Shape>() { rect, circle, square }; Console.WriteLine("Initial array:"); foreach (var shape in array_shapes) { Console.WriteLine(shape); } Console.WriteLine(); array_shapes.Sort(); shapes.Sort(); Console.WriteLine("Sorted ArrayList:"); foreach (var shape in array_shapes) { Console.WriteLine(shape); } Console.WriteLine(); Console.WriteLine("Sorted List<Shapes>:"); foreach (var shape in shapes) { Console.WriteLine(shape); } Console.WriteLine(); Console.WriteLine("Matrix:"); Matrix3D <Shape> matrix = new Matrix3D <Shape>(3, 3, 3, null); matrix[0, 0, 0] = circle; matrix[1, 1, 1] = square; matrix[2, 2, 2] = rect; Console.WriteLine(matrix); Console.WriteLine("Stack:"); SimpleStack <Shape> stack = new SimpleStack <Shape>(); stack.Push(circle); stack.Push(square); stack.Push(rect); while (stack.Count > 0) { Console.WriteLine(stack.Pop()); } return(0); }
public void Clear() { forward = back = null; Count = 0; sectorStack = new SimpleStack <T> { forward = null }; }
public unsafe TinyJsonStreamingWriter(Allocator allocator, int expectedSize = 50, int expectedDepth = 5) { m_WroteToString = false; m_Stack = new SimpleStack(expectedDepth, allocator); m_Buffer = new HeapString(expectedSize, allocator); m_Buffer.Append(((FixedString128)"{ ").GetUnsafePtr(), ((FixedString128)"{ ").Length); m_Stack.Push(k_EmptyObject); }
private static bool TestCase_Push() //Valon Berisa { int pushElement = 7; SimpleStack s1 = new SimpleStack(5); s1.Push(pushElement); return(s1.GetElement() == pushElement); }
private void iniStackPoints(string file) { SimpleStack simple = JsonConvert.DeserializeObject <SimpleStack>(file); SimpleStackWork simpleWork = new SimpleStackWork(simple); stackPoints = simpleWork.getStackPoints(); stackPointsCount = stackPoints.Count; stackEntPoint = simple.enterPoint; }
static void Main(string[] args) { //Array Console.WriteLine("Выполнил: Грызин А.Н. РТ5-31"); Console.WriteLine("------------------------------------------------------------------\n"); Rectangle rctngl = new Rectangle(3.5, 2); Square sqr = new Square(7); Circle crcl = new Circle(5); ArrayList array_l = new ArrayList(); array_l.Add(rctngl); array_l.Add(sqr); array_l.Add(crcl); array_l.Sort(); foreach (Figure element in array_l) { Console.WriteLine(element); } //List Console.WriteLine("------------------------------------------------------------------"); Rectangle rctngl_1 = new Rectangle(323, 123); Square sqr_1 = new Square(41); Circle crcl_1 = new Circle(47); List <Figure> list_f = new List <Figure>(); list_f.Add(rctngl_1); list_f.Add(sqr_1); list_f.Add(crcl_1); list_f.Sort(); foreach (Figure element in list_f) { Console.WriteLine(element); } //Matrix Console.WriteLine("------------------------------------------------------------------"); Console.WriteLine("\nМатрица"); Matrix <Figure> matrix = new Matrix <Figure>(3, 3, 3, new FigureMatrixCheckEmpty()); matrix[0, 0, 0] = rctngl_1; matrix[1, 1, 1] = sqr_1; matrix[2, 2, 2] = crcl_1; Console.WriteLine(matrix.ToString()); //Stack Console.WriteLine("------------------------------------------------------------------\nСтек\n"); SimpleStack <Figure> stack = new SimpleStack <Figure>(); stack.Push(rctngl); stack.Push(rctngl_1); Console.WriteLine(stack.Pop().ToString()); stack.Push(sqr_1); stack.Push(crcl); Console.WriteLine(stack.Pop().ToString()); Console.WriteLine(stack.Pop().ToString()); Console.WriteLine(stack.Pop().ToString()); Console.WriteLine("Элементов в стеке: {0}", stack.Count); }
public void TestCheckTopItem() { List <char> chars = new List <char> { 'a', 'b', 'c' }; SimpleStack <char> charStack = new SimpleStack <char>(chars); Assert.AreEqual(chars[chars.Count - 1], charStack.Top()); }
public static void Main() { SimpleStack <string> a = new SimpleStack <string>(); a.Push("Monday"); a.Push("Tuesday"); a.Push("Wednesday"); a.Push("Thursday"); a.Push("Friday"); //a.Push(6.78); var b = new SimpleStack <string>(); b.Push("June"); b.Push("May"); b.Push("April"); b.Push("March"); SimpleStack <Interval> c = new SimpleStack <Interval>(); c.Push(new Interval(7, 31)); c.Push(new Interval(4, 52)); c.Push(new Interval(5, 23)); c.Push(new Interval(6, 14)); c.Push(new Interval(3, 45)); SimpleStack <object> d = new SimpleStack <object>(); d.Push("Saturday"); d.Push(6.78); d.Push(new Interval(2, 40)); while (!a.Empty()) { Console.WriteLine(a.Pop()); } Console.WriteLine(); while (!b.Empty()) { Console.WriteLine(b.Pop()); } Console.WriteLine(); while (!c.Empty()) { Console.WriteLine(c.Pop()); } Console.WriteLine(); while (!d.Empty()) { Console.WriteLine(d.Pop()); } }
static bool TestCase_IsEmpty() //Drabosenig Andreas { bool result = false; SimpleStack s1 = new SimpleStack(10); if (s1.IsEmpty() == true) { result = true; } return(result); }
public void ConstructorWithItems() { IEnumerable <string> strings = new List <string> { "Hello", "World", "With", "Data", "Structures" }; SimpleStack <string> stringStack = new SimpleStack <string>(strings); Assert.IsInstanceOfType(stringStack, typeof(SimpleStack <string>)); Assert.AreEqual(strings.Count(), stringStack.Count); }
public static void Main() { SimpleStack <string> a = new SimpleStack <string>(); a.Push("Monday"); a.Push("Tuesday"); a.Push("Wednesday"); a.Push("Thursday"); a.Push("Friday"); SimpleStack <string> b = new SimpleStack <string>(); b.Push("June"); b.Push("May"); b.Push("April"); b.Push("March"); SimpleStack <Interval> c = new SimpleStack <Interval>(); c.Push(new Interval(5, 31)); c.Push(new Interval(6, 52)); c.Push(new Interval(4, 13)); c.Push(new Interval(7, 24)); c.Push(new Interval(3, 45)); SimpleStack <object> d = new SimpleStack <object>(); d.Push("Sunday"); d.Push(12.3); d.Push(new Interval(2, 30)); while (!a.Empty()) { Console.WriteLine(a.Pop()); } Console.WriteLine("----------------------"); while (!b.Empty()) { Console.WriteLine(b.Pop()); } Console.WriteLine("----------------------"); while (!c.Empty()) { Console.WriteLine(c.Pop()); } Console.WriteLine("----------------------"); while (!d.Empty()) { Console.WriteLine(d.Pop()); } }
public void TestClearBehavior() { List <char> chars = new List <char> { 'a', 'b', 'c' }; SimpleStack <char> charStack = new SimpleStack <char>(chars); charStack.Clear(); Assert.AreEqual(0, charStack.Count); Assert.AreEqual(true, charStack.IsEmpty()); }
public void DFS(int start, List<int>[] adj, SimpleStack<int> stack, bool[] visited) { visited [start] = true; if (adj[start] != null) { foreach (var r in adj[start]) { if (!visited [r]) { DFS (r, adj, stack, visited); } } } stack.Push (start); }
/// <summary> /// Pushes value on the stack. /// Stack change is only valid for current variable lifetime since everything is allocated on stack. /// </summary> /// <param name="value">Value to be pushed on stack.</param> public void Push(IArgumentScope value) { SimpleStack newStack = new SimpleStack { Previous = null, Value = value, }; Stack = new MyNullable <SimpleStack>() { Value = newStack, }; }
static void Main(string[] args) { SimpleStack<int> stack = new SimpleStack<int>(); stack.Push(1); stack.Push(2); stack.Push(3); Console.WriteLine(stack.ToString()); int poped = stack.Pop(); Console.WriteLine("Poped: {0}", poped); Console.WriteLine(stack.ToString()); }
static bool TestCase_GetElement() // Julian Blaschke { SimpleStack stack1 = new SimpleStack(); stack1.Push(13); if (!(stack1.GetElement() == 13)) { return(false); } return(true); }
static bool TestCase_Merge() //Kandut Nico { //der 2. Stack wird auf den 1. gesetzt bool result = true; int testwert; SimpleStack SimpleStack01; SimpleStack SimpleStack02; SimpleStack ResultingStack; int idxCounter = 0; testwert = 3; SimpleStack01 = new SimpleStack(testwert + 4); SimpleStack02 = new SimpleStack(testwert); for (idxCounter = 0; idxCounter < testwert - 1; idxCounter++) { SimpleStack01.Push(idxCounter); SimpleStack02.Push(idxCounter + 10); } ResultingStack = SimpleStack01.Merge(SimpleStack02); idxCounter = 0; try { while (result && idxCounter > testwert) { if (ResultingStack.Pop() != SimpleStack02.Pop()) { result = false; } idxCounter--; } while (result && idxCounter > 0) { if (ResultingStack.Pop() != SimpleStack01.Pop()) { result = false; } idxCounter--; } } catch { result = false; } return(result); }
static void Main(string[] args) { SimpleStack <int> stack = new SimpleStack <int>(); stack.Push(1); stack.Push(2); stack.Push(3); Console.WriteLine(stack.ToString()); int poped = stack.Pop(); Console.WriteLine("Poped: {0}", poped); Console.WriteLine(stack.ToString()); }
public static void Main() { int[] numbers = { 1, 2, 3, 4, 5, 6 }; numbers.Choose(n => (n % 2) == 1).Consume(Console.WriteLine); Console.WriteLine(); SimpleStack <Interval> store = new SimpleStack <Interval>(); store.Push(new Interval(7, 31)); store.Push(new Interval(4, 52)); store.Push(new Interval(5, 23)); store.Push(new Interval(6, 14)); store.Push(new Interval(3, 45)); store.Choose(i => i.Time > 300).Consume(Console.WriteLine); }
public static void Main() { SimpleStack <Interval> c = new SimpleStack <Interval>(); c.Push(new Interval(7, 31)); c.Push(new Interval(4, 52)); c.Push(new Interval(5, 23)); c.Push(new Interval(6, 14)); c.Push(new Interval(3, 45)); c.Consume(Console.WriteLine); Console.WriteLine(); c.Consume(i => Console.WriteLine(i.Time)); }
public static void Main() { int[] array = { 1, 4, 9, 16, 25, 36 }; array.Print("All integers in array", n => true); array.Print("Odd integers in array", n => (n % 2) == 1); SimpleStack <Interval> stack = new SimpleStack <Interval>(); stack.Push(new Interval(6, 51)); stack.Push(new Interval(7, 12)); stack.Push(new Interval(4, 23)); stack.Push(new Interval(5, 34)); stack.Push(new Interval(3, 45)); stack.Print("All Intervals on stack", i => true); stack.Print("Big Intervals on stack", i => i.Time > 300); }
static bool TestCase_IsFull() //Gragger Nicolas { SimpleStack stackIsFull = new SimpleStack(1); bool result = true; if (stackIsFull.IsFull()) { result = false; } stackIsFull.Push(5); if (!stackIsFull.IsFull()) { result = false; } return(result); }
public int MaxSub(int[] nums) { SimpleStack<int> stack = new SimpleStack<int> (); var lastMax = int.MinValue; for (int i = 0; i < nums.Length; i++) { if (nums [i] < 0) { if (lastMax <= nums [i]) { lastMax = nums [i]; } if (!stack.Empty ()) { var last = stack.Pop (); lastMax = Math.Max (lastMax, last); last += nums [i]; if (last > 0) { stack.Push (last); } } } else if (nums [i] > 0) { if (stack.Empty ()) { stack.Push (nums [i]); } else { var last = stack.Pop (); last += nums [i]; stack.Push (last); } } else { lastMax = Math.Max (nums [i], lastMax); } } if (!stack.Empty ()) { var f = stack.Pop (); lastMax = Math.Max (lastMax, f); } return lastMax; }
public void NextPermutation(int[] nums) { if (nums != null && nums.Length > 1) { var stack = new SimpleStack<Tuple<int, int>>(); stack.Push(new Tuple<int, int>(nums[nums.Length - 1], nums.Length - 1)); int start = 0; for (int i = nums.Length - 2; i >= 0; i--) { Tuple<int, int> replace = null; while(!stack.Empty() && stack.Top().Item1 > nums[i]) { replace = stack.Pop(); } if (replace != null) { Swap(nums, i, replace.Item2); start = i + 1; break; } else { if (nums[i] > stack.Top().Item1) { stack.Push(new Tuple<int,int>(nums[i], i)); } } } for (int i = 0; i <= (nums.Length - 1 - start) / 2; i++) { Swap(nums, start + i, nums.Length - 1 - i); } } }
public void createBranch(List<IStack> stacks, int offset, IFormConfiguration formConfig, ColourConfiguration colourConfig, Vector3 branchStartPosition, Vector3 branchStartTwist, Vector3 origin, FormBounds formBounds) { int iterations = formConfig.getStackIterations(); float scale = formConfig.getStartScale(); for (int o = 0; o < offset; o++) { scale *= formConfig.getScaleDelta(); } //centre of ring Vector3 position = branchStartPosition; //add radius of ring float ringRadius = formConfig.getBranchPositionDelta().x * 10 * scale; //angle to calculate ring float zAngle = branchStartTwist.z * Mathf.Deg2Rad; float yAngle = branchStartTwist.y * Mathf.Deg2Rad; float ringSegmentAngle = 360 / iterations * Mathf.Deg2Rad; Vector3 stackTwist = formConfig.getStackStartTwist(); //colour stuff Color modelColour = new Color (colourConfig.getBaseRed(), colourConfig.getBaseGreen(), colourConfig.getBaseBlue(), 1); //if cycling override base colour if (colourConfig.getCycle ()) { modelColour = colourConfig.getCycleColour(); } for (int i = offset; i < offset + iterations; i++) { position.x = (ringRadius * Mathf.Cos(zAngle) * Mathf.Sin(yAngle)) + branchStartPosition.x; position.y = (ringRadius * Mathf.Sin(zAngle) * Mathf.Sin(yAngle)) + branchStartPosition.y; position.z = (ringRadius * Mathf.Cos(yAngle)) + branchStartPosition.z; formBounds.calculateNewBounds(position); //Stack Shape IStack stack = new SimpleStack(); //default if (formConfig.getStackShape() == IFormConfiguration.StackShape.Sphere) { //spehere stack = new SimpleStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Box) { //box stack = new SimpleBoxStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Complex1) { //complex 1 stack = new ComplexStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Complex2) { //complex 2 stack = new ComplexStack2(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.SimpleTorus) { //simple Torus stack = new SimpleTorusStack(); } //colour stuff order is important Color stackColour = modelColour; if (colourConfig.getFadeColour()) { //fade colour in stackColour = ColourUtility.fadeModelColour(modelColour, iterations, i); } if (colourConfig.getPulse()) { //do pulse stackColour = colourConfig.getPulseColourForStack(stackColour, i); } stack.initialise(position, stackTwist, 0.5f * scale, stackColour); stacks.Add(stack); yAngle += ringSegmentAngle; stackTwist = GeometryUtility.addDeltaToRotation(stackTwist, formConfig.getStackTwistDelta()); } }
public void createBranch(List<IStack> stacks, int offset, IFormConfiguration formConfig, ColourConfiguration colourConfig, Vector3 branchStartPosition, Vector3 branchStartTwist, Vector3 origin, FormBounds formBounds) { float scale = formConfig.getStartScale(); Vector3 stackTwist = formConfig.getStackStartTwist(); Vector3 position = branchStartPosition; Vector3 twist = branchStartTwist; //rotate position araound previous branch start position position = GeometryUtility.rotateCoordinateAboutPoint(origin, position, branchStartTwist); //colour stuff int iterations = formConfig.getStackIterations(); Color modelColour = new Color (colourConfig.getBaseRed(), colourConfig.getBaseGreen(), colourConfig.getBaseBlue(), 1); //if cycling override base colour if (colourConfig.getCycle ()) { modelColour = colourConfig.getCycleColour(); } for (int i = 0; i < iterations; i++) { formBounds.calculateNewBounds(position); //Stack Shape IStack stack = new SimpleStack(); //default if (formConfig.getStackShape() == IFormConfiguration.StackShape.Sphere) { //spehere stack = new SimpleStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Box) { //box stack = new SimpleBoxStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Complex1) { //complex 1 stack = new ComplexStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Complex2) { //complex 2 stack = new ComplexStack2(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.SimpleTorus) { //simple Torus stack = new SimpleTorusStack(); } //colour stuff order is important Color stackColour = modelColour; if (colourConfig.getFadeColour()) { //fade colour in stackColour = ColourUtility.fadeModelColour(modelColour, iterations, i); } if (colourConfig.getPulse()) { //do pulse stackColour = colourConfig.getPulseColourForStack(stackColour, i); } stack.initialise(position, stackTwist, scale, stackColour); stacks.Add(stack); scale = scale * formConfig.getScaleDelta(); Vector3 newPosition = GeometryUtility.addDeltaToPosition(position, formConfig.getBranchPositionDelta(), scale); position = GeometryUtility.rotateCoordinateAboutPoint(position, newPosition, twist); twist = GeometryUtility.addDeltaToRotation(twist, formConfig.getBranchTwistDelta()); stackTwist = GeometryUtility.addDeltaToRotation(stackTwist, formConfig.getStackTwistDelta()); } }
string GenerateTransitionsFor(int i) { StringBuilder transitions = new StringBuilder(); var stack = new SimpleStack<int>(); var set = new HashSet<int>(); var automaton = automata[i]; stack.Push(automaton.InitialState); set.Add(automaton.InitialState); Predicate<int> IsFinalSink = (q => (automaton.GetMovesCountFrom(q) == 1 && automaton.IsLoopState(q) && automaton.IsFinalState(q) && automaton.GetMoveFrom(q).Label.IsFull)); Predicate<int> IsNonfinalSink = (q => (!automaton.IsFinalState(q) && (automaton.GetMovesCountFrom(q) == 0 || (automaton.GetMovesCountFrom(q) == 1 && automaton.IsLoopState(q))))); while (stack.IsNonempty) { int q = stack.Pop(); bool q_is_complete = false; if (IsFinalSink(q)) transitions.Append(String.Format(@" State{0}: return true;", q)); else if (IsNonfinalSink(q)) { transitions.Append(String.Format(@" State{0}: return false;", q)); } else { transitions.Append(String.Format(@" State{0}: if (i == k) return {1};", q, (automaton.IsFinalState(q) ? "true" : "false"))); if (automaton.GetMovesCountFrom(q) > 0) //q is a sink { transitions.Append(String.Format(@" if (!UTF8toUTF16(&r, &i, &c, k, str)) return false;")); //--------------------------------------------------------------------- //many potential optimizations can be made in generating the conditions //--------------------------------------------------------------------- var path = solver.True; foreach (var move in automaton.GetMovesFrom(q)) { path = solver.MkDiff(path, move.Label); if (path == solver.False) //this is the last else case { transitions.Append(String.Format(@" goto State{0};", move.TargetState)); q_is_complete = true; } else transitions.Append(String.Format(@" if ({0}) goto State{1};", helper_predicates.GeneratePredicate(move.Label), move.TargetState)); if (set.Add(move.TargetState)) stack.Push(move.TargetState); } } if (!q_is_complete) //reject the input, this corresponds to q being a partial state //the implicit transition is to a deadend sink state transitions.Append(@" return false;"); } } return transitions.ToString(); }
/// <summary> /// Counts the number of nodes (both terminals and nonterminals) in the BDD. /// </summary> public int CountNodes() { if (IsLeaf) return 1; HashSet<BDD> visited = new HashSet<BDD>(); SimpleStack<BDD> stack = new SimpleStack<BDD>(); stack.Push(this); visited.Add(this); while (stack.IsNonempty) { BDD a = stack.Pop(); if (!a.IsLeaf) { if (visited.Add(a.One)) stack.Push(a.One); if (visited.Add(a.Zero)) stack.Push(a.Zero); } } return visited.Count; }
public string Convert(string expression) { StringBuilder sb = new StringBuilder(); SimpleStack<char> stack = new SimpleStack<char>(); foreach (var token in expression) { if (ops.ContainsKey(token)) { op in_op = ops[token]; op top_op = stack.Count > 0 ? ops[stack.Peek()] : new op(); if (in_op.priority == 0 || (!in_op.is_right_associative && in_op.priority > top_op.priority) || (in_op.is_right_associative && in_op.priority >= top_op.priority)) { stack.Push(token); } else { while (stack.Count > 0) { if (ops[stack.Peek()].priority >= in_op.priority) { sb.Append(stack.Pop()); rang--; } else { break; } } if (token == ')') { if (stack.Count == 0) { throw new BracketsInconsistencyException("Обнаружена несогласованность скобок."); } else { stack.Pop(); } } else { stack.Push(token); } } } else { sb.Append(token); rang++; } } while (stack.Count > 0) { sb.Append(stack.Pop()); rang--; } return sb.ToString(); }
public string GenerateCS() { StringBuilder code = new StringBuilder(); var stack = new SimpleStack<int>(); var set = new HashSet<int>(); stack.Push(automaton.InitialState); set.Add(automaton.InitialState); code.Append(String.Format(@" namespace {0} {{ public class {1} {{ public static bool IsMatch(string input) {{ var cs = input.ToCharArray(); int k = input.Length; int c = 0; int i = -1;", namespacename, classname)); Predicate<int> IsFinalSink = (q => (automaton.GetMovesCountFrom(q) == 1 && automaton.IsLoopState(q) && automaton.IsFinalState(q) && automaton.GetMoveFrom(q).Label.IsFull)); Predicate<int> IsNonfinalSink = (q => (automaton.GetMovesCountFrom(q) == 0)); Predicate<int> IsInitialSource = (q => q == automaton.InitialState && automaton.GetMovesCountTo(q) == 0); while (stack.IsNonempty) { int q = stack.Pop(); bool q_is_complete = false; if (IsFinalSink(q)) code.Append(String.Format(@" State{0}: return true;", q)); else { if (!IsInitialSource(q)) code.Append(String.Format(@" State{0}:", q)); code.Append(String.Format(@" i += 1; if (i == k) return {0}; c = (int)cs[i];", automaton.IsFinalState(q).ToString().ToLower())); //--------------------------------------------------------------------- //many potential optimizations can be made in generating the conditions //--------------------------------------------------------------------- var path = solver.True; foreach (var move in automaton.GetMovesFrom(q)) { path = solver.MkDiff(path, move.Label); if (path == solver.False) //this is the last else case { code.Append(String.Format(@" goto State{0};", move.TargetState)); q_is_complete = true; } else code.Append(String.Format(@" if ({0}) goto State{1};", helper_predicates.GeneratePredicate(move.Label), move.TargetState)); if (set.Add(move.TargetState)) stack.Push(move.TargetState); } if (!q_is_complete) //reject the input, this corresponds to q being a partial state //the implicit transition is to a deadend sink state code.Append(@" return false;"); } } code.Append(@" }"); code.Append(helper_predicates.ToString()); code.Append(@" } }"); return code.ToString(); }
public void createBranch(List<IStack> stacks, int offset, IFormConfiguration formConfig, ColourConfiguration colourConfig, Vector3 branchStartPosition, Vector3 branchStartTwist, Vector3 origin, FormBounds formBounds) { Vector3 position = origin; Vector3 stackTwist = formConfig.getStackStartTwist(); float radius = formConfig.getBranchPositionDelta ().x * 20; float rhumbAngle = formConfig.getStartRotation().x * 0.032f; //colour stuff int iterations = formConfig.getStackIterations(); Color modelColour = new Color (colourConfig.getBaseRed(), colourConfig.getBaseGreen(), colourConfig.getBaseBlue(), 1); //if cycling override base colour if (colourConfig.getCycle ()) { modelColour = colourConfig.getCycleColour(); } for (int i = offset; i < offset + iterations; i++) { position.x = formConfig.getStartPosition().x + (radius * (float) Math.Cos(i) / (float) Math.Cosh (Mathf.Rad2Deg * (Mathf.Deg2Rad * i * rhumbAngle))); position.y = formConfig.getStartPosition().y + (radius * (float) Math.Sin(i) / (float) Math.Cosh (Mathf.Rad2Deg * (Mathf.Deg2Rad * i * rhumbAngle))); position.z = formConfig.getStartPosition().z + (radius * (float) Math.Tanh(Mathf.Rad2Deg * (Mathf.Deg2Rad * i * rhumbAngle))); formBounds.calculateNewBounds(position); //Stack Shape IStack stack = new SimpleStack(); //default if (formConfig.getStackShape() == IFormConfiguration.StackShape.Sphere) { //spehere stack = new SimpleStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Box) { //box stack = new SimpleBoxStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Complex1) { //complex 1 stack = new ComplexStack(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.Complex2) { //complex 2 stack = new ComplexStack2(); } else if (formConfig.getStackShape() == IFormConfiguration.StackShape.SimpleTorus) { //simple Torus stack = new SimpleTorusStack(); } //colour stuff order is important Color stackColour = modelColour; if (colourConfig.getFadeColour()) { //fade colour in stackColour = ColourUtility.fadeModelColour(modelColour, iterations, i); } if (colourConfig.getPulse()) { //do pulse stackColour = colourConfig.getPulseColourForStack(stackColour, i); } stack.initialise(position, stackTwist, 0.5f, stackColour); stacks.Add(stack); stackTwist = GeometryUtility.addDeltaToRotation(stackTwist, formConfig.getStackTwistDelta()); } }