Code Explainers

Code explainers tagged #data-structures

php
final class FormEditorHistory
{
    private SplStack $undoStack;
    private SplStack $redoStack;

Undo/redo with two stacks in PHP

undo-redo stack state-management
Intermediate 9 steps
ruby
class EditorHistory
  MAX_DEPTH = 100
 
  def initialize(initial = "")

How undo/redo history works in Ruby

undo-redo state-management immutability
Intermediate 9 steps
java
public final class IniParser {
 
    private static final Pattern SECTION = Pattern.compile("\\[(.+?)\\]");
 

Parsing INI files in Java

parsing regex state-machine
Intermediate 10 steps
python
class TrieNode:
    __slots__ = ("children", "is_word")
 
    def __init__(self):

Building a prefix trie in Python

trie tree-traversal recursion
Intermediate 7 steps
go
package trie
 
import "sort"
 

Building a frequency-ranked autocomplete trie in Go

trie autocomplete recursion
Intermediate 9 steps
javascript
class LRUCache {
  constructor(capacity) {
    if (capacity < 1) throw new RangeError('capacity must be positive');
    this.capacity = capacity;

Building an LRU cache with a JS Map

lru-cache data-structures insertion-order
Intermediate 8 steps
python
import heapq
 
 
class MovingMedian:

Running median with two heaps

heaps streaming invariants
Advanced 8 steps
go
package hashring
 
import (
	"hash/crc32"

How consistent hashing works in Go

consistent-hashing load-balancing concurrency
Intermediate 8 steps
go
package scheduler
 
import (
	"container/heap"

A priority job queue with Go's container/heap

priority-queue heap interfaces
Intermediate 9 steps
php
<?php
 
namespace App\Queue;
 

A stable priority job queue in PHP

priority-queue data-structures closures
Intermediate 8 steps
php
final class BitmapGrid
{
    private SplFixedArray $cells;
 

A flat-array bitmap grid in PHP

data-structures row-major-order bitmap
Intermediate 7 steps
ruby
class LRUCache
  def initialize(capacity)
    raise ArgumentError, "capacity must be positive" unless capacity.positive?
 

How an LRU cache works in Ruby

caching eviction hash-ordering
Intermediate 7 steps
ruby
class QueryParser
  def self.parse(query_string)
    new(query_string).parse
  end

Parsing nested query strings in Ruby

parsing recursion tokenization
Intermediate 9 steps
javascript
class HistoryStack {
  constructor(initial = '', limit = 100) {
    this.past = [];
    this.present = initial;

Building an undo/redo history stack

undo-redo state-management debouncing
Intermediate 7 steps
python
from collections import OrderedDict
from typing import Any, Hashable, Optional
 
 

Building an LRU cache with OrderedDict

caching eviction data-structures
Intermediate 6 steps
php
<?php
 
function buildTree(array $items, ?int $parentId = null): array
{

Building a tree from a flat list in PHP

recursion tree grouping
Intermediate 6 steps
typescript
export class PriorityQueue<T> {
  private heap: Array<{ value: T; priority: number }> = [];
 
  get size(): number {

Building a binary-heap priority queue in TypeScript

binary-heap priority-queue generics
Intermediate 9 steps
go
package cache
 
import (
	"container/list"

Building a generic LRU cache in Go

lru-cache generics linked-list
Intermediate 8 steps