switched to sets and maps with stable serialization

This commit is contained in:
aaron-jack-manning 2022-09-07 20:26:20 +10:00
parent 33114d5eb2
commit c19b157a28
3 changed files with 11 additions and 11 deletions

View File

@ -4,19 +4,19 @@ use crate::format;
use crate::tasks::Id;
use std::fmt::Write;
use std::collections::{HashSet, HashMap, BTreeSet};
use std::collections::{HashSet, BTreeSet, BTreeMap};
use serde_with::{serde_as, DisplayFromStr};
#[serde_as]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Graph {
#[serde_as(as = "HashMap<DisplayFromStr, _>")]
pub edges : HashMap<Id, HashSet<Id>>,
#[serde_as(as = "BTreeMap<DisplayFromStr, _>")]
pub edges : BTreeMap<Id, BTreeSet<Id>>,
}
impl Graph {
pub fn create(tasks : Vec<tasks::Task>) -> Self {
let mut edges = HashMap::with_capacity(tasks.len());
let mut edges = BTreeMap::new();
for task in tasks {
edges.insert(task.data.id, task.data.dependencies);
@ -32,7 +32,7 @@ impl Graph {
}
pub fn insert_node(&mut self, node : Id) -> bool {
self.edges.insert(node, HashSet::new()).is_none()
self.edges.insert(node, BTreeSet::new()).is_none()
}
pub fn insert_edge(&mut self, first : Id, second : Id) -> Result<bool, error::Error> {

View File

@ -4,19 +4,19 @@ use crate::format;
use crate::tasks::Id;
use std::fmt::Write;
use std::collections::HashMap;
use std::collections::BTreeMap;
use serde_with::{serde_as, DisplayFromStr};
#[serde_as]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Index {
#[serde_as(as = "HashMap<DisplayFromStr, _>")]
map : HashMap<String, Vec<Id>>
#[serde_as(as = "BTreeMap<DisplayFromStr, _>")]
map : BTreeMap<String, Vec<Id>>
}
impl Index {
pub fn create(tasks : &Vec<tasks::Task>) -> Index {
let mut map : HashMap<String, Vec<Id>> = HashMap::with_capacity(tasks.len());
let mut map : BTreeMap<String, Vec<Id>> = BTreeMap::new();
for task in tasks {
match map.get_mut(&task.data.name) {
Some(ids) => {

View File

@ -11,7 +11,7 @@ use std::mem;
use std::cmp;
use std::path;
use std::io::{Write, Seek};
use std::collections::{HashSet, HashMap};
use std::collections::{HashSet, HashMap, BTreeSet};
use chrono::SubsecRound;
pub type Id = u64;
@ -49,7 +49,7 @@ pub struct InternalTask {
pub id : Id,
pub name : String,
pub tags : HashSet<String>,
pub dependencies : HashSet<Id>,
pub dependencies : BTreeSet<Id>,
pub priority : Priority,
pub due : Option<chrono::NaiveDateTime>,
pub created : chrono::NaiveDateTime,