|
| 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