-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_access.zig
214 lines (168 loc) · 5.93 KB
/
random_access.zig
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const std = @import("std");
pub fn init(ptr: anytype) Address(@TypeOf(ptr)) {
return Address(@TypeOf(ptr)){ .value = @intFromPtr(ptr) };
}
/// creates a runtime address from a given pointer
pub fn Address(comptime T: type) type {
return struct {
const Self = @This();
pub const Size = PointerSize(T);
pub const Child = PointerChild(T);
pub const Const = isConst(T);
value: usize,
/// inplace addition to current address by n * sizeof T
pub fn add(self: *Self, n: usize) void {
self.value += n * @sizeOf(Child);
}
/// inplace subtraction to current address by n * sizeof T
pub fn sub(self: *Self, n: usize) void {
self.value -= n * @sizeOf(Child);
}
/// returns pointer of original type and sets address to zero.
pub fn release(self: *Self) T {
defer self.value = 0;
return @ptrFromInt(self.value);
}
/// returns one-item pointer
pub fn one(self: Self) One(Child, Const) {
return @ptrFromInt(self.value);
}
/// returns many-item pointer
pub fn many(self: Self) Many(Child, Const) {
return @ptrFromInt(self.value);
}
/// check if pointer is equal to zero.
pub fn isZero(self: Address) bool {
return self.value == 0;
}
/// check current is equal to other
pub fn eql(self: Self, other: Self) bool {
return self.value == other.value;
}
/// check current is less than other
pub fn lt(self: Self, other: Self) bool {
return self.value < other.value;
}
/// check current is greater than other
pub fn gt(self: Self, other: Self) bool {
return self.value > other.value;
}
/// check current is less than or equal to other
pub fn lte(self: Self, other: Self) bool {
return self.value <= other.value;
}
/// check current is greater than or equal to other
pub fn gte(self: Self, other: Self) bool {
return self.value >= other.value;
}
};
}
fn One(comptime T: type, comptime is_const: bool) type {
return if (is_const) *const T else *T;
}
fn Many(comptime T: type, comptime is_const: bool) type {
return if (is_const) [*]const T else [*]T;
}
fn isConst(comptime T: type) bool {
return switch (@typeInfo(T)) {
.Pointer => |p| p.is_const,
else => @compileError("Requires pointer type, recieved: " ++ @typeName(T))
};
}
fn PointerSize(comptime T: type) std.builtin.Type.Pointer.Size {
return switch (@typeInfo(T)) {
.Pointer => |p| blk: {
if (p.size == .Slice) {
@compileError("Address object requires direct pointer, not slice. Considering using slice.ptr instead.");
}
break :blk p.size;
},
else => @compileError("Requires pointer type, recieved: " ++ @typeName(T))
};
}
fn PointerChild(comptime T: type) type {
return switch (@typeInfo(T)) {
.Pointer => |p| p.child,
else => @compileError("Requires pointer type, recieved: " ++ @typeName(T))
};
}
///////////////////////////////////////
// Testing //
///////////////////////////////////////
// simulaties an import
const ra = @This();
test "slice check addresses" {
const slice = try std.testing.allocator.alloc(i32, 100);
defer std.testing.allocator.free(slice);
@memset(slice, 42);
// create address at beginning of array
var addr = ra.init(slice.ptr);
// loop over slice and check every address
for (slice) |*value| {
const n: usize = @intFromPtr(value);
try std.testing.expect(addr.value == n);
addr.add(1);
}
std.debug.print("\n\n", .{});
}
test "slice iterate" {
const slice = try std.testing.allocator.alloc(i32, 100);
defer std.testing.allocator.free(slice);
@memset(slice, 42);
// create address at beginning of array
var addr = ra.init(slice.ptr);
// create address at one past end of array
const end = ra.init(slice.ptr + slice.len);
// iterate until we reach the end
while (addr.lt(end)) : (addr.add(1)) {
try std.testing.expectEqual(42, addr.one().*);
}
try std.testing.expect(addr.eql(end));
}
test "slice set values" {
const slice = try std.testing.allocator.alloc(i32, 100);
defer std.testing.allocator.free(slice);
@memset(slice, 42);
// create address at beginning of array
var addr = ra.init(slice.ptr);
// create address at one past end of array
const end = ra.init(slice.ptr + slice.len);
while (addr.lt(end)) : (addr.add(1)) {
addr.one().* = 43;
}
try std.testing.expect(addr.eql(end));
for (slice) |value| {
try std.testing.expectEqual(43, value);
}
}
test "slice check addresses reverse" {
const slice = try std.testing.allocator.alloc(i32, 100);
defer std.testing.allocator.free(slice);
@memset(slice, 42);
// create address at one past end of array
var addr = ra.init(slice.ptr + slice.len);
var i: usize = slice.len;
// loop in reverse and check every address
while (i != 0) {
i -= 1;
addr.sub(1);
const n: usize = @intFromPtr(&slice[i]);
try std.testing.expect(addr.value == n);
}
}
test "slice set values reverse" {
const slice = try std.testing.allocator.alloc(i32, 100);
defer std.testing.allocator.free(slice);
@memset(slice, 42);
// create address to last element of array
var addr = ra.init(slice.ptr + (slice.len - 1));
// create address to first element of array
const end = ra.init(slice.ptr);
// iterate until we are one past the beginning
while (addr.gte(end)) : (addr.sub(1)) {
addr.one().* = 55;
}
for (slice) |value| {
try std.testing.expectEqual(55, value);
}
}