From 997c32e3e71023be983af6a92e9a34cfdce3ca53 Mon Sep 17 00:00:00 2001 From: vladimir_holy <vladimir.holy7@gmail.com> Date: Thu, 23 May 2024 10:52:54 +0200 Subject: [PATCH] re #11511 - heap fixed --- Core/ARunner.cs | 6 ++-- Core/DataTypeNS/StackObj.cs | 18 ++++++------ Core/ExpressionSolverNS/AFSSolver.cs | 2 -- Core/MemoryNS/Memory.cs | 43 ++++++++++------------------ Test_Stack.fprg | 4 +-- 5 files changed, 29 insertions(+), 44 deletions(-) diff --git a/Core/ARunner.cs b/Core/ARunner.cs index 23e63b8..c075a62 100644 --- a/Core/ARunner.cs +++ b/Core/ARunner.cs @@ -29,10 +29,10 @@ namespace Core public void PrintHeap() { Console.WriteLine("---------------------\nHeap\n---------------------"); - List<IHeapObj> heap = Memory.getHeapItems(); - foreach (IHeapObj item in heap) + Dictionary<int, IHeapObj> heap = Memory.getHeapItems(); + foreach (var item in heap) { - Console.WriteLine(item.ToString()); + Console.WriteLine(item.Value.ToString()); } Console.WriteLine(); } diff --git a/Core/DataTypeNS/StackObj.cs b/Core/DataTypeNS/StackObj.cs index bf3bf9a..edc2b54 100644 --- a/Core/DataTypeNS/StackObj.cs +++ b/Core/DataTypeNS/StackObj.cs @@ -28,15 +28,17 @@ namespace Core.DataTypeNS } set { - EDataType incomingType = EDataTypeConverter.TypeToDataType(value.GetType()); - if (incomingType != this.Type) - { - this.value = Parse(value); - this.IsInitialized = this.value != null; - return; + if (value != null) { + EDataType incomingType = EDataTypeConverter.TypeToDataType(value.GetType()); + if (incomingType != this.Type) + { + this.value = Parse(value); + this.IsInitialized = this.value != null; + return; + } + this.IsInitialized = true; + this.value = value; } - this.IsInitialized = true; - this.value = value; } } diff --git a/Core/ExpressionSolverNS/AFSSolver.cs b/Core/ExpressionSolverNS/AFSSolver.cs index db42118..679fe46 100644 --- a/Core/ExpressionSolverNS/AFSSolver.cs +++ b/Core/ExpressionSolverNS/AFSSolver.cs @@ -134,7 +134,6 @@ namespace Core.ExpressionSolverNS */ public override bool Solve() { - bool firstNode = true; while (Queue.Count > 0) { ITreeNode iTreeNode = Queue.Peek(); @@ -143,7 +142,6 @@ namespace Core.ExpressionSolverNS return false; } this.Result = iTreeNode.GetStackObj(); - firstNode = false; Queue.Dequeue(); } this.IsSolved = true; diff --git a/Core/MemoryNS/Memory.cs b/Core/MemoryNS/Memory.cs index f392051..56566cc 100644 --- a/Core/MemoryNS/Memory.cs +++ b/Core/MemoryNS/Memory.cs @@ -7,13 +7,13 @@ public static class Memory { private static Stack<Function> FunctionStack = new Stack<Function>(); - private static List<IHeapObj> Heap = new List<IHeapObj>(); + private static Dictionary<int, IHeapObj> Heap = new Dictionary<int, IHeapObj>(); private static Stack<StackObj> ReturnStack = new Stack<StackObj>(); public static void initMemory() { FunctionStack = new Stack<Function>(); - Heap = new List<IHeapObj>(); + Heap = new Dictionary<int, IHeapObj>(); ReturnStack = new Stack<StackObj>(); } @@ -39,35 +39,21 @@ */ public static int addHeapObj(IHeapObj heapObj) { - for (int i = 0; i < Heap.Count + 1; i++) + for (int i = 0; i < int.MaxValue; i++) { - try + if(!Heap.ContainsKey(i)) { - if (Heap[i] == null) - { - Heap.Insert(i, heapObj); - return i; - }; - } - catch - { - Heap.Insert(i, heapObj); + Heap.Add(i, heapObj); return i; - }; + } } - return Heap.Count; + return -1; } public static IHeapObj? getHeapObj(int index) { - try - { - return (IHeapObj)Heap[index]; - } - catch - { - return null; - } + if (Heap.TryGetValue(index, out IHeapObj heapObj)) return heapObj; + return null; } /* @@ -85,7 +71,7 @@ * Methods to return content of Stack and Heap */ - public static List<IHeapObj> getHeapItems() + public static new Dictionary<int, IHeapObj> getHeapItems() { return Heap; } @@ -102,9 +88,9 @@ public static void ClearHeap() { - foreach (IHeapObj heapObj in Heap) + foreach (var item in Heap) { - heapObj.Visited = false; + item.Value.Visited = false; } foreach (Function function in FunctionStack) @@ -128,8 +114,9 @@ } List<Int32> objToRemove = new List<Int32>(); - foreach (IHeapObj heapObj in Heap) + foreach (var item in Heap) { + IHeapObj heapObj = item.Value; if (!heapObj.Visited) { objToRemove.Add(heapObj.HeapObjID); @@ -139,7 +126,7 @@ { if (id < Heap.Count) { - Heap.RemoveAt(id); + if (Heap.ContainsKey(id)) { Heap.Remove(id); } } } } diff --git a/Test_Stack.fprg b/Test_Stack.fprg index aa10fe0..0749bbb 100644 --- a/Test_Stack.fprg +++ b/Test_Stack.fprg @@ -78,7 +78,7 @@ <function name="createStack" type="None" variable=""> <parameters/> <body> - <assign variable="this.data" expression="new String[2]"/> + <assign variable="this.data" expression="new String[5]"/> <assign variable="this.freeIndex" expression="0"/> </body> </function> @@ -129,8 +129,6 @@ <declare name="I" type="Integer"/> <for variable="I" start="0" end="size(help)" direction="inc" step="1"> <assign variable="this.data[I]" expression="help[I]"/> - <output expression="this.data[I]" newline="True"/> - <output expression="help[I]" newline="True"/> </for> </body> </function> -- GitLab