Skip to content

Commit 7623d42

Browse files
authored
Add Tiger Style algorithms - 9 expert implementations with 102 tests (#30)
* Add Tiger Style algorithms - 9 expert implementations with 102 tests - Time simulation framework (deterministic testing) - Zero-recursion merge sort - Heavy-assertion knapsack DP - Bounded ring buffer (fail-fast FIFO) - Raft consensus algorithm - Two-phase commit protocol - VSR consensus (TigerBeetle's actual consensus) - Robin Hood hash table (cache-efficient) - Skip list (probabilistic ordered map) All implementations follow TigerBeetle's Tiger Style principles: - No recursion, explicit iteration with bounded loops - Heavy assertions (2+ per function) - Explicit u32/u64 types (never usize) - Fail-fast on invalid inputs - Zero technical debt Total: 102 tests, all passing * Add Tiger Style algorithms to test suite - Add 9 Tiger Style algorithms to runall.zig test runner - Add Tiger Style build configurations to build.zig - Includes: time_simulation, merge_sort_tiger, knapsack_tiger, ring_buffer, raft_consensus, two_phase_commit, vsr_consensus, robin_hood_hash, skip_list * Fix formatting for tiger_style files to pass zig fmt checks
1 parent 9bfae68 commit 7623d42

13 files changed

+4427
-0
lines changed

DIRECTORY.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@
4444
* [Quicksort](https://github.com/TheAlgorithms/Zig/blob/HEAD/sort/quickSort.zig)
4545
* [Radixsort](https://github.com/TheAlgorithms/Zig/blob/HEAD/sort/radixSort.zig)
4646

47+
## Tiger Style
48+
Expert-level algorithms following [TigerBeetle's Tiger Style](https://github.com/tigerbeetle/tigerbeetle/blob/main/docs/TIGER_STYLE.md) principles
49+
* [Time Simulation](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/time_simulation.zig) - Deterministic time framework
50+
* [Merge Sort Tiger](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/merge_sort_tiger.zig) - Zero-recursion merge sort
51+
* [Knapsack Tiger](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/knapsack_tiger.zig) - Heavy-assertion DP
52+
* [Ring Buffer](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/ring_buffer.zig) - Bounded FIFO queue
53+
* [Raft Consensus](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/raft_consensus.zig) - Raft consensus
54+
* [Two-Phase Commit](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/two_phase_commit.zig) - 2PC protocol
55+
* [VSR Consensus](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/vsr_consensus.zig) - Viewstamped Replication
56+
* [Robin Hood Hash](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/robin_hood_hash.zig) - Cache-efficient hash table
57+
* [Skip List](https://github.com/TheAlgorithms/Zig/blob/HEAD/tiger_style/skip_list.zig) - Probabilistic ordered map
58+
4759
## Web
4860
* Http
4961
* [Client](https://github.com/TheAlgorithms/Zig/blob/HEAD/web/http/client.zig)

build.zig

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,71 @@ pub fn build(b: *std.Build) void {
224224
.name = "newton_raphson_root.zig",
225225
.category = "numerical_methods",
226226
});
227+
228+
// Tiger Style
229+
if (std.mem.eql(u8, op, "tiger_style/time_simulation"))
230+
buildAlgorithm(b, .{
231+
.optimize = optimize,
232+
.target = target,
233+
.name = "time_simulation.zig",
234+
.category = "tiger_style",
235+
});
236+
if (std.mem.eql(u8, op, "tiger_style/merge_sort_tiger"))
237+
buildAlgorithm(b, .{
238+
.optimize = optimize,
239+
.target = target,
240+
.name = "merge_sort_tiger.zig",
241+
.category = "tiger_style",
242+
});
243+
if (std.mem.eql(u8, op, "tiger_style/knapsack_tiger"))
244+
buildAlgorithm(b, .{
245+
.optimize = optimize,
246+
.target = target,
247+
.name = "knapsack_tiger.zig",
248+
.category = "tiger_style",
249+
});
250+
if (std.mem.eql(u8, op, "tiger_style/ring_buffer"))
251+
buildAlgorithm(b, .{
252+
.optimize = optimize,
253+
.target = target,
254+
.name = "ring_buffer.zig",
255+
.category = "tiger_style",
256+
});
257+
if (std.mem.eql(u8, op, "tiger_style/raft_consensus"))
258+
buildAlgorithm(b, .{
259+
.optimize = optimize,
260+
.target = target,
261+
.name = "raft_consensus.zig",
262+
.category = "tiger_style",
263+
});
264+
if (std.mem.eql(u8, op, "tiger_style/two_phase_commit"))
265+
buildAlgorithm(b, .{
266+
.optimize = optimize,
267+
.target = target,
268+
.name = "two_phase_commit.zig",
269+
.category = "tiger_style",
270+
});
271+
if (std.mem.eql(u8, op, "tiger_style/vsr_consensus"))
272+
buildAlgorithm(b, .{
273+
.optimize = optimize,
274+
.target = target,
275+
.name = "vsr_consensus.zig",
276+
.category = "tiger_style",
277+
});
278+
if (std.mem.eql(u8, op, "tiger_style/robin_hood_hash"))
279+
buildAlgorithm(b, .{
280+
.optimize = optimize,
281+
.target = target,
282+
.name = "robin_hood_hash.zig",
283+
.category = "tiger_style",
284+
});
285+
if (std.mem.eql(u8, op, "tiger_style/skip_list"))
286+
buildAlgorithm(b, .{
287+
.optimize = optimize,
288+
.target = target,
289+
.name = "skip_list.zig",
290+
.category = "tiger_style",
291+
});
227292
}
228293

229294
fn buildAlgorithm(b: *std.Build, info: BInfo) void {

runall.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ pub fn main() !void {
5151

5252
// Numerical Methods
5353
try runTest(allocator, "numerical_methods/newton_raphson");
54+
55+
// Tiger Style
56+
try runTest(allocator, "tiger_style/time_simulation");
57+
try runTest(allocator, "tiger_style/merge_sort_tiger");
58+
try runTest(allocator, "tiger_style/knapsack_tiger");
59+
try runTest(allocator, "tiger_style/ring_buffer");
60+
try runTest(allocator, "tiger_style/raft_consensus");
61+
try runTest(allocator, "tiger_style/two_phase_commit");
62+
try runTest(allocator, "tiger_style/vsr_consensus");
63+
try runTest(allocator, "tiger_style/robin_hood_hash");
64+
try runTest(allocator, "tiger_style/skip_list");
5465
}
5566

5667
fn runTest(allocator: std.mem.Allocator, comptime algorithm: []const u8) !void {

tiger_style/README.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Tiger Style Algorithms
2+
3+
> "Simplicity and elegance are unpopular because they require hard work and discipline to achieve" — Edsger Dijkstra
4+
5+
This folder showcases **expert-level algorithmic techniques** following [TigerBeetle's Tiger Style](https://github.com/tigerbeetle/tigerbeetle/blob/main/docs/TIGER_STYLE.md) coding principles.
6+
7+
## Tiger Style Principles
8+
9+
### 1. **Safety First**
10+
11+
- Heavy use of assertions (minimum 2 per function)
12+
- Assert all preconditions, postconditions, and invariants
13+
- Fail-fast on violations
14+
- Assertions downgrade catastrophic bugs into liveness bugs
15+
16+
### 2. **Explicit Everything**
17+
18+
- Use explicitly-sized types: `u32`, `u64`, `i32` (never `usize`)
19+
- Simple, explicit control flow only
20+
- No recursion - all algorithms are iterative
21+
- Bounded loops with explicit upper limits
22+
23+
### 3. **Zero Technical Debt**
24+
25+
- Production-quality code from the start
26+
- No shortcuts or workarounds
27+
- Solve problems correctly the first time
28+
- Every abstraction must earn its place
29+
30+
### 4. **Deterministic Testing**
31+
32+
- Time simulation for reproducible tests
33+
- Fuzz-friendly with heavy assertions
34+
- Test edge cases exhaustively
35+
36+
## Implementations
37+
38+
### `time_simulation.zig`
39+
40+
**Deterministic time simulation framework** inspired by TigerBeetle's testing approach.
41+
42+
- Virtual clock with nanosecond precision
43+
- Deterministic event scheduling
44+
- Reproducible test scenarios
45+
- Perfect for testing distributed algorithms
46+
47+
### `merge_sort_tiger.zig`
48+
49+
**Zero-recursion merge sort** with Tiger Style discipline.
50+
51+
- Iterative bottom-up implementation
52+
- Explicit stack bounds
53+
- Heavy assertions on every invariant
54+
- No hidden allocations
55+
56+
### `knapsack_tiger.zig`
57+
58+
**0/1 Knapsack with militant assertion discipline.**
59+
60+
- Every array access validated
61+
- Explicit capacity bounds
62+
- DP table invariants checked
63+
- Overflow protection
64+
65+
### `ring_buffer.zig`
66+
67+
**Bounded ring buffer** demonstrating fail-fast principles.
68+
69+
- Fixed capacity with compile-time guarantees
70+
- All operations bounded O(1)
71+
- Assertions on every state transition
72+
- Production-grade reliability
73+
74+
### `raft_consensus.zig`
75+
76+
**Raft consensus algorithm** for distributed systems.
77+
78+
- Explicit state machine (follower/candidate/leader)
79+
- Bounded log with fail-fast
80+
- Leader election with majority votes
81+
- Inspired by TigerBeetle's consensus approach
82+
83+
### `two_phase_commit.zig`
84+
85+
**Two-Phase Commit protocol** for distributed transactions.
86+
87+
- Coordinator with bounded participants
88+
- Prepare and commit phases
89+
- Timeout detection
90+
- Atomic commit/abort decisions
91+
92+
### `vsr_consensus.zig`
93+
94+
**VSR (Viewstamped Replication)** - TigerBeetle's actual consensus.
95+
96+
- More sophisticated than Raft
97+
- View change protocol
98+
- Explicit view and op numbers
99+
- Inspired by "Viewstamped Replication Revisited"
100+
101+
### `robin_hood_hash.zig`
102+
103+
**Cache-efficient hash table** with Robin Hood hashing.
104+
105+
- Fixed capacity (no dynamic resizing)
106+
- Linear probing with fairness
107+
- Bounded probe distances
108+
- Explicit load factor limits
109+
110+
### `skip_list.zig`
111+
112+
**Skip list** - probabilistic ordered map.
113+
114+
- Foundation for LSM trees
115+
- Deterministic randomness (seeded RNG)
116+
- Bounded maximum level
117+
- No recursion (iterative traversal)
118+
119+
## Why Tiger Style?
120+
121+
Tiger Style is about **engineering excellence**:
122+
123+
1. **Code you can trust** - Heavy assertions catch bugs during fuzzing
124+
2. **No surprises** - Explicit bounds prevent tail latency spikes
125+
3. **Maintainable** - Simple control flow is easy to reason about
126+
4. **Fast** - No recursion overhead, explicit types, cache-friendly
127+
128+
## Running Tests
129+
130+
```bash
131+
# Test individual files
132+
zig test tiger_style/time_simulation.zig # 7 tests
133+
zig test tiger_style/merge_sort_tiger.zig # 14 tests
134+
zig test tiger_style/knapsack_tiger.zig # 12 tests
135+
zig test tiger_style/ring_buffer.zig # 15 tests
136+
zig test tiger_style/raft_consensus.zig # 12 tests
137+
zig test tiger_style/two_phase_commit.zig # 9 tests
138+
zig test tiger_style/vsr_consensus.zig # 11 tests ⭐
139+
zig test tiger_style/robin_hood_hash.zig # 12 tests ⭐
140+
zig test tiger_style/skip_list.zig # 10 tests ⭐
141+
142+
# Total: 102 tests across 9 implementations!
143+
```
144+
145+
## Contributing
146+
147+
When adding to tiger_style/:
148+
149+
1. **No recursion** - use iteration with explicit bounds
150+
2. **Assert everything** - minimum 2 assertions per function
151+
3. **Explicit types** - u32/u64/i32, never usize
152+
4. **Bounded loops** - every loop must have a provable upper bound
153+
5. **Fail-fast** - detect violations immediately
154+
6. **Simple control flow** - keep it explicit and obvious
155+
156+
---
157+
158+
*"The rules act like the seat-belt in your car: initially they are perhaps a little uncomfortable, but after a while their use becomes second-nature and not using them becomes unimaginable."* — Gerard J. Holzmann

0 commit comments

Comments
 (0)