-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractBuffer.cs
175 lines (144 loc) · 5.16 KB
/
AbstractBuffer.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// This file is part of bugreport.
// Copyright (c) 2006-2009 The bugreport Developers.
// See AUTHORS.txt for details.
// Licensed under the GNU General Public License, Version 3 (GPLv3).
// See LICENSE.txt for details.
using System;
namespace bugreport
{
public sealed class AbstractBuffer
{
private AbstractValue[] storage;
public AbstractBuffer(AbstractValue[] values)
{
if (null == values)
{
throw new ArgumentNullException("values");
}
storage = values;
Length = storage.Length;
}
public AbstractBuffer(AbstractBuffer other)
{
if (null == other)
{
throw new ArgumentNullException("other");
}
BaseIndex = other.BaseIndex;
Length = other.Length;
storage = new AbstractValue[other.storage.Length];
Array.Copy(other.storage, storage, other.storage.Length);
}
private UInt32 BaseIndex { get; set; }
public int Length { get; private set; }
public AbstractValue this[Int32 index]
{
get
{
// We check this.storage.Length as well so that we aren't calling Extend() when we dont need to.
if (IsIndexPastBounds(index))
{
Extend(BaseIndex + (UInt32)index);
return storage[BaseIndex + index];
}
return storage[BaseIndex + index];
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if ((BaseIndex + index) >= Length)
{
value.IsOutOfBounds = true;
}
if (IsIndexPastBounds(index))
{
Extend(BaseIndex + (UInt32)index);
storage[BaseIndex + index] = value;
}
else
{
storage[BaseIndex + index] = value;
}
}
}
public AbstractBuffer DoOperation(OperatorEffect operatorEffect, AbstractValue rhs)
{
if (null == rhs)
{
throw new ArgumentNullException("rhs");
}
var lhs = this;
// TODO: should have a guard for if rhs isn't a pointer
switch (operatorEffect)
{
case OperatorEffect.Assignment:
{
var result = new AbstractBuffer(lhs);
return result;
}
case OperatorEffect.Add:
{
var result = new AbstractBuffer(lhs);
result.BaseIndex += rhs.Value;
return result;
}
case OperatorEffect.Sub:
{
var result = new AbstractBuffer(lhs);
if (result.BaseIndex <
rhs.Value)
{
throw new ArgumentOutOfRangeException(
String.Format(
"Attempting to set a negative baseindex, baseindex: {0:x4}, _subValue {1:x4}",
result.BaseIndex,
rhs.Value
)
);
}
result.BaseIndex -= rhs.Value;
return result;
}
case OperatorEffect.And:
{
var result = new AbstractBuffer(lhs);
result.BaseIndex &= rhs.Value;
return result;
}
default:
throw new ArgumentException(
String.Format("Unsupported OperatorEffect: {0}", operatorEffect), "operatorEffect");
}
}
private Boolean IsIndexPastBounds(Int32 index)
{
return ((BaseIndex + index) >= Length) && ((BaseIndex + index) >= storage.Length);
}
private void Extend(UInt32 newLength)
{
// Account for element [0] of the array
newLength = newLength + 1;
if (newLength >= Length)
{
var extendedCopy = new AbstractValue[newLength];
Int32 i;
for (i = 0; i < storage.Length; i++)
{
extendedCopy[i] = storage[i];
}
for (; i < newLength; i++)
{
extendedCopy[i] = new AbstractValue(AbstractValue.UNKNOWN)
{
IsOutOfBounds = true
};
}
storage = extendedCopy;
}
return;
}
}
}