中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

用 C# 實(shí)現(xiàn)帶鍵值的優(yōu)先隊(duì)列

在上一篇隨筆 Timus 1037. Memory management 的“進(jìn)一步的討論”小節(jié)中,我提到:

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

現(xiàn)在,就開始行動(dòng)吧。

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

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

接著,就是我們的帶鍵值的優(yōu)先隊(duì)列 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, "更新優(yōu)先隊(duì)列時(shí)無此健值");      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("優(yōu)先隊(duì)列為空");    }    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 是要存儲在這個(gè)帶鍵值的優(yōu)先隊(duì)列中的數(shù)據(jù)的類型,K 是鍵的數(shù)據(jù)類型,V 是值的數(shù)據(jù)類型。

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

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

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

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

進(jìn)行這個(gè)判斷,而不是在將該類聲明為:

class KeyedPriorityQueue where T : class { ... }

這是因?yàn)椋绻恍枰{(diào)用 Update(T v) 方法的話,T 還是允許是值類型的。

該類的其他方面,除了加入對 keys 字段的處理以外,就和標(biāo)準(zhǔn)的優(yōu)先隊(duì)列差不多了。

有了這個(gè) 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 類的實(shí)例,如果調(diào)用 ContainsKey 方法,總是返回 false。如果調(diào)用 Update 方法,總是引發(fā) NotSupportedException 異常。

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

首先,Block 必須從結(jié)構(gòu)改為類,如下所示:

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

然后,需要一個(gè)實(shí)現(xiàn) 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());

就可以了。

當(dāng)然,這也是有代價(jià)的,就是運(yùn)行時(shí)間和內(nèi)存使用都增加了:

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

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

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

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

NET技術(shù)用 C# 實(shí)現(xiàn)帶鍵值的優(yōu)先隊(duì)列,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 黄网址在线观看 | 国产精品国产自产拍高清 | 久久精品视频一区二区 | 亚洲一区二区在线 | 亚洲最大成人综合 | 日韩av三区 | 福利精品在线观看 | 在线日韩欧美 | 国产一区二区在线免费观看 | 一级毛片在线播放 | 成人在线 | 久久久久久久久国产精品 | 久久一二区 | 国产在线观看一区 | 亚洲乱码国产乱码精品精的特点 | 欧美精品一区二区在线观看 | 久久国产精品亚洲 | 亚洲 中文 欧美 日韩 在线观看 | 久久精品国产久精国产 | 成人一级毛片 | 精品一区二区三区入口 | 亚洲精品久久国产高清情趣图文 | 国产精品激情在线 | 亚洲美女视频 | 国产精品久久久久国产a级 欧美日韩国产免费 | 国产精品高潮呻吟久久 | 国产黄色精品 | 琪琪午夜伦伦电影福利片 | 二区在线视频 | 99在线免费观看视频 | 精品视频久久久 | 99这里只有精品视频 | 九九九久久国产免费 | 国产精品一区二区无线 | 国产观看 | 久久在线精品 | 91久久国产综合久久 | 免费久久精品视频 | 成人精品福利 | 人碰人操| 欧美激情一区二区 |