1
2
3
4
5
6 namespace System
7 {
8 public class RandomAccessIter<T, R, P>
9 {
10 public typedef T ValueType;
11 public typedef R ReferenceType;
12 public typedef P PointerType;
13 private typedef RandomAccessIter<ValueType, ReferenceType, PointerType> Self;
14
15 public inline nothrow RandomAccessIter() : ptr(null)
16 {
17 }
18 public inline explicit nothrow RandomAccessIter(PointerType ptr_) : ptr(ptr_)
19 {
20 }
21 public inline Self& operator++()
22 {
23 if (ptr == null)
24 {
25 ThrowNullPointerException();
26 }
27 ++ptr;
28 return *this;
29 }
30 public inline Self& operator--()
31 {
32 if (ptr == null)
33 {
34 ThrowNullPointerException();
35 }
36 --ptr;
37 return *this;
38 }
39 public inline ReferenceType operator*()
40 {
41 if (ptr == null)
42 {
43 ThrowNullPointerException();
44 }
45 return *ptr;
46 }
47 public inline PointerType operator->()
48 {
49 if (ptr == null)
50 {
51 ThrowNullPointerException();
52 }
53 return ptr;
54 }
55 public inline ReferenceType operator[](long index)
56 {
57 if (ptr == null)
58 {
59 ThrowNullPointerException();
60 }
61 return ptr[index];
62 }
63 public inline nothrow PointerType Ptr() const
64 {
65 return ptr;
66 }
67 private PointerType ptr;
68 }
69
70 public inline RandomAccessIter<T, R, P> operator+<T, R, P>(const RandomAccessIter<T, R, P>& it, long offset)
71 {
72 if (it.Ptr() == null)
73 {
74 ThrowPreconditionViolationException();
75 }
76 return RandomAccessIter<T, R, P>(it.Ptr() + offset);
77 }
78
79 public inline RandomAccessIter<T, R, P> operator+<T, R, P>(long offset, const RandomAccessIter<T, R, P>& it)
80 {
81 if (it.Ptr() == null)
82 {
83 ThrowPreconditionViolationException();
84 }
85 return RandomAccessIter<T, R, P>(it.Ptr() + offset);
86 }
87
88 public inline RandomAccessIter<T, R, P> operator-<T, R, P>(const RandomAccessIter<T, R, P>& it, long offset)
89 {
90 if (it.Ptr() == null)
91 {
92 ThrowPreconditionViolationException();
93 }
94 return RandomAccessIter<T, R, P>(it.Ptr() - offset);
95 }
96
97 public inline long operator-<T, R, P>(const RandomAccessIter<T, R, P>& left, const RandomAccessIter<T, R, P>& right)
98 {
99 if (!(left.Ptr() == null && right.Ptr() == null || left.Ptr() != null && right.Ptr() != null))
100 {
101 ThrowPreconditionViolationException();
102 }
103 if (left.Ptr() == null && right.Ptr() == null)
104 {
105 return 0;
106 }
107 return left.Ptr() - right.Ptr();
108 }
109
110 public inline nothrow bool operator==<T, R, P>(const RandomAccessIter<T, R, P>& left, const RandomAccessIter<T, R, P>& right)
111 {
112 return left.Ptr() == right.Ptr();
113 }
114
115 public inline bool operator<<T, R, P>(const RandomAccessIter<T, R, P>& left, const RandomAccessIter<T, R, P>& right)
116 {
117 if (!(left.Ptr() == null && right.Ptr() == null || left.Ptr() != null && right.Ptr() != null))
118 {
119 ThrowPreconditionViolationException();
120 }
121 return left.Ptr() < right.Ptr();
122 }
123 }