using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CollectionDemo
{
class Program
{
static void Main(string[] args)
{
BitArray bits1 = new BitArray(8);
bits1.SetAll(true);
}
}
public class Document
{
public string Title { get; private set; }
public string Content { get; private set; }
public byte Priority { get; private set; }
public Document(string title, string content, byte priority = 0)
{
this.Title = title;
this.Content = content;
this.Priority = priority;
}
}
public class PriorityDocumentManager
{
private readonly LinkedList<Document> documentList;
private readonly List<LinkedListNode<Document>> priorityNodes;//最多适合10个 LinkedListNode
public PriorityDocumentManager()
{
documentList = new System.Collections.Generic.LinkedList<Document>();
priorityNodes = new System.Collections.Generic.List<System.Collections.Generic.LinkedListNode<Document>>(10);
for (int i = 0; i < 10; i++)
{
priorityNodes.Add(new LinkedListNode<Document>(null));
}
}
private void AddDocumentToPriorityNode(Document doc, int priority)
{
if (priority > 9 || priority < 0)
{
throw new ArgumentException("Priority must be be between 0 and 9");
}
if (priorityNodes[priority].Value == null)
{
--priority;
if (priority >= 0)
{
AddDocumentToPriorityNode(doc, priority);
}
else
{
documentList.AddLast(doc);
priorityNodes[doc.Priority] = documentList.Last;
}
}
else
{
LinkedListNode<Document> prioNode = priorityNodes[priority];
if (doc.Priority == priority)
{
documentList.AddAfter(prioNode, doc);
priorityNodes[priority] = prioNode.Next;
}
else
{
LinkedListNode<Document> firstPrioNode = prioNode;
while (firstPrioNode.Previous != null && firstPrioNode.Previous.Value.Priority == prioNode.Value.Priority)
{
firstPrioNode = firstPrioNode.Previous;
prioNode = firstPrioNode;
}
documentList.AddBefore(firstPrioNode,doc);
priorityNodes[doc.Priority] = firstPrioNode.Previous;
}
}
}
}
}