html分页

1.CSS

BitArraySample

图片 1图片 2

图片 3图片 4

 1 /*
 2  * 页数按钮样式
 3  */
 4 .page_div span:nth-of-type(2){
 5     float: right;    
 6 }
 7 .page_div a:last-child{
 8     margin-right: 0;
 9 }
10 .page{
11     padding-right: 21px;
12 }
13 .page_div a {
14     min-width: 30px;
15     height: 28px;
16     border: 1px solid #a6acb7;
17     text-align: center;
18     margin: 0 3px;
19     cursor: pointer;
20     line-height: 28px;
21     color: #000;
22     font-size: 13px;
23     display: inline-block;
24     background: #fff;
25 }
26 
27 .page_div .current {
28     color: #FFFFFF;
29     border: none !important;
30     background-color: #44884f;
31 
32 }
33 .page_div .current:hover{
34 color: #FFFFFF;
35     border: none !important;
36     background-color: #44884f;
37 }
38 .totalPages {
39     margin: 0 10px;
40 }
41 
42 .totalPages span,
43 .totalSize span {
44     color: #0073A9;
45     margin: 0 5px;
46 }
47 
48 /*end分页引用外部样式*/
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;

namespace BitArraySample
{
  class Program
  {
    static void BitArrayDemo()
    {
      var bits1 = new BitArray(8);
      bits1.SetAll(true);
      bits1.Set(1, false);
      bits1[5] = false;
      bits1[7] = false;
      Console.Write("initialized: ");
      DisplayBits(bits1);
      Console.WriteLine();


      DisplayBits(bits1);
      bits1.Not();
      Console.Write(" not ");
      DisplayBits(bits1);
      Console.WriteLine();

      var bits2 = new BitArray(bits1);
      bits2[0] = true;
      bits2[1] = false;
      bits2[4] = true;
      DisplayBits(bits1);
      Console.Write(" or ");
      DisplayBits(bits2);
      Console.Write(" : ");
      bits1.Or(bits2);
      DisplayBits(bits1);
      Console.WriteLine();


      DisplayBits(bits2);
      Console.Write(" and ");
      DisplayBits(bits1);
      Console.Write(" : ");
      bits2.And(bits1);
      DisplayBits(bits2);
      Console.WriteLine();

      DisplayBits(bits1);
      Console.Write(" xor ");
      DisplayBits(bits2);
      bits1.Xor(bits2);
      Console.Write(" : ");
      DisplayBits(bits1);
      Console.WriteLine();
    }

    static void BitVectorDemo()
    {

      var bits1 = new BitVector32();
      int bit1 = BitVector32.CreateMask();
      int bit2 = BitVector32.CreateMask(bit1);
      int bit3 = BitVector32.CreateMask(bit2);
      int bit4 = BitVector32.CreateMask(bit3);
      int bit5 = BitVector32.CreateMask(bit4);

      bits1[bit1] = true;
      bits1[bit2] = false;
      bits1[bit3] = true;
      bits1[bit4] = true;
      Console.WriteLine(bits1);

      bits1[0xabcdef] = true;
      Console.WriteLine(bits1);


      int received = 0x79abcdef;

      var bits2 = new BitVector32(received);
      Console.WriteLine(bits2);
      // sections: FF EEE DDD CCCC BBBBBBBB AAAAAAAAAAAA
      BitVector32.Section sectionA = BitVector32.CreateSection(0xfff);
      BitVector32.Section sectionB = BitVector32.CreateSection(0xff, sectionA);
      BitVector32.Section sectionC = BitVector32.CreateSection(0xf, sectionB);
      BitVector32.Section sectionD = BitVector32.CreateSection(0x7, sectionC);
      BitVector32.Section sectionE = BitVector32.CreateSection(0x7, sectionD);
      BitVector32.Section sectionF = BitVector32.CreateSection(0x3, sectionE);



      Console.WriteLine("Section A: "   IntToBinaryString(bits2[sectionA], true));
      Console.WriteLine("Section B: "   IntToBinaryString(bits2[sectionB], true));
      Console.WriteLine("Section C: "   IntToBinaryString(bits2[sectionC], true));
      Console.WriteLine("Section D: "   IntToBinaryString(bits2[sectionD], true));
      Console.WriteLine("Section E: "   IntToBinaryString(bits2[sectionE], true));
      Console.WriteLine("Section F: "   IntToBinaryString(bits2[sectionF], true));


    }

    static string IntToBinaryString(int bits, bool removeTrailingZero)
    {
      var sb = new StringBuilder(32);

      for (int i = 0; i < 32; i  )
      {
        if ((bits & 0x80000000) != 0)
        {
          sb.Append("1");
        }
        else
        {
          sb.Append("0");
        }
        bits = bits << 1;
      }
      string s = sb.ToString();
      if (removeTrailingZero)
        return s.TrimStart('0');
      else
        return s;
    }

    static void Main()
    {
      // BitArrayDemo();
      BitVectorDemo();
    }


    static void DisplayBits(BitArray bits)
    {
      foreach (bool bit in bits)
      {
        Console.Write(bit ? 1 : 0);
      }
    }
  }
}

View Code

View Code

2.HTML

ConcurrentSample

1 <div class="p_pager">
2                                 <p class="page_div" id="page">
3                                 
4                                 </p>
5                             </div>

图片 5图片 6

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wrox.ProCSharp.Collections
{
  public class Info
  {
    public string Word { get; set; }
    public int Count { get; set; }
    public string Color { get; set; }

    public override string ToString()
    {
      return String.Format("{0} x: {1}", Count, Word);
    }
  }
}

3.JS

View Code

图片 7图片 8

图片 9图片 10

  1 (function($, window, document, undefined) {
  2     //定义分页类
  3     function Paging(element, options) {
  4         this.element = element;
  5         //传入形参
  6         this.options = {
  7             pageNo: options.pageNo||1,
  8             totalPage: options.totalPage,
  9             totalSize:options.totalSize,
 10             callback:options.callback
 11         };
 12         //根据形参初始化分页html和css代码
 13         this.init();
 14     }
 15     //对Paging的实例对象添加公共的属性和方法
 16     Paging.prototype = {
 17         constructor: Paging,
 18         init: function() {
 19             this.creatHtml();
 20             this.bindEvent();
 21             this.pageBtnHover();
 22         },
 23         //分页翻页按钮hover效果
 24         pageBtnHover: function () {
 25             $("#nextPage")
 26                 .on("mouseout",
 27                 function () {
 28                     $(this).find("img").attr("src", "/img/rightButtonPage.png");
 29                 });
 30             $("#prePage")
 31                 .on("mouseout",
 32                 function () {
 33                     $(this).find("img").attr("src", "/img/leftButtonPage.png");
 34                 });
 35             $("#nextPage")
 36                 .on("mouseover",
 37                 function () {
 38                     $(this).find("img").attr("src", "/img/pa_right_per.png");
 39                 });
 40             $("#prePage")
 41                 .on("mouseover",
 42                 function () {
 43                     $(this).find("img").attr("src", "/img/pa_left_per.png");
 44                 });
 45         },
 46         creatHtml: function () {
 47              
 48             var me = this;
 49             var content = "";
 50             var current = me.options.pageNo;
 51             var total = me.options.totalPage;
 52             var totalNum = me.options.totalSize;
 53             content  = "显示&nbsp;<select id='selectPage'><option>10</option><option>25</option><option>50</option><option>100</option></select>&nbsp;项结果显示第1至"   totalNum "项结果,共" total "页";
 54             content  = "<a id='prePage'><img src='/img/leftButtonPage.png'></a>";
 55 
 56             //总页数大于6时候
 57             if(total > 6) {
 58                 //当前页数小于5时显示省略号
 59                 if(current < 5) {
 60                     for(var i = 1; i < 6; i  ) {
 61                         if(current == i) {
 62                             content  = "<a class='current'>"   i   "</a>";
 63                         } else {
 64                             content  = "<a>"   i   "</a>";
 65                         }
 66                     }
 67                     content  = ". . .";
 68                     content  = "<a>" total "</a>";
 69                 } else {
 70                      //判断页码在末尾的时候
 71                     if(current < total - 3) {
 72                         for(var i = current - 2; i < current   3; i  ) {
 73                             if(current == i) {
 74                                 content  = "<a class='current'>"   i   "</a>";
 75                             } else {
 76                                 content  = "<a>"   i   "</a>";
 77                             }
 78                         }
 79                         content  = ". . .";
 80                         content  = "<a>" total "</a>";
 81                     //页码在中间部分时候    
 82                     } else {
 83                         content  = "<a>1</a>";
 84                         content  = ". . .";
 85                         for(var i = total - 4; i < total   1; i  ) {
 86                             if(current == i) {
 87                                 content  = "<a class='current'>"   i   "</a>";
 88                             } else {
 89                                 content  = "<a>"   i   "</a>";
 90                             }
 91                         }
 92                     }
 93                 }
 94                 //页面总数小于6的时候
 95             } else {
 96                 for(var i = 1; i < total   1; i  ) {
 97                     if(current == i) {
 98                         content  = "<a class='current'>"   i   "</a>";
 99                     } else {
100                         content  = "<a>"   i   "</a>";
101                     }
102                 }
103             }
104             content  = "<a id='nextPage'><img src='/img/rightButtonPage.png'></a>";    
105             me.element.html(content);
106         },
107         //添加页面操作事件
108         bindEvent: function() {
109             var me = this;
110             me.element.off('click', 'a');
111             me.element.on('click', 'a', function() {
112                 var num = $(this).html();
113                 var id=$(this).attr("id");
114                 if(id == "prePage") {
115                     if(me.options.pageNo == 1) {
116                         me.options.pageNo = 1;
117                     } else {
118                         me.options.pageNo =  me.options.pageNo - 1;
119                     }
120                 } else if(id == "nextPage") {
121                     if(me.options.pageNo == me.options.totalPage) {
122                         me.options.pageNo = me.options.totalPage
123                     } else {
124                         me.options.pageNo =  me.options.pageNo   1;
125                     }
126 
127                 } else if(id =="lastPage") {
128                     me.options.pageNo = me.options.totalPage;
129                 }else{
130                     me.options.pageNo =  num;
131                 }
132                 me.creatHtml();
133                 if(me.options.callback) {
134                     me.options.callback(me.options.pageNo);
135                 }
136             });
137         }
138     };
139     //通过jQuery对象初始化分页对象
140     $.fn.paging = function(options) {
141         return new Paging($(this), options);
142     }
143 })(jQuery, window, document);
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Wrox.ProCSharp.Collections
{
  class Program
  {
    static void Main()
    {
      // BlockingDemo();

      //BlockingDemoSimple();

      PipelineSample();

      Console.ReadLine();

    }

    private static void ReadFileNames(string path, BlockingCollection<string> output)
    {
      foreach (string filename in Directory.EnumerateFiles(path, "*.cs"))
      {
        output.Add(filename);
      }
      output.CompleteAdding();
    }

    private static void AddOrIncrementValue(string key, ConcurrentDictionary<string, int> dict)
    {
      bool success = false;
      while (!success)
      {
        int value;
        if (dict.TryGetValue(key, out value))
        {
          if (dict.TryUpdate(key, value   1, value))
          {
            success = true;
          }
        }
        else
        {
          if (dict.TryAdd(key, 1))
          {
            success = true;
          }
        }
      }
    }



    private static async void LoadContent(BlockingCollection<string> input, ConcurrentDictionary<string, int> output)
    {
      foreach (var filename in input.GetConsumingEnumerable())
      {
        using (FileStream stream = File.OpenRead(filename))
        {
          var reader = new StreamReader(stream);
          string line = await reader.ReadLineAsync();
          string[] words = line.Split(' ', ';', 't');
          foreach (var word in words)
          {
            AddOrIncrementValue(word, output);
          }
        }
      }
    }

    private static void TransferContent(ConcurrentDictionary<string, int> input, BlockingCollection<Info> output)
    {
      foreach (var word in input.Keys)
        {
        int value;
        if (input.TryGetValue(word, out value))
        {
              output.Add(new Info { Word = word, Count = value });
        }
        }
    }
    private static void ShowContent(BlockingCollection<Info> input)
    {
      foreach (var item in input.GetConsumingEnumerable())
      {
        Console.WriteLine(item);
      }
    }

    private static async void PipelineSample()
    {
      BlockingCollection<string> coll1 = new BlockingCollection<string>();
      ConcurrentDictionary<string, int> coll2 = new ConcurrentDictionary<string,int>();
      BlockingCollection<Info> coll3 = new BlockingCollection<Info>();
      Task t1 = Task.Factory.StartNew(() => ReadFileNames(@"C:tempMvcApplication1MvcApplication1Controllers", coll1), TaskCreationOptions.LongRunning);
      Console.WriteLine("started stage 1");
      Task t2 = Task.Factory.StartNew(() => LoadContent(coll1, coll2), TaskCreationOptions.LongRunning);
      Console.WriteLine("started stage 2");
      await Task.WhenAll(t1, t2);
      Console.WriteLine("stage 1 and 2 completed");
      Task t3 = Task.Factory.StartNew(() => TransferContent(coll2, coll3), TaskCreationOptions.LongRunning);
      Task t4 = Task.Factory.StartNew(() => ShowContent(coll3), TaskCreationOptions.LongRunning);
      Console.WriteLine("stages 3 and 4 started");
      await Task.WhenAll(t3, t4);
      Console.WriteLine("all finished");


    }

    static void BlockingDemoSimple()
    {
      var sharedCollection = new BlockingCollection<int>();
      var events = new ManualResetEventSlim[2];
      var waits = new WaitHandle[2];
      for (int i = 0; i < 2; i  )
      {
        events[i] = new ManualResetEventSlim(false);
        waits[i] = events[i].WaitHandle;
      }

      var producer = new Thread(obj =>
      {
        var state = (Tuple<BlockingCollection<int>, ManualResetEventSlim>)obj;
        var coll = state.Item1;
        var ev = state.Item2;
        var r = new Random();

        for (int i = 0; i < 300; i  )
        {
          coll.Add(r.Next(3000));
        }
        ev.Set();
      });
      producer.Start(Tuple.Create<BlockingCollection<int>, ManualResetEventSlim>(sharedCollection, events[0]));

      var consumer = new Thread(obj =>
      {
        var state = (Tuple<BlockingCollection<int>, ManualResetEventSlim>)obj;
        var coll = state.Item1;
        var ev = state.Item2;

        for (int i = 0; i < 300; i  )
        {
          int result = coll.Take();
        }
        ev.Set();
      });
      consumer.Start(Tuple.Create<BlockingCollection<int>, ManualResetEventSlim>(sharedCollection, events[1]));

      if (!WaitHandle.WaitAll(waits))
        Console.WriteLine("wait failed");
      else
        Console.WriteLine("reading/writing finished");

    }

    static async void BlockingDemo()
    {
      const int taskCount = 10;
      ManualResetEventSlim[] events = new ManualResetEventSlim[taskCount];
      WaitHandle[] waits = new WaitHandle[taskCount];
      var consoleLock = new object();

      for (int task = 0; task < taskCount; task  )
      {
        events[task] = new ManualResetEventSlim(false);
        waits[task] = events[task].WaitHandle;
      }

      var sharedCollection = new BlockingCollection<int>();


      for (int task = 0; task < taskCount >> 1; task  )
      {

        var producer = new Task((state) =>
        {
          var coll = ((Tuple<BlockingCollection<int>, ManualResetEventSlim>)state).Item1;
          var wait = ((Tuple<BlockingCollection<int>, ManualResetEventSlim>)state).Item2;
          var r = new Random();
          for (int i = 0; i < 300; i  )
          {
            int data = r.Next(30000);
            if (!coll.TryAdd(data))
            {
              Console.WriteLine("**** couldn't add");
            }
            else
            {
              lock (consoleLock)
              {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write(" {0} ", data);
                Console.ResetColor();
              }
            }
            Thread.Sleep(r.Next(40));
          }
          wait.Set();
        }, Tuple.Create<BlockingCollection<int>, ManualResetEventSlim>(sharedCollection, events[task]));

        producer.Start();
//        producer.Start(Tuple.Create<BlockingCollection<int>, ManualResetEventSlim>(sharedCollection, events[task]));
      }

      await Task.Delay(500); // give the producers a headstart


      for (int task = taskCount >> 1; task < taskCount; task  )
      {
        var consumer = new Task((state) =>
        {
          var coll = ((Tuple<BlockingCollection<int>, ManualResetEventSlim>)state).Item1;
          var wait = ((Tuple<BlockingCollection<int>, ManualResetEventSlim>)state).Item2;
          var r = new Random();
          for (int i = 0; i < 3000; i  )
          {
            int result;
            if (!coll.TryTake(out result))
            {
              Console.WriteLine("couldn't take");
            }
            else
            {
              lock (consoleLock)
              {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(" {0} ", result);
                Console.ResetColor();
              }
            }

            Thread.Sleep(r.Next(40));
          }
          wait.Set();
        }, Tuple.Create<BlockingCollection<int>, ManualResetEventSlim>(sharedCollection, events[task]));
        consumer.Start();
      }

      if (!WaitHandle.WaitAll(waits))
        Console.WriteLine("error waiting...");

    }
  }
}

View Code

View Code

4.调用

DictionarySample

图片 11图片 12

图片 13图片 14

 1 (function ($, window, document, undefined) {
 2     $.extend({
 3         pageTest: function (options) {
 4             var settings = {
 5                 row: 10,
 6             };
 7             getData();
 8             function getData() {
 9                 //调用接口获取数据
10                 //....
11                 //显示分页
12                 showPage();
13             }
14            
15             function showPage(pageNo, total) {
16                 var totalPage = Math.ceil(total / settings.row);
17                 $("#page").paging({
18                     pageNo: pageNo,
19                     totalPage: totalPage,
20                     totalSize: total,
21                     callback: function (num) {
22                         settings.page = num;
23                         //调用接口获取数据
24                         getData();
25                     }
26                 });
27                 $("#selectPage").val(settings.row);
28             }
29         }
30     });
31   
32 })(jQuery, window, document);
using System;
using System.Collections.Generic;

namespace Wrox.ProCSharp.Collections
{
  class Program
  {
    static void Main()
    {
      var employees = new Dictionary<EmployeeId, Employee>(31);

      var idTony = new EmployeeId("C3755");
      var tony = new Employee(idTony, "Tony Stewart", 379025.00m);
      employees.Add(idTony, tony);
      Console.WriteLine(tony);

      var idCarl = new EmployeeId("F3547");
      var carl = new Employee(idCarl, "Carl Edwards", 403466.00m);
      employees.Add(idCarl, carl);
      Console.WriteLine(carl);

      var idKevin = new EmployeeId("C3386");
      var kevin = new Employee(idKevin, "Kevin Harwick", 415261.00m);
      employees.Add(idKevin, kevin);
      Console.WriteLine(kevin);

      var idMatt = new EmployeeId("F3323");
      var matt = new Employee(idMatt, "Matt Kenseth", 1589390.00m);
      employees[idMatt] = matt;
      Console.WriteLine(matt);

      var idBrad = new EmployeeId("D3234");
      var brad = new Employee(idBrad, "Brad Keselowski", 322295.00m);
      employees[idBrad] = brad;
      Console.WriteLine(brad);



      while (true)
      {
        Console.Write("Enter employee id (X to exit)> ");
        var userInput = Console.ReadLine();
        userInput = userInput.ToUpper();
        if (userInput == "X") break;

        EmployeeId id;
        try
        {
          id = new EmployeeId(userInput);


          Employee employee;
          if (!employees.TryGetValue(id, out employee))
          {
            Console.WriteLine("Employee with id {0} does not exist", id);
          }
          else
          {
            Console.WriteLine(employee);
          }
        }
        catch (EmployeeIdException ex)
        {
          Console.WriteLine(ex.Message);
        }
      }

    }
  }
}

View Code

View Code

 

图片 15图片 16

using System;
using System.Diagnostics.Contracts;

namespace Wrox.ProCSharp.Collections
{
  [Serializable]
  public class EmployeeIdException : Exception
  {
    public EmployeeIdException(string message) : base(message) { }
  }

  [Serializable]
  public struct EmployeeId : IEquatable<EmployeeId>
  {
    private readonly char prefix;
    private readonly int number;

    public EmployeeId(string id)
    {
      Contract.Requires<ArgumentNullException>(id != null);

      prefix = (id.ToUpper())[0];
      int numLength = id.Length - 1;
      try
      {
        number = int.Parse(id.Substring(1, numLength > 6 ? 6 : numLength));
      }
      catch (FormatException)
      {
        throw new EmployeeIdException("Invalid EmployeeId format");
      }
    }

    public override string ToString()
    {
      return prefix.ToString()   string.Format("{0,6:000000}", number);
    }

    public override int GetHashCode()
    {
      return (number ^ number << 16) * 0x15051505;
    }

    public bool Equals(EmployeeId other)
    {
      if (other == null) return false;

      return (prefix == other.prefix && number == other.number);
    }

    public override bool Equals(object obj)
    {
      return Equals((EmployeeId)obj);
    }

    public static bool operator ==(EmployeeId left, EmployeeId right)
    {
      return left.Equals(right);
    }

    public static bool operator !=(EmployeeId left, EmployeeId right)
    {
      return !(left == right);
    }
  }

}

View Code

图片 17图片 18

using System;

namespace Wrox.ProCSharp.Collections
{
  [Serializable]
  public class Employee
  {
    private string name;
    private decimal salary;
    private readonly EmployeeId id;

    public Employee(EmployeeId id, string name, decimal salary)
    {
      this.id = id;
      this.name = name;
      this.salary = salary;
    }

    public override string ToString()
    {
      return String.Format("{0}: {1, -20} {2:C}",
            id.ToString(), name, salary);
    }
  }

}

View Code

ImmutableCollectionsSample

图片 19图片 20

using System;
using System.Collections.Generic;
using System.Collections.Immutable;

namespace ImmutableCollectionsSample
{
  class Program
  {
    static void Main(string[] args)
    {
      // ArraySample();
      ListSample();
      Console.ReadKey();
    }

    private static void ListSample()
    {
      List<Account> accounts = new List<Account>() {
        new Account {
          Name = "Scrooge McDuck", 
          Amount = 667377678765m
        },
        new Account {
          Name = "Donald Duck",
          Amount = -200m
        },
       new Account {
         Name = "Ludwig von Drake",
         Amount = 20000m
        }};
      ImmutableList<Account> immutableAccounts = accounts.ToImmutableList();

      ImmutableList<Account>.Builder builder = immutableAccounts.ToBuilder();
      for (int i = 0; i < builder.Count; i  )
      {
        Account a = builder[i];
        if (a.Amount > 0)
        {
          builder.Remove(a);
        }
      }

      ImmutableList<Account> overdrawnAccounts = builder.ToImmutable();


      foreach (var item in overdrawnAccounts)
      {
        Console.WriteLine("{0} {1}", item.Name, item.Amount);
      }

    }

    private static void ArraySample()
    {
      ImmutableArray<string> a1 = ImmutableArray.Create<string>();
      ImmutableArray<string> a2 = a1.Add("Williams");
      ImmutableArray<string> a3 = a2.Add("Ferrari").Add("Mercedes").Add("Red Bull Racing");
      foreach (var item in a3)
      {
        Console.WriteLine(item);
      }

    }
  }
}

View Code

图片 21图片 22

namespace ImmutableCollectionsSample
{
  public class Account
  {
    public string Name { get; set; }
    public decimal Amount { get; set; }
  }
}

View Code

LinkedListSample

图片 23图片 24

using System;
namespace Wrox.ProCSharp.Collections
{
  class Program
  {
    static void Main()
    {
      PriorityDocumentManager pdm = new PriorityDocumentManager();
      pdm.AddDocument(new Document("one", "Sample", 8));
      pdm.AddDocument(new Document("two", "Sample", 3));
      pdm.AddDocument(new Document("three", "Sample", 4));
      pdm.AddDocument(new Document("four", "Sample", 8));
      pdm.AddDocument(new Document("five", "Sample", 1));
      pdm.AddDocument(new Document("six", "Sample", 9));
      pdm.AddDocument(new Document("seven", "Sample", 1));
      pdm.AddDocument(new Document("eight", "Sample", 1));

      pdm.DisplayAllNodes();
      Console.ReadKey();
    }
  }
}

View Code

图片 25图片 26

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace Wrox.ProCSharp.Collections
{
  public class PriorityDocumentManager
  {
    private readonly LinkedList<Document> documentList;

    // priorities 0.9
    private readonly List<LinkedListNode<Document>> priorityNodes;

    public PriorityDocumentManager()
    {
      documentList = new LinkedList<Document>();

      priorityNodes = new List<LinkedListNode<Document>>(10);
      for (int i = 0; i < 10; i  )
      {
        priorityNodes.Add(new LinkedListNode<Document>(null));
      }
    }

    public void AddDocument(Document d)
    {
      //Contract.Requires<ArgumentNullException>(d != null, "argument d must not be null");
      if (d == null) throw new ArgumentNullException("d");

      AddDocumentToPriorityNode(d, d.Priority);
    }

    private void AddDocumentToPriorityNode(Document doc, int priority)
    {
      //Contract.Requires<ArgumentException>(priority >= 0 && priority < 10, "priority value must be between 0 and 9");
      if (priority > 9 || priority < 0)
          throw new ArgumentException("Priority must be between 0 and 9");

      if (priorityNodes[priority].Value == null)
      {
        --priority;
        if (priority >= 0)
        {
          // check for the next lower priority
          AddDocumentToPriorityNode(doc, priority);
        }
        else // now no priority node exists with the same priority or lower
        // add the new document to the end
        {
          documentList.AddLast(doc);
          priorityNodes[doc.Priority] = documentList.Last;
        }
        return;
      }
      else // a priority node exists
      {
        LinkedListNode<Document> prioNode = priorityNodes[priority];
        if (priority == doc.Priority)
        // priority node with the same priority exists
        {
          documentList.AddAfter(prioNode, doc);

          // set the priority node to the last document with the same priority
          priorityNodes[doc.Priority] = prioNode.Next;
        }
        else // only priority node with a lower priority exists
        {
          // get the first node of the lower priority
          LinkedListNode<Document> firstPrioNode = prioNode;

          while (firstPrioNode.Previous != null &&
             firstPrioNode.Previous.Value.Priority == prioNode.Value.Priority)
          {
            firstPrioNode = prioNode.Previous;
            prioNode = firstPrioNode;
          }

          documentList.AddBefore(firstPrioNode, doc);

          // set the priority node to the new value
          priorityNodes[doc.Priority] = firstPrioNode.Previous;
        }
      }
    }

    public void DisplayAllNodes()
    {
      foreach (Document doc in documentList)
      {
        Console.WriteLine("priority: {0}, title {1}", doc.Priority, doc.Title);
      }
    }

    // returns the document with the highest priority
    // (that's first in the linked list)
    public Document GetDocument()
    {
      Document doc = documentList.First.Value;
      documentList.RemoveFirst();
      return doc;
    }

  }

}

View Code

图片 27图片 28

namespace Wrox.ProCSharp.Collections
{
  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)
    {
      this.Title = title;
      this.Content = content;
      this.Priority = priority;
    }
  }

}

View Code

ListSamples

图片 29图片 30

using System;
using System.Collections.Generic;

namespace Wrox.ProCSharp.Collections
{
  public enum CompareType
  {
    FirstName,
    LastName,
    Country,
    Wins
  }

  public class RacerComparer : IComparer<Racer>
  {
    private CompareType compareType;
    public RacerComparer(CompareType compareType)
    {
      this.compareType = compareType;
    }

    public int Compare(Racer x, Racer y)
    {
      if (x == null && y == null) return 0;
      if (x == null) return -1;
      if (y == null) return 1;

      int result;
      switch (compareType)
      {
        case CompareType.FirstName:
          return string.Compare(x.FirstName, y.FirstName);
        case CompareType.LastName:
          return string.Compare(x.LastName, y.LastName);
        case CompareType.Country:
          result = string.Compare(x.Country, y.Country);
          if (result == 0)
            return string.Compare(x.LastName, y.LastName);
          else
            return result;
        case CompareType.Wins:
          return x.Wins.CompareTo(y.Wins);
        default:
          throw new ArgumentException("Invalid Compare Type");
      }
    }
  }

}

View Code

图片 31图片 32

using System;

namespace Wrox.ProCSharp.Collections
{
  [Serializable]
  public class Racer : IComparable<Racer>, IFormattable
  {
    public int Id { get; private set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Country { get; set; }
    public int Wins { get; set; }

    public Racer(int id, string firstName, string lastName, string country)
      : this(id, firstName, lastName, country, wins: 0)
    {
    }
    public Racer(int id, string firstName, string lastName, string country, int wins)
    {
      this.Id = id;
      this.FirstName = firstName;
      this.LastName = lastName;
      this.Country = country;
      this.Wins = wins;
    }

    public override string ToString()
    {
      return String.Format("{0} {1}", FirstName, LastName);
    }

    public string ToString(string format, IFormatProvider formatProvider)
    {
      if (format == null) format = "N";
      switch (format.ToUpper())
      {
        case null:
        case "N": // name
          return ToString();
        case "F": // first name
          return FirstName;
        case "L": // last name
          return LastName;
        case "W": // Wins
          return String.Format("{0}, Wins: {1}", ToString(), Wins);
        case "C": // Country
          return String.Format("{0}, Country: {1}", ToString(), Country);
        case "A": // All
          return String.Format("{0}, {1} Wins: {2}", ToString(), Country, Wins);
        default:
          throw new FormatException(String.Format(formatProvider,
                "Format {0} is not supported", format));
      }
    }

    public string ToString(string format)
    {
      return ToString(format, null);
    }

    public int CompareTo(Racer other)
    {
      if (other == null) return -1;
      int compare = string.Compare(this.LastName, other.LastName);
      if (compare == 0)
        return string.Compare(this.FirstName, other.FirstName);
      return compare;
    }
  }

}

View Code

图片 33图片 34

using System.Collections.Generic;

namespace Wrox.ProCSharp.Collections
{
    class Program
    {
        static void Main()
        {
            var graham = new Racer(7, "Graham", "Hill", "UK", 14);
            var emerson = new Racer(13, "Emerson", "Fittipaldi", "Brazil", 14);
            var mario = new Racer(16, "Mario", "Andretti", "USA", 12);

            var racers = new List<Racer>(20) { graham, emerson, mario };

            racers.Add(new Racer(24, "Michael", "Schumacher", "Germany", 91));
            racers.Add(new Racer(27, "Mika", "Hakkinen", "Finland", 20));

            racers.AddRange(new Racer[] {
               new Racer(14, "Niki", "Lauda", "Austria", 25),
               new Racer(21, "Alain", "Prost", "France", 51)});

            var racers2 = new List<Racer>(new Racer[] {
               new Racer(12, "Jochen", "Rindt", "Austria", 6),
               new Racer(22, "Ayrton", "Senna", "Brazil", 41) });



        }
    }
}

View Code

LookupSample

图片 35图片 36

using System;

namespace Wrox.ProCSharp.Collections
{
  [Serializable]
  public class Racer : IComparable<Racer>, IFormattable
  {
    public int Id { get; private set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Country { get; set; }
    public int Wins { get; set; }

    public Racer(int id, string firstName, string lastName, string country = null, int wins = 0)
    {
      this.Id = id;
      this.FirstName = firstName;
      this.LastName = lastName;
      this.Country = country;
      this.Wins = wins;
    }

    public override string ToString()
    {
      return String.Format("{0} {1}", FirstName, LastName);
    }

    public string ToString(string format, IFormatProvider formatProvider)
    {
      if (format == null) format = "N";
      switch (format.ToUpper())
      {
        case "N": // name
          return ToString();
        case "F": // first name
          return FirstName;
        case "L": // last name
          return LastName;
        case "W": // Wins
          return String.Format("{0}, Wins: {1}", ToString(), Wins);
        case "C": // Country
          return String.Format("{0}, Country: {1}", ToString(), Country);
        case "A": // All
          return String.Format("{0}, {1} Wins: {2}", ToString(), Country, Wins);
        default:
          throw new FormatException(String.Format(formatProvider,
                "Format {0} is not supported", format));
      }
    }

    public string ToString(string format)
    {
      return ToString(format, null);
    }

    public int CompareTo(Racer other)
    {
      int compare = this.LastName.CompareTo(other.LastName);
      if (compare == 0)
        return this.FirstName.CompareTo(other.FirstName);
      return compare;
    }
  }

}

本文由威尼斯人棋牌发布于计算机教程,转载请注明出处:html分页