public void SetByKey(object key, object value) { key = FixKey(key); // Update heap use throwing an exception on failure // before making any changes to the array. int keysize = HeapTrackerObject.Size(key); int newheapuse = heapUse; object oldval; if (dnary.TryGetValue(key, out oldval)) { newheapuse -= keysize + HeapTrackerObject.Size(oldval); } if (value != null) { newheapuse += keysize + HeapTrackerObject.Size(value); } heapUse = inst.UpdateHeapUse(heapUse, newheapuse); // Save new value in array, replacing one of same key if there. // null means remove the value, ie, script did array[key] = undef. if (value != null) { dnary[key] = value; } else { dnary.Remove(key); // Shrink the enumeration array, but always leave at least one element. if ((array != null) && (dnary.Count < array.Length / 2)) { Array.Resize <KeyValuePair <object, object> >(ref array, array.Length / 2); } } // The enumeration array is invalid because the dictionary has been modified. // Next time a ForEach() call happens, it will repopulate 'array' as elements are retrieved. arrayValid = 0; }
public void RecvArrayObj(RecvArrayObjDelegate recvObj) { heapUse = inst.UpdateHeapUse(heapUse, EMPTYHEAP); // Cause any enumeration to refill the array from the sorted dictionary. // Since it is a sorted dictionary, any enumerations will be in the same // order as on the sending side. arrayValid = 0; enumrValid = false; // Fill dictionary. dnary.Clear(); int count = (int)recvObj(); while (--count >= 0) { object key = FixKey(recvObj()); object val = recvObj(); int htuse = HeapTrackerObject.Size(key) + HeapTrackerObject.Size(val); heapUse = inst.UpdateHeapUse(heapUse, heapUse + htuse); dnary.Add(key, val); } }