public static StackValue GetArrayElementAt(StackValue array, int index, Core core) { int ptr = (int)array.opdata; HeapElement hs = core.Rmem.Heap.Heaplist[ptr]; return(hs.Stack[index]); }
/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// /// Note this function doesn't support the replication of array indexing. /// </summary> /// <param name="array"></param> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue SetValueForIndices(StackValue array, StackValue[] indices, StackValue value, Core core) { Validity.Assert(array.IsArray || array.IsString); for (int i = 0; i < indices.Length - 1; ++i) { StackValue index = indices[i]; HeapElement he = GetHeapElement(array, core); StackValue subArray; if (index.IsNumeric) { index = index.ToInteger(); int absIndex = he.ExpandByAcessingAt((int)index.opdata); subArray = he.Stack[absIndex]; } else { subArray = GetValueFromIndex(array, index, core); } // auto-promotion if (!subArray.IsArray) { subArray = core.Heap.AllocateArray(new StackValue[] { subArray }, null); GCUtils.GCRetain(subArray, core); SetValueForIndex(array, index, subArray, core); } array = subArray; } return(SetValueForIndex(array, indices[indices.Length - 1], value, core)); }
private static int GetMaxRankForArray(StackValue array, Core core, int tracer) { if (tracer > RECURSION_LIMIT) { throw new CompilerInternalException("Internal Recursion limit exceeded in Rank Check - Possible heap corruption {3317D4F6-4758-4C19-9680-75B68DA0436D}"); } if (!StackUtils.IsArray(array)) { return(0); } //throw new ArgumentException("The stack value provided was not an array"); int ret = 1; //This is the element on the heap that manages the data structure HeapElement heapElement = GetHeapElement(array, core); int largestSub = 0; for (int i = 0; i < heapElement.VisibleSize; ++i) { StackValue sv = heapElement.Stack[i]; if (sv.optype == AddressType.ArrayPointer) { int subArrayRank = GetMaxRankForArray(sv, core, tracer + 1); largestSub = Math.Max(subArrayRank, largestSub); } } return(largestSub + ret); }
public void HeapTest() { _heap.Insert(new HeapElement(0)); _heap.Insert(new HeapElement(2)); _heap.Insert(new HeapElement(1)); _heap.Insert(new HeapElement(-5)); _heap.Insert(new HeapElement(10)); Assert.That(_heap.Validate(), Is.EqualTo(true)); Assert.That(_heap.PeekMin().value, Is.EqualTo(-5)); _heap.RemoveMin(); Assert.That(_heap.Validate(), Is.EqualTo(true)); Assert.That(_heap.PeekMin().value, Is.EqualTo(0)); var element4 = new HeapElement(4); _heap.Insert(element4); Assert.That(_heap.Validate(), Is.EqualTo(true)); _heap.Remove(element4); Assert.That(_heap.Validate(), Is.EqualTo(true)); Assert.That(_heap.Count, Is.EqualTo(4)); }
/// <summary> /// Generate type statistics for given layer of an array /// </summary> /// <param name="array"></param> /// <returns></returns> public static Dictionary <ClassNode, int> GetTypeStatisticsForLayer(StackValue array, Core core) { if (!array.IsArray) { Dictionary <ClassNode, int> ret = new Dictionary <ClassNode, int>(); ret.Add(core.ClassTable.ClassNodes[array.metaData.type], 1); return(ret); } Dictionary <ClassNode, int> usageFreq = new Dictionary <ClassNode, int>(); //This is the element on the heap that manages the data structure HeapElement heapElement = GetHeapElement(array, core); foreach (var sv in heapElement.VisibleItems) { ClassNode cn = core.ClassTable.ClassNodes[sv.metaData.type]; if (!usageFreq.ContainsKey(cn)) { usageFreq.Add(cn, 0); } usageFreq[cn] = usageFreq[cn] + 1; } return(usageFreq); }
public void FibonacciHeapMergeTestMinHeaps() { // create two minheaps based on the priority value of the elements. FibonacciHeap <HeapElement> heap1 = new FibonacciHeap <HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true); FibonacciHeap <HeapElement> heap2 = new FibonacciHeap <HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true); FillHeap(heap1, 100); FillHeap(heap2, 100); Assert.AreEqual(100, heap1.Count); Assert.AreEqual(100, heap2.Count); // merge heap1 into heap2. heap2 will be empty afterwards heap1.Merge(heap2); Assert.AreEqual(200, heap1.Count); Assert.AreEqual(0, heap2.Count); // check if they are inserted correctly HeapElement previous = heap1.ExtractRoot(); HeapElement current = heap1.ExtractRoot(); while (current != null) { Assert.IsTrue(previous.Priority <= current.Priority); previous = current; current = heap1.ExtractRoot(); } // heap1 should be empty as well Assert.AreEqual(0, heap1.Count); }
/// <summary> /// = array[index] /// </summary> /// <param name="array"></param> /// <param name="index"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue GetValueFromIndex(StackValue array, int index, RuntimeCore runtimeCore) { Validity.Assert(array.IsArray || array.IsString); RuntimeMemory rmem = runtimeCore.RuntimeMemory; if (array.IsString) { string str = rmem.Heap.GetString(array); if (str == null) { return(StackValue.Null); } if (index < 0) { index = index + str.Length; } if (index >= str.Length || index < 0) { runtimeCore.RuntimeStatus.LogWarning(ProtoCore.Runtime.WarningID.kOverIndexing, Resources.kArrayOverIndexed); return(StackValue.Null); } return(rmem.Heap.AllocateString(str.Substring(index, 1))); } else { HeapElement he = GetHeapElement(array, runtimeCore); return(StackUtils.GetValue(he, index, runtimeCore)); } }
/// <summary> /// Retrieve the first non-array element in an array /// </summary> /// <param name="svArray"></param> /// <param name="sv"></param> /// <param name="core"></param> /// <returns> true if the element was found </returns> public static bool GetFirstNonArrayStackValue(StackValue svArray, ref StackValue sv, Core core) { if (!svArray.IsArray) { return(false); } HeapElement he = core.Heap.GetHeapElement(svArray); if (null == he.Stack || he.Stack.Length == 0) { return(false); } while (he.Stack[0].IsArray) { he = core.Heap.GetHeapElement(he.Stack[0]); // Handle the case where the array is valid but empty if (he.Stack.Length == 0) { return(false); } } sv = he.Stack[0].ShallowClone(); return(true); }
/// <summary> /// array[index] = value. Here index can be any type. /// /// Note this function doesn't support the replication of array indexing. /// </summary> /// <param name="array"></param> /// <param name="index"></param> /// <param name="value"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue SetValueForIndex(StackValue array, StackValue index, StackValue value, RuntimeCore runtimeCore) { Validity.Assert(array.IsArray); if (index.IsNumeric) { index = index.ToInteger(); return(SetValueForIndex(array, (int)index.opdata, value, runtimeCore)); } else { HeapElement he = GetHeapElement(array, runtimeCore); if (he.Dict == null) { he.Dict = new Dictionary <StackValue, StackValue>(new StackValueComparer(runtimeCore)); } StackValue oldValue; if (!he.Dict.TryGetValue(index, out oldValue)) { oldValue = StackValue.Null; } he.Dict[index] = value; return(oldValue); } }
/// <summary> /// Get an array's next key /// </summary> /// <param name="key"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue GetNextKey(StackValue key, RuntimeCore runtimeCore) { StackValue array; int index; if (!key.TryGetArrayKey(out array, out index)) { return(StackValue.Null); } int nextIndex = Constants.kInvalidIndex; if (array.IsString) { var str = runtimeCore.Heap.GetString(array); if (str.Length > index + 1) { nextIndex = index + 1; } } else { HeapElement he = GetHeapElement(array, runtimeCore); if ((he.VisibleSize > index + 1) || (he.Dict != null && he.Dict.Count + he.VisibleSize > index + 1)) { nextIndex = index + 1; } } return(nextIndex == Constants.kInvalidIndex ? StackValue.Null : StackValue.BuildArrayKey(array, nextIndex)); }
/// <summary> /// array[index] = value. Here index can be any type. /// /// Note this function doesn't support the replication of array indexing. /// </summary> /// <param name="array"></param> /// <param name="index"></param> /// <param name="value"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue SetValueForIndex(StackValue array, StackValue index, StackValue value, Core core) { Validity.Assert(array.IsArray || array.IsString); if (index.IsNumeric) { index = index.ToInteger(); return(SetValueForIndex(array, (int)index.opdata, value, core)); } else { HeapElement he = GetHeapElement(array, core); if (he.Dict == null) { he.Dict = new Dictionary <StackValue, StackValue>(new StackValueComparer(core)); } StackValue oldValue; GCUtils.GCRetain(value, core); if (he.Dict.TryGetValue(index, out oldValue)) { GCUtils.GCRelease(oldValue, core); } else { oldValue = StackValue.Null; GCUtils.GCRetain(index, core); } he.Dict[index] = value; return(oldValue); } }
/// <summary> /// Gets all array elements in a List of given type using the given converter to /// convert the stackValue. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="core"></param> /// <param name="converter"></param> /// <returns></returns> public static List <T> GetValues <T>(StackValue array, Core core, Func <StackValue, T> converter) { Validity.Assert(StackUtils.IsArray(array)); if (!StackUtils.IsArray(array)) { return(null); } HeapElement he = GetHeapElement(array, core); List <T> values = new List <T>(); foreach (var sv in he.Stack) { values.Add(converter(sv)); } if (he.Dict != null) { foreach (var sv in he.Dict.Values) { values.Add(converter(sv)); } } return(values); }
public void FibonacciHeapUpdateKeyMinHeap() { FibonacciHeap <HeapElement> heap = new FibonacciHeap <HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true); FillHeap(heap, 100); Assert.AreEqual(100, heap.Count); // add new element. Give it a priority of 50 HeapElement element = new HeapElement(50, "Value: 50"); heap.Insert(element); Assert.AreEqual(101, heap.Count); // update key to 10, using an action lambda heap.UpdateKey(element, (a, b) => a.Priority = b, 10); Assert.AreEqual(10, element.Priority); // check if the heap is still correct HeapElement previous = heap.ExtractRoot(); HeapElement current = heap.ExtractRoot(); while (current != null) { Assert.IsTrue(previous.Priority <= current.Priority); previous = current; current = heap.ExtractRoot(); } // heap should be empty Assert.AreEqual(0, heap.Count); }
/// <summary> /// Generate type statistics for given layer of an array /// </summary> /// <param name="array"></param> /// <returns></returns> public static Dictionary <ClassNode, int> GetTypeStatisticsForLayer(StackValue array, Core core) { if (!StackUtils.IsArray(array)) { Dictionary <ClassNode, int> ret = new Dictionary <ClassNode, int>(); ret.Add(core.DSExecutable.classTable.ClassNodes[(int)array.metaData.type], 1); return(ret); } Dictionary <ClassNode, int> usageFreq = new Dictionary <ClassNode, int>(); //This is the element on the heap that manages the data structure HeapElement heapElement = GetHeapElement(array, core); for (int i = 0; i < heapElement.VisibleSize; ++i) { StackValue sv = heapElement.Stack[i]; ClassNode cn = core.DSExecutable.classTable.ClassNodes[(int)sv.metaData.type]; if (!usageFreq.ContainsKey(cn)) { usageFreq.Add(cn, 0); } usageFreq[cn] = usageFreq[cn] + 1; } return(usageFreq); }
public static Dictionary <int, StackValue> GetTypeExamplesForLayer(StackValue array, Core core) { if (!StackUtils.IsArray(array)) { Dictionary <int, StackValue> ret = new Dictionary <int, StackValue>(); ret.Add((int)array.metaData.type, array); return(ret); } Dictionary <int, StackValue> usageFreq = new Dictionary <int, StackValue>(); //This is the element on the heap that manages the data structure HeapElement heapElement = GetHeapElement(array, core); for (int i = 0; i < heapElement.VisibleSize; ++i) { StackValue sv = heapElement.Stack[i]; if (!usageFreq.ContainsKey((int)sv.metaData.type)) { usageFreq.Add((int)sv.metaData.type, sv); } } return(usageFreq); }
/// <summary> /// Get all keys from an array /// </summary> /// <param name="array"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue[] GetKeys(StackValue array, Core core) { Validity.Assert(StackUtils.IsArray(array)); if (!StackUtils.IsArray(array)) { return(null); } HeapElement he = GetHeapElement(array, core); List <StackValue> keys = new List <StackValue>(); for (int i = 0; i < he.VisibleSize; ++i) { keys.Add(StackUtils.BuildInt(i)); } if (he.Dict != null) { foreach (var key in he.Dict.Keys) { keys.Add(key); } } return(keys.ToArray()); }
public static StackValue GetNextKey(StackValue key, Core core) { if (StackUtils.IsNull(key) || key.optype != AddressType.ArrayKey || key.opdata < 0 || key.metaData.type < 0) { return(StackUtils.BuildNull()); } int ptr = key.metaData.type; int index = (int)key.opdata; HeapElement he = core.Heap.Heaplist[ptr]; if ((he.VisibleSize > index + 1) || (he.Dict != null && he.Dict.Count + he.VisibleSize > index + 1)) { StackValue newKey = key; newKey.opdata = newKey.opdata + 1; return(newKey); } return(StackUtils.BuildNull()); }
/// <summary> /// array[index] = value. Here index can be any type. /// /// Note this function doesn't support the replication of array indexing. /// </summary> /// <param name="array"></param> /// <param name="index"></param> /// <param name="value"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue SetValueForIndex(StackValue array, StackValue index, StackValue value, Core core) { Validity.Assert(StackUtils.IsArray(array) || StackUtils.IsString(array)); if (StackUtils.IsNumeric(index)) { index = index.AsInt(); return(SetValueForIndex(array, (int)index.opdata, value, core)); } else { HeapElement he = GetHeapElement(array, core); if (he.Dict == null) { he.Dict = new Dictionary <StackValue, StackValue>(new StackValueComparer(core)); } StackValue oldValue; if (!he.Dict.TryGetValue(index, out oldValue)) { oldValue = StackUtils.BuildNull(); } GCUtils.GCRetain(index, core); GCUtils.GCRetain(value, core); he.Dict[index] = value; return(oldValue); } }
public HeapElement <TKey, TValue> ExtractMin() { if (heap.Count == 0) { return(null); } else if (heap.Count == 1) { HeapElement <TKey, TValue> min = heap[0]; heap.RemoveAt(0); indexMap.Remove(min.value); return(min); } else { int n = heap.Count; HeapElement <TKey, TValue> min = heap[0]; indexMap[heap[n - 1].value] = 0; heap[0] = heap[n - 1]; heap.RemoveAt(n - 1); indexMap.Remove(min.value); MinHeapifyDown(0); return(min); } }
public static StackValue SetDataAtIndices(StackValue array, int[] indices, StackValue data, Core core) { Validity.Assert(array.optype == AddressType.ArrayPointer || array.optype == AddressType.String); StackValue arrayItem = array; for (int i = 0; i < indices.Length - 1; ++i) { HeapElement arrayHeap = core.Heap.Heaplist[(int)arrayItem.opdata]; int index = arrayHeap.ExpandByAcessingAt(indices[i]); arrayItem = arrayHeap.Stack[index]; if (arrayItem.optype == AddressType.String) { core.RuntimeStatus.LogWarning(RuntimeData.WarningID.kOverIndexing, ProtoCore.RuntimeData.WarningMessage.kStringOverIndexed); return(StackUtils.BuildNull()); } else if (arrayItem.optype != AddressType.ArrayPointer) { StackValue sv = HeapUtils.StoreArray(new StackValue[] { arrayItem }, core); GCUtils.GCRetain(sv, core); arrayHeap.Stack[index] = sv; arrayItem = arrayHeap.Stack[index]; } } return(ArrayUtils.SetDataAtIndex(arrayItem, indices[indices.Length - 1], data, core)); }
/// <summary> /// Pull the heap element out of an array pointer /// </summary> /// <param name="sv"></param> /// <param name="core"></param> /// <returns></returns> public static HeapElement GetHeapElement(StackValue sv, Core core) { Validity.Assert(IsArray(sv)); HeapElement he = core.Heap.Heaplist[(int)sv.opdata]; return(he); }
public void Add(HeapElement <T> obj) { itemList.Add(obj); SwapItem(itemList.Count - 1, lastPos); obj.id = indexList.Count; indexList.Add(indexList.Count); TrickleUp(lastPos++); }
public IList <IList <int> > GetSkyline(int[][] buildings) { var lines = new List <HeapElement <int> > (); var rLists = new List <IList <int> > (); int i = 1; foreach (var item in buildings) { lines.Add(new HeapElement <int> (item[2] * -1, item[0], i)); lines.Add(new HeapElement <int> (item[2], item[1], i++)); } lines.Sort(delegate(HeapElement <int> event1, HeapElement <int> event2) { if (event1.x == event2.x) { return(event1.h.CompareTo(event2.h)); } return(event1.x.CompareTo(event2.x)); }); var maxHeap = new MaxHeap <int> (lines.Count / 2 + 1); var prev = new HeapElement <int> (0, 0, 0); maxHeap.Add(prev, 0); foreach (var item in lines) { if (item.h < 0) { item.h *= -1; maxHeap.Add(item, item.id); } else { maxHeap.Remove(item); } var cur = maxHeap.Max(); if (cur.h != prev.h) { var list = new List <int> (); list.Add(item.x); list.Add(cur.h); rLists.Add(list); prev = cur; } } //maxHeap.Sort(); maxHeap.Pop(); maxHeap.Add(new HeapElement <int> (27)); maxHeap.Add(new HeapElement <int> (17)); maxHeap.Add(new HeapElement <int> (47)); maxHeap.Add(new HeapElement <int> (37)); maxHeap.Sort(); return(rLists); }
private void swap(int i, int j) { HeapElement temp = heap[i]; heap[i] = heap[j]; hash[heap[j].s] = i; heap[j] = temp; hash[temp.s] = j; }
public int CompareTo(HeapElement <T> compareEvent) { if (compareEvent == null) { return(1); } //return Math.Abs(compareEvent.h).CompareTo(Math.Abs(h)); return(h.CompareTo(compareEvent.h)); }
private void Swap(int i, int j) { HeapElement temp = m_heap[i]; m_heap[i] = m_heap[j]; m_hash[m_heap[j].Coordinates] = i; m_heap[j] = temp; m_hash[temp.Coordinates] = j; }
// Concatenates the list of strings into a single string of comma separated types /*private string GetTypesHelper(List<string> types) * { * string type = ""; * foreach(string s in types) * { * if(string.IsNullOrEmpty(type)) * type = s; * else * type += "," + s; * } * return type; * }*/ /*private string GetAssembly(StackValue sv) * { * if (sv.IsObject()) * { * ClassNode classNode = core.ClassTable.ClassNodes[(int)sv.metaData.type]; * assemblyName = classNode.ExternLib; * * } * else if (sv.IsArray) * { * assemblyName = GetTypesHelper(); * } * return assemblyName; * }*/ // Returns a list of unique types in the input array //private List<string> GetArrayTypes(StackValue svData) private Dictionary <string, List <string> > GetArrayTypes(StackValue svData) { Dictionary <string, List <string> > asmTypes = new Dictionary <string, List <string> >(); //List<string> types = new List<string>(); Validity.Assert(svData.IsArray); int ptr = (int)svData.opdata; HeapElement hs = core.Heap.Heaplist[ptr]; foreach (var sv in hs.VisibleItems) { if (sv.IsArray) { Dictionary <string, List <string> > types = GetArrayTypes(sv); foreach (var kvp in types) { if (!asmTypes.ContainsKey(kvp.Key)) { asmTypes.Add(kvp.Key, kvp.Value); } else { List <string> cTypes = asmTypes[kvp.Key]; // Check if each type in kvp.Value is not in cTypes foreach (string s in kvp.Value) { if (!cTypes.Contains(s)) { cTypes.Add(s); } } } } } else { Dictionary <string, List <string> > asmType = GetType(sv); var iter = asmType.GetEnumerator(); iter.MoveNext(); KeyValuePair <string, List <string> > kvp = iter.Current; if (!asmTypes.ContainsKey(kvp.Key)) { asmTypes.Add(kvp.Key, kvp.Value); } else { List <string> cTypes = asmTypes[kvp.Key]; cTypes.AddRange(kvp.Value); } } } //return types; return(asmTypes); }
/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// </summary> /// <param name="array"></param> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="t"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue SetValueForIndices(StackValue array, List <StackValue> indices, StackValue value, Type t, Core core) { StackValue[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, core); if (zippedIndices == null || zippedIndices.Length == 0) { return(StackUtils.BuildNull()); } if (zippedIndices.Length == 1) { StackValue coercedData = TypeSystem.Coerce(value, t, core); GCUtils.GCRetain(coercedData, core); return(ArrayUtils.SetValueForIndices(array, zippedIndices[0], coercedData, core)); } else if (value.optype == AddressType.ArrayPointer) { // Replication happens on both side. if (t.rank > 0) { t.rank = t.rank - 1; if (t.rank == 0) { t.IsIndexable = false; } } HeapElement dataHeapElement = GetHeapElement(value, core); int length = Math.Min(zippedIndices.Length, dataHeapElement.VisibleSize); StackValue[] oldValues = new StackValue[length]; for (int i = 0; i < length; ++i) { StackValue coercedData = TypeSystem.Coerce(dataHeapElement.Stack[i], t, core); GCUtils.GCRetain(coercedData, core); oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, core); } // The returned old values shouldn't have any key-value pairs return(HeapUtils.StoreArray(oldValues, null, core)); } else { // Replication is only on the LHS, so collect all old values // and return them in an array. StackValue coercedData = TypeSystem.Coerce(value, t, core); GCUtils.GCRetain(coercedData, core); StackValue[] oldValues = new StackValue[zippedIndices.Length]; for (int i = 0; i < zippedIndices.Length; ++i) { oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, core); } // The returned old values shouldn't have any key-value pairs return(HeapUtils.StoreArray(oldValues, null, core)); } }
/// <summary> /// = array[index]. /// /// Note this function doesn't support the replication of array indexing. /// </summary> /// <param name="array"></param> /// <param name="index"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue GetValueFromIndex(StackValue array, StackValue index, Core core) { Validity.Assert(array.IsArray || array.IsString); if (!array.IsArray && !array.IsString) { return(StackValue.Null); } if (index.IsNumeric) { index = index.ToInteger(); return(GetValueFromIndex(array, (int)index.opdata, core)); } else if (index.IsArrayKey) { int fullIndex = (int)index.opdata; HeapElement he = GetHeapElement(array, core); if (he.VisibleSize > fullIndex) { return(GetValueFromIndex(array, fullIndex, core)); } else { fullIndex = fullIndex - he.VisibleSize; if (he.Dict != null && he.Dict.Count > fullIndex) { int count = 0; foreach (var key in he.Dict.Keys) { if (count == fullIndex) { return(he.Dict[key]); } count = count + 1; } } } return(StackValue.Null); } else { HeapElement he = GetHeapElement(array, core); StackValue value = StackValue.Null; if (he.Dict != null && he.Dict.TryGetValue(index, out value)) { return(value); } else { return(StackValue.Null); } } }
/// <summary> /// = array[index]. /// /// Note this function doesn't support the replication of array indexing. /// </summary> /// <param name="array"></param> /// <param name="index"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue GetValueFromIndex(StackValue array, StackValue index, Core core) { Validity.Assert(StackUtils.IsArray(array) || StackUtils.IsString(array)); if (!StackUtils.IsArray(array) && !StackUtils.IsString(array)) { return(StackUtils.BuildNull()); } if (StackUtils.IsNumeric(index)) { index = index.AsInt(); return(GetValueFromIndex(array, (int)index.opdata, core)); } else if (index.optype == AddressType.ArrayKey) { int fullIndex = (int)index.opdata; HeapElement he = GetHeapElement(array, core); if (he.VisibleSize > fullIndex) { return(GetValueFromIndex(array, fullIndex, core)); } else { fullIndex = fullIndex - he.VisibleSize; if (he.Dict != null && he.Dict.Count > fullIndex) { int count = 0; foreach (var key in he.Dict.Keys) { if (count == fullIndex) { return(he.Dict[key]); } count = count + 1; } } } return(StackUtils.BuildNull()); } else { HeapElement he = GetHeapElement(array, core); StackValue value = StackUtils.BuildNull(); if (he.Dict != null && he.Dict.TryGetValue(index, out value)) { return(value); } else { return(StackUtils.BuildNull()); } } }
public void FibonacciHeapUpdateKeyMinHeap() { FibonacciHeap<HeapElement> heap = new FibonacciHeap<HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true); FillHeap(heap, 100); Assert.AreEqual(100, heap.Count); // add new element. Give it a priority of 50 HeapElement element = new HeapElement(50, "Value: 50"); heap.Insert(element); Assert.AreEqual(101, heap.Count); // update key to 10, using an action lambda heap.UpdateKey(element, (a, b) => a.Priority = b, 10); Assert.AreEqual(10, element.Priority); // check if the heap is still correct HeapElement previous = heap.ExtractRoot(); HeapElement current = heap.ExtractRoot(); while(current != null) { Assert.IsTrue(previous.Priority <= current.Priority); previous = current; current = heap.ExtractRoot(); } // heap should be empty Assert.AreEqual(0, heap.Count); }
private void WriteTerm(HeapElement maxElement) { streamWriter.WriteLine(maxElement.Word + " " + maxElement.PostingList.Length); string[] postingList = maxElement.PostingList.Trim().Split(new[] {' '}); foreach (string s in postingList) { streamWriter.Write(s + " "); } streamWriter.WriteLine(); }
// Inserts a node (id, val) into the heap public bool Insert(int id, double val) { if (id >= max_size || look_up[id] == -1) { return false; } HeapElement e = new HeapElement(id, val); heap[nxt_idx] = e; look_up[id] = nxt_idx; nxt_idx++; PercolateUp(look_up[id]); return true; }
public void Equals(HeapElement arg1) { cost = arg1.cost; index = arg1.index; }