-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathArrPtr.cs
More file actions
107 lines (95 loc) · 2.66 KB
/
ArrPtr.cs
File metadata and controls
107 lines (95 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//-----------------------------------------------------------------------------
// Filename: ArrPtr.cs
//
// Description: This class can be used where the common C pattern of using
// a pointer to an array is employed. For example:
//
// MODE_INFO *mip; /* Base of allocated array */
// MODE_INFO* mi; /* Corresponds to upper left visible macroblock */
//
// The mip variable is either an actual array or as in the case above a pointer
// to a dynamically allocated of items, e.g.:
//
// mip = calloc(100, sizeof(MODE_INFO));
//
// The mi pointer is then typically set to a location within mip, e.g.:
//
// mi = mip + 10;
//
// The same approach can be used in C# IF the array is pinned with "fixed".
// If the array and pointer are properties of a class and passed as parameters
// keeping track of the pins becomes unweildy.
//
// This class sovles that problem by replacing the mi pointer with an index into
// the mip managed array. Any pointer arithmetic that would have been carried out
// on mi gets applied to the index instead.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 03 Nov 2020 Aaron Clauson Created, Dublin, Ireland.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
namespace Vpx.Net
{
public struct ArrPtr<T>
{
private T[] _src { get; }
public int Index { get; private set; }
public ArrPtr(T[] src, int index = 0)
{
_src = src;
Index = index;
}
public ref T get()
{
return ref _src[Index];
}
public T get(int i)
{
return _src[Index + i];
}
public void set(T val)
{
_src[Index] = val;
}
public void set(int index, T val)
{
_src[Index + index] = val;
}
public void setMultiple(T val, int count)
{
for (int i = 0; i < count; i++)
{
_src[Index + i] = val;
}
}
public T[] src()
{
return _src;
}
public static ArrPtr<T> operator ++(ArrPtr<T> x)
{
x.Index++;
return x;
}
public static ArrPtr<T> operator --(ArrPtr<T> x)
{
x.Index--;
return x;
}
public static ArrPtr<T> operator +(ArrPtr<T> x, int i)
{
x.Index += i;
return x;
}
public static ArrPtr<T> operator -(ArrPtr<T> x, int i)
{
x.Index -= i;
return x;
}
}
}