Ruby: use IndexMap

This is the same idea as Java's LinkedHashMap: it gives the same O(1)
insertion and lookup as HashMap, but preserves insertion order for
iteration.
This commit is contained in:
Nick Rolfe
2021-11-01 19:16:50 +00:00
parent 6908a0dc12
commit ea5d696d55
4 changed files with 8 additions and 7 deletions

BIN
ruby/Cargo.lock generated

Binary file not shown.

View File

@@ -18,3 +18,4 @@ tracing-subscriber = { version = "0.2", features = ["env-filter"] }
rayon = "1.5.0"
num_cpus = "1.13.0"
regex = "1.4.3"
indexmap = "1.7.0"

View File

@@ -1,6 +1,6 @@
use crate::trap;
use indexmap::IndexMap;
use node_types::{EntryKind, Field, NodeTypeMap, Storage, TypeName};
use std::collections::BTreeMap as Map;
use std::fmt;
use std::path::Path;
@@ -407,7 +407,7 @@ impl<'a> Visitor<'a> {
child_nodes: &[ChildNode],
parent_id: trap::Label,
) -> Option<Vec<trap::Arg>> {
let mut map: Map<&Option<String>, (&Field, Vec<trap::Arg>)> = Map::new();
let mut map: IndexMap<&Option<String>, (&Field, Vec<trap::Arg>)> = IndexMap::new();
for field in fields {
map.insert(&field.name, (field, Vec::new()));
}

View File

@@ -1,22 +1,22 @@
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt;
use std::io::BufWriter;
use std::path::Path;
use flate2::write::GzEncoder;
use indexmap::IndexMap;
pub struct Writer {
/// Labels that should be assigned fresh ids, e.g. `#123=*`.
fresh_ids: Vec<Label>,
/// Labels that should be assigned trap keys, e.g. `#7=@"foo"`.
global_keys: BTreeMap<String, Label>,
global_keys: IndexMap<String, Label>,
/// Database rows to emit. Each key is the tuple name, each value is a list.
/// Each member of *that* list represents an instance of that tuple,
/// containing a list of the arguments/column values.
tuples: BTreeMap<String, Vec<Vec<Arg>>>,
tuples: IndexMap<String, Vec<Vec<Arg>>>,
/// A counter for generating fresh labels
counter: u32,
@@ -26,8 +26,8 @@ impl Writer {
pub fn new() -> Writer {
Writer {
fresh_ids: Vec::new(),
tuples: BTreeMap::new(),
global_keys: BTreeMap::new(),
tuples: IndexMap::new(),
global_keys: IndexMap::new(),
counter: 0,
}
}