From d4432cda7ad3117d5dce66797f7178c0c76900f9 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 24 Apr 2024 19:52:56 -0500 Subject: [PATCH] cl-structures: Give the StringArena a new home. --- compiler/cl-structures/src/arena.rs | 51 +------------------ compiler/cl-structures/src/arena/intern.rs | 2 +- .../cl-structures/src/arena/string_arena.rs | 48 +++++++++++++++++ 3 files changed, 51 insertions(+), 50 deletions(-) create mode 100644 compiler/cl-structures/src/arena/string_arena.rs diff --git a/compiler/cl-structures/src/arena.rs b/compiler/cl-structures/src/arena.rs index 2baa4f4..ca1e1cb 100644 --- a/compiler/cl-structures/src/arena.rs +++ b/compiler/cl-structures/src/arena.rs @@ -1,54 +1,7 @@ -//! Simple, long-lived string buffer - -use std::marker::PhantomData; -use symbol::Symbol; +//! Simple arena-like allocation pub mod global_intern; pub mod intern; +pub mod string_arena; pub mod symbol; -/// Compactly stores a set of immutable strings, producing a [Symbol] for each one -#[derive(Debug)] -pub struct StringArena { - ends: Vec, - buf: String, - _t: PhantomData, -} - -impl StringArena { - pub fn new() -> Self { - Default::default() - } - /// # May panic - /// Panics if Symbol::from_usize would panic - fn next_key(&self) -> T { - Symbol::from_usize(self.ends.len()) - } - - fn get_span(&self, key: T) -> Option<(usize, usize)> { - let key = key.into_usize(); - Some((*self.ends.get(key - 1)?, *self.ends.get(key)?)) - } - - pub fn get(&self, key: T) -> Option<&str> { - let (start, end) = self.get_span(key)?; - // Safety: start and end offsets were created by push_string - Some(unsafe { self.buf.get_unchecked(start..end) }) - } - - pub fn push_string(&mut self, s: &str) -> T { - if self.ends.is_empty() { - self.ends.push(self.buf.len()) - } - let key = self.next_key(); - self.buf.push_str(s); - self.ends.push(self.buf.len()); - key - } -} - -impl Default for StringArena { - fn default() -> Self { - Self { ends: Default::default(), buf: Default::default(), _t: PhantomData } - } -} diff --git a/compiler/cl-structures/src/arena/intern.rs b/compiler/cl-structures/src/arena/intern.rs index 71b0ee0..139d22d 100644 --- a/compiler/cl-structures/src/arena/intern.rs +++ b/compiler/cl-structures/src/arena/intern.rs @@ -1,6 +1,6 @@ //! A string interner with deduplication -use super::{symbol::Symbol, StringArena}; +use super::{string_arena::StringArena, symbol::Symbol}; use hashbrown::hash_table::HashTable; use std::hash::{BuildHasher, RandomState}; diff --git a/compiler/cl-structures/src/arena/string_arena.rs b/compiler/cl-structures/src/arena/string_arena.rs new file mode 100644 index 0000000..38ace17 --- /dev/null +++ b/compiler/cl-structures/src/arena/string_arena.rs @@ -0,0 +1,48 @@ +//! Compactly stores a set of immutable strings, producing a [Symbol] for each one +use super::symbol::Symbol; +use std::marker::PhantomData; +/// Compactly stores a set of immutable strings, producing a [Symbol] for each one +#[derive(Debug)] +pub struct StringArena { + ends: Vec, + buf: String, + _t: PhantomData, +} + +impl StringArena { + pub fn new() -> Self { + Default::default() + } + /// # May panic + /// Panics if Symbol::from_usize would panic + fn next_key(&self) -> T { + Symbol::from_usize(self.ends.len()) + } + + fn get_span(&self, key: T) -> Option<(usize, usize)> { + let key = key.into_usize(); + Some((*self.ends.get(key - 1)?, *self.ends.get(key)?)) + } + + pub fn get(&self, key: T) -> Option<&str> { + let (start, end) = self.get_span(key)?; + // Safety: start and end offsets were created by push_string + Some(unsafe { self.buf.get_unchecked(start..end) }) + } + + pub fn push_string(&mut self, s: &str) -> T { + if self.ends.is_empty() { + self.ends.push(self.buf.len()) + } + let key = self.next_key(); + self.buf.push_str(s); + self.ends.push(self.buf.len()); + key + } +} + +impl Default for StringArena { + fn default() -> Self { + Self { ends: Default::default(), buf: Default::default(), _t: PhantomData } + } +}