-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_key_types.zig
159 lines (119 loc) · 5.19 KB
/
01_key_types.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
const std = @import("std");
// Import the Cache type from the zigache library
const Cache = @import("zigache").Cache;
pub fn main() !void {
// Set up a general purpose allocator
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Run examples for different key types
try stringKeys(allocator);
try integerKeys(allocator);
try structKeys(allocator);
try arrayKeys(allocator);
try pointerKeys(allocator);
try enumKeys(allocator);
try optionalKeys(allocator);
}
fn stringKeys(allocator: std.mem.Allocator) !void {
std.debug.print("\n--- String Keys ---\n", .{});
// Create a cache with string keys and values
// The cache size is set to 2 and uses the SIEVE eviction policy
var cache: Cache([]const u8, []const u8, .{}) = try .init(allocator, .{ .cache_size = 2, .policy = .SIEVE });
defer cache.deinit(); // Ensure cache resources are freed when we're done
// Set key-value pairs in the cache
try cache.put("key1", "value1");
try cache.put("key2", "value2");
// Retrieve a value from the cache
if (cache.get("key1")) |value| {
std.debug.print("Value for 'key1': {s}\n", .{value});
}
// Remove a key-value pair and check if it was successful
_ = cache.remove("key2");
std.debug.print("'key2' removed. Contains 'key2': {}\n", .{cache.contains("key2")});
}
fn integerKeys(allocator: std.mem.Allocator) !void {
std.debug.print("\n--- Integer Keys ---\n", .{});
// Create a cache with integer keys and string values
var cache: Cache(i32, []const u8, .{}) = try .init(allocator, .{ .cache_size = 2, .policy = .SIEVE });
defer cache.deinit();
try cache.put(1, "one");
try cache.put(2, "two");
if (cache.get(1)) |value| {
std.debug.print("Value for 1: {s}\n", .{value});
}
_ = cache.remove(2);
std.debug.print("2 removed. Contains 2: {}\n", .{cache.contains(2)});
}
fn structKeys(allocator: std.mem.Allocator) !void {
std.debug.print("\n--- Struct Keys ---\n", .{});
// Define a custom struct to use as a key
const Point = struct { x: i32, y: i32 };
// Create a cache with struct keys and string values
var cache: Cache(Point, []const u8, .{}) = try .init(allocator, .{ .cache_size = 2, .policy = .SIEVE });
defer cache.deinit();
try cache.put(.{ .x = 1, .y = 2 }, "point1");
try cache.put(.{ .x = 3, .y = 4 }, "point2");
if (cache.get(.{ .x = 1, .y = 2 })) |value| {
std.debug.print("Value for (1,2): {s}\n", .{value});
}
_ = cache.remove(.{ .x = 3, .y = 4 });
std.debug.print("(3,4) removed. Contains (3,4): {}\n", .{cache.contains(.{ .x = 3, .y = 4 })});
}
fn arrayKeys(allocator: std.mem.Allocator) !void {
std.debug.print("\n--- Array Keys ---\n", .{});
// Create a cache with fixed-size array keys
var cache: Cache([3]u8, []const u8, .{}) = try .init(allocator, .{ .cache_size = 2, .policy = .SIEVE });
defer cache.deinit();
try cache.put([3]u8{ 1, 2, 3 }, "array1");
try cache.put([3]u8{ 4, 5, 6 }, "array2");
if (cache.get([3]u8{ 1, 2, 3 })) |value| {
std.debug.print("Value for [1,2,3]: {s}\n", .{value});
}
_ = cache.remove([3]u8{ 4, 5, 6 });
std.debug.print("[4,5,6] removed. Contains [4,5,6]: {}\n", .{cache.contains([3]u8{ 4, 5, 6 })});
}
fn pointerKeys(allocator: std.mem.Allocator) !void {
std.debug.print("\n--- Pointer Keys ---\n", .{});
// Create some values to point to
var value1: i32 = 10;
var value2: i32 = 20;
// Create a cache with pointer keys
var cache: Cache(*i32, []const u8, .{}) = try .init(allocator, .{ .cache_size = 2, .policy = .SIEVE });
defer cache.deinit();
try cache.put(&value1, "pointer1");
try cache.put(&value2, "pointer2");
if (cache.get(&value1)) |value| {
std.debug.print("Value for &value1: {s}\n", .{value});
}
_ = cache.remove(&value2);
std.debug.print("&value2 removed. Contains &value2: {}\n", .{cache.contains(&value2)});
}
fn enumKeys(allocator: std.mem.Allocator) !void {
std.debug.print("\n--- Enum Keys ---\n", .{});
// Define an enum to use as keys
const Color = enum { Red, Green, Blue };
// Create a cache with enum keys
var cache: Cache(Color, []const u8, .{}) = try .init(allocator, .{ .cache_size = 2, .policy = .SIEVE });
defer cache.deinit();
try cache.put(.Red, "red");
try cache.put(.Green, "green");
if (cache.get(.Red)) |value| {
std.debug.print("Value for Red: {s}\n", .{value});
}
_ = cache.remove(.Green);
std.debug.print("Green removed. Contains Green: {}\n", .{cache.contains(.Green)});
}
fn optionalKeys(allocator: std.mem.Allocator) !void {
std.debug.print("\n--- Optional Keys ---\n", .{});
// Create a cache with optional integer keys
var cache: Cache(?i32, []const u8, .{}) = try .init(allocator, .{ .cache_size = 2, .policy = .SIEVE });
defer cache.deinit();
try cache.put(null, "null_value");
try cache.put(42, "forty_two");
if (cache.get(null)) |value| {
std.debug.print("Value for null: {s}\n", .{value});
}
_ = cache.remove(42);
std.debug.print("42 removed. Contains 42: {}\n", .{cache.contains(42)});
}