using System; using System.Collections.Generic; using System.Text; namespace OpenTraffic.Collections { class TrafficHeapWithUpdate : TrafficHeap where K : IComparable where T : class { Dictionary posObject; public TrafficHeapWithUpdate(int size) : base(size) { posObject = new Dictionary(); } public override void Add(KeyValuePair kv) { posObject.Add(kv.Value, last); base.Add(kv); System.Diagnostics.Debug.Assert(this.ControlInnerState()); } public override KeyValuePair Pop() { TrafficProfile.GetProfile().Start("pop_update"); // System.Diagnostics.Debug.Assert(this.ControlInnerState()); posObject.Remove(data[0].Value); if (posObject.Count != 0) { posObject[data[data.Count - 1].Value] = 0; } TrafficProfile.GetProfile().Stop(); return base.Pop(); } protected override void SwitchElement(int i1, int i2) { posObject[data[i2].Value] = i1; posObject[data[i1].Value] = i2; base.SwitchElement(i1, i2); } public void Update(K key, T v) { int pos; if (posObject.TryGetValue(v, out pos)) { if (key.CompareTo(data[pos].Key) > 0) { this.BubbleUp(pos); } else { this.BubbleDown(pos); } } else { Add(key, v); } System.Diagnostics.Debug.Assert(this.ControlInnerState()); } public override bool ControlInnerState() { foreach (KeyValuePair kv in posObject) { if (data[kv.Value].Value != kv.Key) return false; } return base.ControlInnerState(); } /* KeyValuePair Pop(); void Clear(); void Add(K k, T t); KeyValuePair Peek(); int Count { get; }*/ } }