前提
Binary searchを使いたいので、VectorではなくBtreeSetをcollectionとして使いたいです。
自分で定義したEntry型は外部モジュールなのでderive macroが使えない状況を想定していただけるとありがたいです。
実現したいこと
- BtreeSetに対してreplace関数によってidと合致したものは上書きをする,
IDがないものに対しては追加する。
- BtreeSetへの追加で自動的にordがされる。
ordの実装が上手できなくて困っています。
何かわかることがあればぜひご教授お願いします。
Code
外部モジュール
#[derive(Debug, Clone, Hash, PartialEq, Eq)] struct Entry { id: String, value: BigDecimal } impl Entry { fn new(id: String, value: BigDecimal) -> Entry { Entry { id, value } } }
##main.rs
use bigdecimal::BigDecimal; use std::collections::BTreeSet; use std::cmp::Ordering; #[derive(Debug)] struct WrapEntry(Entry); impl WrapEntry { fn new(e: Entry) -> WrapEntry { WrapEntry(e) } } impl Ord for WrapEntry { fn cmp(&self, other: &Self) -> Ordering { if self.0.id == other.0.id { Ordering::Equal } else { match self.0.value.cmp(&other.0.value) { Ordering::Equal => Ordering::Greater, Ordering::Greater => Ordering::Greater, Ordering::Less => Ordering::Less, } } } } impl PartialOrd for WrapEntry { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(&other)) } } impl Eq for WrapEntry {} impl PartialEq for WrapEntry { fn eq(&self, other: &Self) -> bool { self.0.id == other.0.id } } #[tokio::main] async fn main() -> anyhow::Result<()> { println!("Start"); let mut set: BTreeSet<WrapEntry> = BTreeSet::new(); let a1 = WrapEntry::new(Entry::new("aaa".to_string(), BigDecimal::from(100))); let a2 = WrapEntry::new(Entry::new("aaa".to_string(), BigDecimal::from(103))); let a3 = WrapEntry::new(Entry::new("aaa".to_string(), BigDecimal::from(97))); let b1 = WrapEntry::new(Entry::new("bbb".to_string(), BigDecimal::from(100))); let b2 = WrapEntry::new(Entry::new("bbb".to_string(), BigDecimal::from(106))); let b3 = WrapEntry::new(Entry::new("bbb".to_string(), BigDecimal::from(94))); let c1 = WrapEntry::new(Entry::new("ccc".to_string(), BigDecimal::from(100))); let c2 = WrapEntry::new(Entry::new("ccc".to_string(), BigDecimal::from(110))); let c3 = WrapEntry::new(Entry::new("ccc".to_string(), BigDecimal::from(84))); println!("{:?}", set.replace(a1)); println!("{:?}", set.replace(b2)); println!("{:?}", set.replace(c1)); println!("{:?}", set.replace(b3)); println!("{:?}", set.replace(a2)); println!("{:?}", set.replace(c2)); println!("{:?}", set.replace(c3)); println!("{:?}", set.replace(a3)); println!("{:?}", set.replace(b1)); println!("xxxxxxxxxxxxxxxxxx"); for i in set.iter() { println!("{:?}", i); } Ok(()) }
結果
idが上手く認識されないです。
Start None None None None Some(WrapEntry(Entry { id: "aaa", value: BigDecimal("100") })) Some(WrapEntry(Entry { id: "ccc", value: BigDecimal("100") })) None Some(WrapEntry(Entry { id: "aaa", value: BigDecimal("103") })) Some(WrapEntry(Entry { id: "bbb", value: BigDecimal("94") })) xxxxxxxxxxxxxxxxxxxxx WrapEntry(Entry { id: "ccc", value: BigDecimal("84") }) WrapEntry(Entry { id: "bbb", value: BigDecimal("100") }) WrapEntry(Entry { id: "aaa", value: BigDecimal("97") }) WrapEntry(Entry { id: "ccc", value: BigDecimal("110") }) WrapEntry(Entry { id: "bbb", value: BigDecimal("106") })
試したこと
- 自分のアイディアはEntry型をWrapしてOrdを実装することでWrapEntry型をOrdできるようにしたいと思ってます。
idが違う場合に違うobjectと認識させるためにはどのようにしたらいいか教えてほしいです。
よろしくお願いします。
回答をしたいのですが、実現したいことが分からないので質問させて下さい。
1.
> BtreeSetへの追加で自動的にordがされる。
とはどういう意味でしょう。
2.
Entryに対してどのような順序関係を導入しようとしていますか。例えば以下の4つの `Entry` を小さい順に並べるとどうなりますか。
`Entry("aaa", 1)`, `Entry("aaa", 2)`, `Entry("bbb", 1)`, `Entry("bbb", 2)`