A HashMap/Vector hybrid: efficient, ordered key-value data storage in Rust.

Overview

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 which they were inserted into the map. It's essentially a vector whose values can be inserted/retrieved with keys.

Example

use hashvec::*;

// Create a new hashvec containing pairs of animal names and species
// The hashvec! macro acts like vec!, but with key-value tuple pairs
let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
    ("Doug", "Kobold"),
    ("Skye", "Hyena"),
    ("Lee", "Shiba"),
    ("Sock", "Man"),
    ("Salad", "Wolf"),
    ("Finn", "Human")
];

// Insert a value into the hashvec (HashMap-style)
// Inserting overwrites existing keys' entries in-place
hashvec.insert("Jake", "Dog");

// Push a value onto the hashvec (Vector-style)
// Pushing overwrites existing keys' entries and moves them to the end
hashvec.push(("Susie", "Squid"));

// Access a value by key
match hashvec.get(&"Finn") {
    Some(value) => {
        assert_eq!(*value, "Human");
    },
    None => {}
}

// Access an entry by index
let lee_value = hashvec[2];
assert_eq!(lee_value, ("Lee", "Shiba"));

// Get the index of a key
let lee_index = hashvec.index(&"Lee").unwrap();
assert_eq!(lee_index, 2);

// Get the length of the hashvec
let hashvec_length = hashvec.len();
assert_eq!(hashvec_length, 8);

// Change an entry's key in-place
hashvec.rename(&"Salad", "Caesar");
assert_eq!(hashvec[4], ("Caesar", "Wolf"));

// Mutate a value
match hashvec.get_mut(&"Sock") {
    Some(value) => {
        *value = "Guinea Pig";
    },
    None => {}
}
assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");

// Remove an entry
hashvec.remove_key(&"Doug");
assert_eq!(hashvec.get(&"Doug"), None);

// Swap the locations of two entries by their keys
hashvec.swap_keys(&"Lee", &"Skye");
assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
assert_eq!(hashvec.index(&"Skye").unwrap(), 1);

// Now swap them again, by their indices
hashvec.swap_indices(0, 1);
assert_eq!(hashvec[0], ("Skye", "Hyena"));
assert_eq!(hashvec[1], ("Lee", "Shiba"));

// Iterate over each of the key-value pairs in the hashvec
for (k, v) in hashvec.into_iter() {
    println!("{} is a {}!", k, v);
}

// Remove an entry from the end of the hashvec
let last_entry = hashvec.pop();
assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));

// Clear the hashvec
hashvec.clear();
You might also like...
Powerful math lib for Vector, Matrix and Quaternion operations

An opinionated, powerful math lib for Vector2, Vector3, Matrix and Quaternion operations Vector2 Add, Sub, Div, Mul, Eq Distance Move towards target a

Powerful math lib for Vector, Matrix and Quaternion operations
Powerful math lib for Vector, Matrix and Quaternion operations

An opinionated, powerful math lib for Vector2, Vector3, Matrix and Quaternion operations Vector2 Add, Sub, Div, Mul, Eq Distance Move towards target a

Type erased vector. All elements have the same type.

Type erased vector. All elements have the same type. Designed to be type-erased as far as possible - most of the operations does not know about concre

Compact, clone-on-write vector and string.

ecow Compact, clone-on-write vector and string. Types An EcoVec is a reference-counted clone-on-write vector. It takes up two words of space (= 2 usiz

Vemcache is an in-memory vector database.

Vemcache Vemcache is an in-memory vector database. Vemcache can be thought of as the Redis equivalent for vector databases. Getting Started Prerequisi

The first compute-centric vector graphic video game
The first compute-centric vector graphic video game

🕹️ Vong This repository contains source code for the first native use of a compute-centric vector graphics video game, inspired by Pong. ✍️ Authors @

An AI-native lightweight, reliable, and high performance open-source vector database.
An AI-native lightweight, reliable, and high performance open-source vector database.

What is OasysDB? OasysDB is a vector database that can be used to store and query high-dimensional vectors. Our goal is to make OasysDB fast and easy

Manas project aims to create a modular framework and ecosystem to create robust storage servers adhering to Solid protocol in rust.

मनस् | Manas Solid is a web native protocol to enable interoperable, read-write, collaborative, and decentralized web, truer to web's original vision.

High concurrency, RealTime, In-memory storage inspired by erlang mnesia
High concurrency, RealTime, In-memory storage inspired by erlang mnesia

DarkBird is a Document oriented, high concurrency in-memory Storage, also persist data to disk to avoid loss any data The darkbird provides the follow

Owner
Skye Terran
VFX programmer / technical artist specializing in real-time visual effects and virtual production.
Skye Terran
An embedded key-value storage for learning purpose, which is based on the idea of SSTable / LSM-tree.

Nouzdb An embedded key-value storage for learning purpose, which is based on the idea of SSTable / LSM-tree. Plan Implement a memtable. Implement the

Nouzan 1 Dec 5, 2021
Linked Atomic Random Insert Vector: a thread-safe, self-memory-managed vector with no guaranteed sequential insert.

Linked Atomic Random Insert Vector Lariv is a thread-safe, self-memory-managed vector with no guaranteed sequential insert. It internally uses a linke

Guillem Jara 8 Feb 1, 2023
An inline SIMD accelerated hashmap designed for small amount of data.

Small-Map An inline SIMD accelerated hashmap designed for small amount of data. Usage use small_map::SmallMap; // Don't worry about the 16 here. // Wh

ihc童鞋@提不起劲 49 Nov 14, 2023
The Fast Vector Similarity Library is designed to provide efficient computation of various similarity measures between vectors.

Fast Vector Similarity Library Introduction The Fast Vector Similarity Library is designed to provide efficient computation of various similarity meas

Jeff Emanuel 243 Sep 6, 2023
A simpler and 5x faster alternative to HashMap in Rust, which doesn't use hashing and doesn't use heap

At least 5x faster alternative of HashMap, for very small maps. It is also faster than FxHashMap, hashbrown, ArrayMap, and nohash-hasher. The smaller

Yegor Bugayenko 12 Apr 19, 2023
A lock-free, partially wait-free, eventually consistent, concurrent hashmap.

A lock-free, partially wait-free, eventually consistent, concurrent hashmap. This map implementation allows reads to always be wait-free on certain pl

Ian Smith 216 Nov 18, 2022
A small in-memory key value database for rust

SmollDB Small in-memory key value database for rust This is a small in-memory key-value database, which can be easly backed up in a file or stream and

null 13 Dec 15, 2022
Key-value store for embedded systems, for raw NOR flash, using an LSM-Tree.

ekv Key-value store for embedded systems, for raw NOR flash, using an LSM-Tree. Features None yet TODO Everything Minimum supported Rust version (MSRV

Dario Nieuwenhuis 16 Nov 22, 2022
Rust Vector for large amounts of data, that does not copy when growing, by using full `mmap`'d pages.

Large Vector Rust Vector for large amounts of data, that does not copy when growing, by using full mmap'd pages. Maturity I made ths to learn about mm

Wonko der Verständige 21 Apr 23, 2024
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