CScript: disassembly support
[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 int ItemsLeft
32         {
33             get { return Elements.Count - Index; }
34         }
35
36         public WrappedList(IList<T> List, int Start)
37         {
38             Elements = new List<T>(List);
39             Index = Start;
40         }
41
42         public WrappedList(IList<T> List)
43         {
44             Elements = new List<T>(List);
45             Index = 0;
46         }
47
48         public T GetItem()
49         {
50             if (Elements.Count <= Index)
51             {
52                 throw new WrappedListException("No elements left.");
53             }
54
55             return Elements[Index++];
56         }
57
58         public T[] GetItems(int Count)
59         {
60             if (ItemsLeft < Count)
61             {
62                 throw new WrappedListException("Unable to read requested amount of data.");
63             }
64
65             T[] result = Elements.Skip<T>(Index).Take<T>(Count).ToArray<T>();
66             Index += Count;
67
68             return result;
69         }
70
71         public IEnumerable<T> GetEnumerableItems(int Count)
72         {
73             if (Elements.Count - Index < Count)
74             {
75                 throw new WrappedListException("Unable to read requested amount of data.");
76             }
77
78             IEnumerable<T> result = Elements.Skip<T>(Index).Take<T>(Count);
79             Index += Count;
80
81             return result;
82         }
83     }
84 }