Module: text.string_interner

Source: ./text/string_interner.reef


Overview

text/string_interner - String Interning for Memory Efficiency

Provides a string interner that deduplicates strings and allows fast comparison by integer ID. Essential for compilers handling many repeated identifiers.

Usage: import text.string_interner

let interner = string_interner.create_interner() let id1 = result.unwrap_ok(string_interner.intern(interner, "main")) let id2 = result.unwrap_ok(string_interner.intern(interner, "main")) // same ID // id1 == id2 (fast integer comparison)

let s = option.unwrap(string_interner.get_string(interner, id1)) // "main"

Benefits:

  • Memory: Single copy of each string
  • Speed: O(n) intern (linear search), O(1) lookup by ID
  • Comparison: Integer compare vs string compare

Contract:

  • intern: Ok(id) on success (existing or newly interned); Err(error.Error) when the interner is at capacity.
  • get_string: Some(str) for a valid id; None if the id is invalid/out of range.
  • get_id: Some(id) if the string is interned; None on miss.

Note: Future versions may use HashMap for O(1) intern


Types

StringInterner

Fields:

Name Type
strings [string]
size int

Functions

fn max_strings(): int

Maximum capacity for interned strings

fn create_interner(): StringInterner

Create a new string interner

fn find_string(interner: StringInterner, s: string): int

Find string in interner, returns ID or -1 if not found

fn intern(interner: StringInterner, s: string): result.Result[int, error.Error]

Intern a string - returns its unique ID If string already exists, returns Ok(existing ID) If new, adds to interner and returns Ok(new ID) Returns Err when the interner is at capacity

fn get_string(interner: StringInterner, id: int): option.Option[string]

Get the string for a given ID Returns None if the ID is invalid/out of range

fn contains(interner: StringInterner, s: string): bool

Check if a string has been interned

fn get_id(interner: StringInterner, s: string): option.Option[int]

Get the ID for a string Returns None if the string has not been interned

fn count(interner: StringInterner): int

Get the number of interned strings


Generated by reefc doc