Thứ Ba, 12 tháng 2, 2008

Làm thế nào để tạo một Block Iterator cho một danh sách Chung

Trong ví dụ này là lớp chung Stack thực hiện các giao diện chung IEnumerator. Một mảng của kiểu T được khai báo và được giao các giá trị bằng cách sử dụng các method Push. Trong phương thức GetEnumerator, các giá trị của mảng được trả về bằng cách sử dụng các báo cáo về năng suất.

Các GetEnumerator không chung chung cũng được triển khai thực hiện, bởi vì kế thừa IEnumerable từ IEnumerable. Ví dụ này cho thấy việc thực hiện điển hình, trong đó các method chung không chỉ chuyển tiếp cuộc gọi đến các method chung.

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

namespace GenericIteratorExample
{
       public class Stack : IEnumerable
       {
               private T[] values = new T[100];
               private int top = 0;

               public void Push(T t) { values[top++] = t; }
               public T Pop() { return values[--top]; }

               // These make Stack implement IEnumerable allowing
               // a stack to be used in a foreach statement.
               public IEnumerator GetEnumerator()
               {
                      for (int i = top - 1; i >= 0; i-- )
                      {
                            yield return values[i];
                      }
               }

               IEnumerator IEnumerable.GetEnumerator()
               {
                       return GetEnumerator();
               }

              // Iterate from top to bottom.
              public IEnumerable TopToBottom
              {
                    get
                    {
                           // Since we implement IEnumerable
                           // and the default iteration is top to bottom,
                           // just return the object.
                           return this;
                    }
              }

              // Iterate from bottom to top.
              public IEnumerable BottomToTop
              {
                     get
                     {
                            for (int i = 0; i < top; i++) { 
                                   yield return values[i]; 
                            } 
                     } 
               } 
              // A parameterized iterator that return n items from the top 
              public IEnumerable TopN(int n) { 
                      // in this example we return less than N if necessary                
                      int j = n >= top ? 0 : top - n;

                      for (int i = top; --i >= j; )
                      {
                              yield return values[i];
                      }
              }
      }

// This code uses a stack and the TopToBottom and BottomToTop properties
// to enumerate the elements of the stack.
class Test
{
       static void Main()
       {
              Stack s = new Stack();
              for (int i = 0; i < 10; i++) { s.Push(i); } 
              // Prints: 9 8 7 6 5 4 3 2 1 0 
              // Foreach legal since s implements IEnumerable 
              foreach (int n in s) { 
                    System.Console.Write("{0} ", n); 
              } 
              System.Console.WriteLine(); 
              // Prints: 9 8 7 6 5 4 3 2 1 0 
              // Foreach legal since 
              s.TopToBottom returns IEnumerable
              foreach (int n in s.TopToBottom)
              {
                     System.Console.Write("{0} ", n);
              }
              System.Console.WriteLine();

             // Prints: 0 1 2 3 4 5 6 7 8 9
             // Foreach legal since s.BottomToTop returns IEnumerable
             foreach (int n in s.BottomToTop)
             {
                   System.Console.Write("{0} ", n);
             }
             System.Console.WriteLine();
             // Prints: 9 8 7 6 5 4 3
             // Foreach legal since s.TopN returns IEnumerable
             foreach (int n in s.TopN(7))
            {
                  System.Console.Write("{0} ", n);
            }
            System.Console.WriteLine();

           // Keep the console window open in debug mode.
           System.Console.WriteLine("Press any key to exit.");
           System.Console.ReadKey();
     }
}
}
/* Output:
9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3
*/



DangTrung.

Không có nhận xét nào:

Đăng nhận xét