Questions tagged [btreemap]
15 questions
3
votes
2 answers
How to serialise and deserialise BTreeMaps with arbitrary key types?
This example code:
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
struct Foo {
bar: String,
baz: Baz
}
#[derive(Debug, Clone, Copy,…

fadedbee
- 42,671
- 44
- 178
- 308
2
votes
1 answer
btreemap vs hashmap for transposition table
I have created a minimax algorithm that uses alpha beta pruning and a transposition table to speed up the search. I am currently using a hashmap that uses the board state as the key and saves the score as the value. (the game is tic tac toe on a 5x5…

The Mungler
- 136
- 1
- 12
2
votes
1 answer
Idiomatic way of getting the key of the largest value in a BTreeMap?
I need to get get the key associated with the largest value in a BTreeMap. (Doing this the other way around is simple.)
My attempt so far is:
let mut opt_pair: Option<(&Foo, u32)> = None;
for (key, value) in my_btreemap { // my_btreemap is known…

fadedbee
- 42,671
- 44
- 178
- 308
1
vote
1 answer
How to "zeroize" Rust BTreeMap securely
I want to use Rust's zeroize crate for zeroizing BTreeMap entries. But zeroize doesn't seems to provide an out-of-the-box implementation for BTreeMap.
Currently, I'm resorting to clear to remove the map's entries.
Q1. How safe is it to use simply…

NewToCode
- 174
- 8
0
votes
1 answer
O(N) complexity mapping of a BTreeMap with a closure that increases (or decreases) with respect to key order
I want to make a O(N) time complexity mapping of a BTreeMap with a closure that increases (or decreases) with respect to key order. (N is the number of elements)
In other words, I'd like to do something like this:
/*
f is monotonic, i.e. is either…

FreD
- 393
- 2
- 12
0
votes
0 answers
How to get a mutable value from BTreeMap in a range in rust and if not found get the first value as mutable
I am storing bunch of items in BTreeMap and I am querying for keys as mutable in a range. If nothing is found in that range, I want to return the first value in map as mutable. Idea is doing something like cycle() but without copying. In my…

Sigma
- 742
- 2
- 9
- 24
0
votes
1 answer
How to efficiently clone and truncate a BTreeMap?
I'd like to efficiently, preferably in a functional way, truncate an existing BTreeMap from a reference and return a cloned, truncated BTreeMap of the same key-value pairs.
use std::collections::BTreeMap;
/// I want to return a BTreeMap containing…

Sergio Gliesh
- 329
- 1
- 2
- 8
0
votes
1 answer
Rust - BTreeMap not updating vector in value correctly
I've stumbled over a problem when using BTreeMaps; They don't seem to update while the program is running. Here an example:
fn monke_do_monke_bisnis(mut monkes: BTreeMap) -> BTreeMap {
for mut monke in monkes.to_owned()…

Wadafacc
- 3
- 2
0
votes
1 answer
Get the Dereferenced Keys From a Map
I'm trying to get the keys of a BTreeMap with u32 keys.
When I use the .iter().keys() method, it returns a reference to the keys: &u32.
I understand the logic behind getting a reference to the key because it doesn't consume the data structure, but…

Charles Edward Gagnon
- 65
- 1
- 5
0
votes
1 answer
Find neighbor of target in BTreeMap (or any other treemap) in rust
How to efficiently implement below c++ function in rust? The data structure must be tree based (BTree, RBTree, etc).
Given a sorted map m, a key target, and a value val.
Find the lower_bound entry (the first key >= target). return DEFAULT if no…

azyx
- 35
- 3
0
votes
1 answer
Data Structure with Range Search Against Constantly Updating Keys
I have the need to store many data flows consisting of something like:
struct Flow {
source: Address,
destination: Address,
last_seq_num_sent: u32,
last_seq_num_rcvd: u32,
last_seq_num_ackd: u32
}
I need to query by…

armani
- 93
- 1
- 10
- 23
-2
votes
1 answer
BTreeMap terminal display issue in Rust
I have a beautified string which I want to associate to a key and I want to display it from a Hashmap or a BTreeMap in Rust. My string is shown below as follows:-
The `beautified_string` which when printed on terminal using println!("{}",…

roku675
- 59
- 1
- 5
-2
votes
1 answer
How to get the hash which would be used for a value in a BTreeMap
I'm currently using BTreeMap because I need the stable ordering.
Is there a Rust Map structure which would allow me to tentatively add a new key/value to an ordered Map and see what the hash would be?
Currently I have to clone the whole BTreeMap,…

fadedbee
- 42,671
- 44
- 178
- 308
-3
votes
2 answers
Rust BTreeMap - what is the equivalent of a "peek" or "first" function?
Binary Trees have a root node. Typically with such a data structure implementation, a function is provided to "peek" the root node, as well as the smallest and largest elements.
In other words, in Rust world, I would like to be able to do this:
let…

FreelanceConsultant
- 13,167
- 27
- 115
- 225
-4
votes
1 answer
Rust BTreeMap how to get a reference to the root node element via an OccupiedEntry object?
I asked a question a few minutes ago about how to get a reference to the root node of a Rust BTreeMap. I have since learned that this can be done using first_entry which is a function which returns an Option object.
However, I cannot…

FreelanceConsultant
- 13,167
- 27
- 115
- 225