Implement some CScript functionality
[NovacoinLibrary.git] / Novacoin / WrappedList.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Novacoin
8 {
9     public class WrappedListException : Exception
10     {
11         public WrappedListException()
12         {
13         }
14
15         public WrappedListException(string message)
16             : base(message)
17         {
18         }
19
20         public WrappedListException(string message, Exception inner)
21             : base(message, inner)
22         {
23         }
24     }
25
26     public class WrappedList<T>
27     {
28         private int Index;
29         private List<T> Elements;
30
31         public WrappedList(IList<T> List, int Start)
32         {
33             Elements = new List<T>(List);
34             Index = Start;
35         }
36
37         public WrappedList(IList<T> List)
38         {
39             Elements = new List<T>(List);
40             Index = 0;
41         }
42
43         public T GetItem()
44         {
45             if (Elements.Count <= Index)
46             {
47                 throw new WrappedListException("No elements left.");
48             }
49
50             return Elements[Index++];
51         }
52
53         public T[] GetItems(int Count)
54         {
55             if (Elements.Count - Index < Count)
56             {
57                 throw new WrappedListException("Unable to read requested amount of data.");
58             }
59
60             T[] result = Elements.Skip<T>(Index).Take<T>(Count).ToArray<T>();
61             Index += Count;
62
63             return result;
64         }
65
66         public IEnumerable<T> GetEnumerableItems(int Count)
67         {
68             if (Elements.Count - Index < Count)
69             {
70                 throw new WrappedListException("Unable to read requested amount of data.");
71             }
72
73             IEnumerable<T> result = Elements.Skip<T>(Index).Take<T>(Count);
74             Index += Count;
75
76             return result;
77         }
78     }
79 }