A compact generational arena data structure for Rust.

Overview

Compact Generational Arena

This crate provides Arena<T>, a contiguous growable container which assigns and returns IDs to values when they are added to it.

These IDs can then be used to access their corresponding values at any time, like an index, except that they remain valid even if other items in the arena are removed or if the arena is sorted.

  • faster insersion, removal, and lookup speed than a HashMap
  • values are stored in a contiguous vector you can access/iterate
  • memory and cache efficient: uses only 2 heap-allocated vectors

Examples

// create a new empty arena
let mut arena = Arena::new();

// add values to it and store their returned IDs
let a = arena.insert('A');
let b = arena.insert('B');
let c = arena.insert('C');
let d = arena.insert('D');

// we can use those IDs to fetch the values
assert_eq!(arena.get(a), Some(&'A'));
assert_eq!(arena.get(b), Some(&'B'));
assert_eq!(arena.get(c), Some(&'C'));
assert_eq!(arena.get(d), Some(&'D'));

// the values live in a contiguous vector we can access
assert_eq!(arena.as_slice(), &['A', 'B', 'C', 'D']);

// we can remove a value from anywhere using its ID
arena.remove(b);

// the value at the end will fill the hole left by the removed one
assert_eq!(arena.as_slice(), &['A', 'D', 'C']);

// even though 'D' moved, its ID is still valid and can be used
assert_eq!(arena.get(a), Some(&'A'));
assert_eq!(arena.get(b), None);
assert_eq!(arena.get(c), Some(&'C'));
assert_eq!(arena.get(d), Some(&'D'));

// we can even sort the values to order them
arena.sort();

// and all the IDs will still be valid
assert_eq!(arena.as_slice(), &['A', 'C', 'D']);
assert_eq!(arena.get(a), Some(&'A'));
assert_eq!(arena.get(b), None);
assert_eq!(arena.get(c), Some(&'C'));
assert_eq!(arena.get(d), Some(&'D'));
You might also like...
The ultimate Data Engineering Chadstack. Apache Airflow running Rust. Bring it.

RustOnApacheAirflow The ultimate Data Engineering Chadstack. Apache Airflow running Rust. Bring it. This is part of a larger blog post trying to do so

Data structures and algorithms for 3D geometric modeling.

geom3d Data structures and algorithms for 3D geometric modeling. Features: Bezier curve and surface B-Spline curve and surface Spin surface Sweep surf

Prometheus exporter that scrapes data in different formats

data-exporter A prometheus exporter that scrapes remote data or local files and converts them to prometheus metrics. It is similar to json_exporter, b

A tool to deserialize data from an input encoding, transform it and serialize it back into an output encoding.

dts A simple tool to deserialize data from an input encoding, transform it and serialize it back into an output encoding. Requires rust = 1.56.0. Ins

A simple and convenient way to bundle owned data with a borrowing type.

A simple and convenient way to bundle owned data with a borrowing type. The Problem One of the main selling points of Rust is its borrow checker, whic

An implementation of a predicative polymorphic language with bidirectional type inference and algebraic data types

Vinilla Lang Vanilla is a pure functional programming language based on System F, a classic but powerful type system. Merits Simple as it is, Vanilla

A parser for the perf.data format

linux-perf-data This repo contains a parser for the perf.data format which is output by the Linux perf tool. It also contains a main.rs which acts sim

Parse and encoding of data using the SCTE-35 standard.

SCTE-35 lib and parser for Rust Work in progress! This library provide access to parse and encoding of data using the SCTE-35 standard. This standard

Owner
Chevy Ray Johnston
I'm a video game programmer, designer, artist, and writer. Creator of Ikenfell: http://ikenfell.com
Chevy Ray Johnston
A Rust crate that implements a range map data structure backed by a Vec.

range_map_vec This crate implements a range map data structure backed by a Vec using binary search. Docs and usage can be found in the corresponding r

Microsoft 9 Sep 8, 2023
Proof-of-concept for a memory-efficient data structure for zooming billion-event traces

Proof-of-concept for a gigabyte-scale trace viewer This repo includes: A memory-efficient representation for event traces An unusually simple and memo

Tristan Hume 59 Sep 5, 2022
Generate SUMMARY.md files based on your book's file structure

mdbook-autosummary Generate a SUMMARY.md for your mdBook based on your folder structure! Warning The implementation is hacky and has several limitatio

Hyper 3 Sep 30, 2023
An expression based data notation, aimed at transpiling itself to any cascaded data notation.

Lala An expression oriented data notation, aimed at transpiling itself to any cascaded data notation. Lala is separated into three components: Nana, L

null 37 Mar 9, 2022
A library for transcoding between bytes in Astro Notation Format and Native Rust data types.

Rust Astro Notation A library for transcoding between hexadecimal strings in Astro Notation Format and Native Rust data types. Usage In your Cargo.tom

Stelar Software 1 Feb 4, 2022
A HashMap/Vector hybrid: efficient, ordered key-value data storage in Rust.

hashvec A HashVec is a hash map / dictionary whose key-value pairs are stored (and can be iterated over) in a fixed order, by default the order in whi

Skye Terran 2 May 16, 2022
Library and proc macro to analyze memory usage of data structures in rust.

Allocative: memory profiler for Rust This crate implements a lightweight memory profiler which allows object traversal and memory size introspection.

Meta Experimental 19 Jan 6, 2023
A fast rendezvous in rust where data can optionally be swapped between the two threads.

rendezvous_swap A rendezvous is an execution barrier between a pair of threads, but this crate also provides the option of swapping data at the synchr

Erik 5 Mar 17, 2023
Platform independent data channels for WebRTC/Rust.

preach Platform independent data channels Preach provides an abstraction for WebRTC data channels that runs on both native and web platforms. Preach m

Douglas Dwyer 3 Apr 16, 2023
Rust library for concurrent data access, using memory-mapped files, zero-copy deserialization, and wait-free synchronization.

mmap-sync mmap-sync is a Rust crate designed to manage high-performance, concurrent data access between a single writer process and multiple reader pr

Cloudflare 97 Jun 26, 2023