天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

用 C# 實現帶鍵值的優先隊列

在上一篇隨筆 Timus 1037. Memory management 的“進一步的討論”小節中,我提到:

這個程序中使用 KeyedPriorityQueue存儲已分配的“內存塊”,使用 PriorityQueue存儲尚未分配的“自由塊”。這兩個優先隊列的算法是一樣的,可以想辦法合并。這將在下一篇隨筆中討論。

現在,就開始行動吧。

首先,需要一個接口,用來獲取鍵以及獲取和設置值,如下所示:

namespace Skyiv.Util{  interface IKeyValue  {    K GetKey(T x);    V GetValue(T x);    void SetValue(T x, V v);  }}

接著,就是我們的帶鍵值的優先隊列 KeyedPriorityQueue 登場了:

using System;using System.Collections.Generic;namespace Skyiv.Util{  class KeyedPriorityQueue  {    IComparer comparer;    IKeyValue kver;    Dictionaryint> keys;    bool hasKey;    T[] heap;    public int Count { get; private set; }    public KeyedPriorityQueue(IKeyValue kv) : this(null, kv) { }    public KeyedPriorityQueue(int capacity, IKeyValue kv) : this(capacity, null, kv) { }    public KeyedPriorityQueue(IComparer comparer, IKeyValue kv) : this(16, comparer, kv) { }    public KeyedPriorityQueue(int capacity, IComparer comparer, IKeyValue kver)    {      this.keys = new Dictionaryint>();      this.comparer = (comparer == null) ? Comparer.Default : comparer;      this.kver = kver;      this.hasKey = (kver != null);      this.heap = new T[capacity];    }    public bool ContainsKey(K key)    {      return keys.ContainsKey(key);    }    public void Update(T v)    {      if (!hasKey) throw new NotSupportedException();      if (typeof(T).IsValueType) throw new InvalidOperationException("T 不能是值類型");      if (!ContainsKey(kver.GetKey(v))) throw new ArgumentOutOfRangeException("v", v, "更新優先隊列時無此健值");      var id = keys[kver.GetKey(v)];      var cmp = comparer.Compare(v, heap[id]);      kver.SetValue(heap[id], kver.GetValue(v)); // 注意: 這一句要求 T 是引用類型,不能是值類型       if (cmp < 0) SiftDown(id);      else if (cmp > 0) SiftUp(id);    }    public void Push(T v)    {      if (Count >= heap.Length) Array.Resize(ref heap, Count * 2);      if (hasKey) keys[kver.GetKey(v)] = Count;      heap[Count] = v;      SiftUp(Count++);    }    public T Pop()    {      var v = Top();      if (hasKey) keys.Remove(kver.GetKey(v));      heap[0] = heap[--Count];      if (Count > 0) SiftDown(hasKey ? (keys[kver.GetKey(heap[0])] = 0) : 0);      return v;    }    public T Top()    {      if (Count > 0) return heap[0];      throw new InvalidOperationException("優先隊列為空");    }    void SiftUp(int n)    {      var v = heap[n];      for (var n2 = n / 2; n > 0 && comparer.Compare(v, heap[n2]) > 0; n = n2, n2 /= 2)        heap[hasKey ? (keys[kver.GetKey(heap[n2])] = n) : n] = heap[n2];      heap[hasKey ? (keys[kver.GetKey(v)] = n) : n] = v;    }    void SiftDown(int n)    {      var v = heap[n];      for (var n2 = n * 2; n2 < Count; n = n2, n2 *= 2)      {        if (n2 + 1 < Count && comparer.Compare(heap[n2 + 1], heap[n2]) > 0) n2++;        if (comparer.Compare(v, heap[n2]) >= 0) break;        heap[hasKey ? (keys[kver.GetKey(heap[n2])] = n) : n] = heap[n2];      }      heap[hasKey ? (keys[kver.GetKey(v)] = n) : n] = v;    }  }}

上述 KeyedPriorityQueue 類中,T 是要存儲在這個帶鍵值的優先隊列中的數據的類型,K 是鍵的數據類型,V 是值的數據類型。

Dictionaryint> keys 字段用于存儲鍵(K)在堆(heap)中的索引。

bool ContainsKey(K key) 方法用于查找指定的鍵是否在該優先隊列中。

Update(T v) 方法用于更新指定項目的值。注意,如果要使用這個方法的話,T 不能是值類型。之所以在這個方法的第二行:

if (typeof(T).IsValueType) throw new InvalidOperationException("T 不能是值類型");

進行這個判斷,而不是在將該類聲明為:

class KeyedPriorityQueue where T : class { ... }

這是因為,如果不需要調用 Update(T v) 方法的話,T 還是允許是值類型的。

該類的其他方面,除了加入對 keys 字段的處理以外,就和標準的優先隊列差不多了。

有了這個 KeyedPriorityQueue 類,就可以從中派生出 PriorityQueue 類來:

class PriorityQueue : KeyedPriorityQueueobject, object>{  public PriorityQueue() : base(null) { }  public PriorityQueue(int capacity) : base(capacity, null) { }  public PriorityQueue(IComparer comparer) : base(comparer, null) { }  public PriorityQueue(int capacity, IComparer comparer) : base(capacity, comparer, null) { }}

對于 PriorityQueue 類的實例,如果調用 ContainsKey 方法,總是返回 false。如果調用 Update 方法,總是引發 NotSupportedException 異常。

現在讓我們回到上一篇隨筆 Timus 1037. Memory management 中,需要稍微修改一下我們的內存管理器程序。

首先,Block 必須從結構改為類,如下所示:

sealed class Block{  public int Id { get; private set; }  public int Time { get; set; }  public Block(int id, int time) { Id = id; Time = time; }}

然后,需要一個實現 IKeyValue 接口的類:

sealed class IdTime : IKeyValue<Block, int, int>{  public int GetKey(Block x) { return x.Id; }  public int GetValue(Block x) { return x.Time; }  public void SetValue(Block x, int v) { x.Time = v; }}

最后,就剩下最簡單的工作了,將 Main 方法的第二行:

var used = new KeyedPriorityQueue(new TimeComparer());

修改為:

var used = new KeyedPriorityQueue<Block, int, int>(new TimeComparer(), new IdTime());

就可以了。

當然,這也是有代價的,就是運行時間和內存使用都增加了:

IDDateAuthorProblemLanguageJudgement
result
Execution
time
Memory
used
256800721:22:09 18 Apr 2009skyivben1037C#Accepted0.4066 129 KB
256677309:24:17 18 Apr 2009skyivben1037C#Accepted0.2654 521 KB

上表中第二行就是上一篇隨筆中的程序提交的結果,第一行就是按照本篇隨筆修改后的程序提交的結果。

實際上,雖然 KeyedPriorityQueue 類中的代碼與 PriorityQueue 類中代碼有大量的重復,可以用本篇隨筆的方法將 KeyedPriorityQueue 類改為 KeyedPriorityQueue 泛型類,再從中派生出 PriorityQueue 類來,以消除重復的代碼。但是,這樣必然造成 PriorityQueue 類的運行效率降低。所以,一般情況下,PriorityQueue 類還是使用原來的代碼為好。

當然,如果能夠從 PriorityQueue 類派生出 KeyedPriorityQueue 類,那就比較完美了。不知各位大俠是否還有更好的方法?

NET技術用 C# 實現帶鍵值的優先隊列,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本经典片免费看 | 精品国产手机视频在在线 | 经典三级四虎在线观看 | 啊好深啊别拔就射在里面 | 久久99re2在线视频精品 | ZZoo兽2皇 | 亚洲嫩草影院久久精品 | 久久国产精品麻豆AV影视 | 和老外3p爽粗大免费视频 | 国产无线乱码一区二三区 | 午夜伦4480yy妇女久久 | 亚洲AV久久无码精品热九九 | 国产精品亚洲二线在线播放 | 成人国产亚洲精品A区天堂蜜臀 | 日本不卡不码高清免费 | 亚洲人成影院在线播放 | 乱辈通奷XXXXXHD猛交 | 亚洲国产在线2o20 | 国产普通话精品久久 | 青青草原亚洲 | 波多野结衣 熟女 | 2022一本久道久久综合狂躁 | 国产精品悠悠久久人妻精品 | 亚洲粉嫩美白在线 | 97色伦在色在线播放 | 肉动漫无修在线播放 | 99免费在线 | 99视频精品全部免费观看 | 99精品视频在线免费观看 | 乱xxxjapanese黑人| 午夜DY888国产精品影院 | 亚洲色图另类小说 | 日产久久视频 | 后入式啪gif动态图 后入式狂顶免费视频 | 在线看片成人免费视频 | 影音先锋 av天堂 | 双手绑在床头调教乳尖 | 亚洲色欲色欲WWW在线丝 | 狠狠爱亚洲五月婷婷av | 免费看的一级毛片 | 国内精品乱码卡一卡2卡三卡 |