1 // =================================
 2 // Copyright (c) 2024 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 namespace System
 7 {
 8     public class RandomAccessIter<TRP>
 9     {
10         public typedef T ValueType;
11         public typedef R ReferenceType;
12         public typedef P PointerType;
13         private typedef RandomAccessIter<ValueTypeReferenceTypePointerType> Self;
14 
15         public inline RandomAccessIter() : ptr(null)
16         {
17         }
18         public inline explicit RandomAccessIter(PointerType ptr_) : ptr(ptr_)
19         {
20         }
21         public inline Self& operator++()
22         {
23             #assert(ptr != null);
24             ++ptr;
25             return *this;
26         }
27         public inline Self& operator--()
28         {
29             #assert(ptr != null);
30             --ptr;
31             return *this;
32         }
33         public inline ReferenceType operator*()
34         {
35             #assert(ptr != null);
36             return *ptr;
37         }
38         public inline PointerType operator->()
39         {
40             #assert(ptr != null);
41             return ptr;
42         }
43         public inline ReferenceType operator[](long index)
44         {
45             #assert(ptr != null);
46             return ptr[index];
47         }
48         public inline PointerType Ptr() const
49         {
50             return ptr;
51         }
52         private PointerType ptr;
53     }
54 
55     public inline RandomAccessIter<TRP> operator+<TRP>(const RandomAccessIter<TRP>& itlong offset)
56     {
57         #assert(it.Ptr() != null);
58         return RandomAccessIter<TRP>(it.Ptr() + offset);
59     }
60 
61     public inline RandomAccessIter<TRP> operator+<TRP>(long offsetconst RandomAccessIter<TRP>& it)
62     {
63         #assert(it.Ptr() != null);
64         return RandomAccessIter<TRP>(it.Ptr() + offset);
65     }
66 
67     public inline RandomAccessIter<TRP> operator-<TRP>(const RandomAccessIter<TRP>& itlong offset)
68     {
69         #assert(it.Ptr() != null);
70         return RandomAccessIter<TRP>(it.Ptr() - offset);
71     }
72 
73     public inline long operator-<TRP>(const RandomAccessIter<TRP>& leftconst RandomAccessIter<TRP>& right)
74     {
75         #assert((left.Ptr() == null && right.Ptr() == null || left.Ptr() != null && right.Ptr() != null));
76         if (left.Ptr() == null && right.Ptr() == null)
77         {
78             return 0;
79         }
80         return left.Ptr() - right.Ptr();
81     }
82 
83     public inline bool operator==<TRP>(const RandomAccessIter<TRP>& leftconst RandomAccessIter<TRP>& right)
84     {
85         return left.Ptr() == right.Ptr();
86     }
87 
88     public inline bool operator<<TRP>(const RandomAccessIter<TRP>& leftconst RandomAccessIter<TRP>& right)
89     {
90         #assert((left.Ptr() == null && right.Ptr() == null || left.Ptr() != null && right.Ptr() != null));
91         return left.Ptr() < right.Ptr();
92     }