Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

rust - How to compare keys of vector in a HashMap?

I have a scenario where I have to compare the keys of vector. I have 2 values in each vector key.

I have to find a vector key in which

  1. first value of the vector should be greater than all first value of each vector key
  2. and the second value of the vector should be smaller than all second value of each vector key

Please find below sample code:

let mut queue : HashMap<Vec<u8>, Vec<u8>> = HashMap::new();

queue.insert(vec![5 as u8, queue.keys().len() as u8], vec![0]);
queue.insert(vec![10 as u8, queue.keys().len() as u8], vec![1]);
queue.insert(vec![10 as u8, queue.keys().len() as u8], vec![2]);
queue.insert(vec![3 as u8, queue.keys().len() as u8], vec![2]);
queue.insert(vec![4 as u8, queue.keys().len() as u8], vec![3]);
queue.insert(vec![6 as u8, queue.keys().len() as u8], vec![4]);

let key= queue
    .iter()
    .max_by(|a, b| {
        a.0.cmp(&b.0)
    })
    .map(|(k, _v)| k);

println!("{:?}", key);

I am getting this output Some([10, 2]). But I want Some([10, 1]) as output. `

here is my map : {[10, 1]: [1], [4, 4]: [3], [6, 5]: [4], [10, 2]: [2], [3, 3]: [2], [5, 0]: [0]}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You want a max_by comparison function which compares the first element to find which is greatest and the second to find which is smallest. Let's deal with the first criterion first: the first element should be largest.

a.0[0].cmp(&b.0[0])

a.0 and b.0, as you've likely already surmised, select the key from the key-value pair provided. Then we use ordinary [] indexing to get the first element of the vector.

Now, I'm assuming for the purposes of the question that we want lexicographic ordering, also called dictionary order. That is, you've listed two criteria, and I'm assuming the first takes precedent, so that for instance [10, 2] should be favorable to [9, 1], since the first element is bigger, despite the fact that the second is not smaller.

With that assumption in mind, we can expand our comparison. To reverse an ordering (i.e. to select a smallest rather than largest element) we can simply switch the order of arguments.

b.0[1].cmp(&a.0[1])

Then we only want to use this comparison if the first failed us, i.e. if the first was equal.

match a.0[0].cmp(&b.0[0]) {
  Ordering::Equal => b.0[1].cmp(&a.0[1]),
  x => x
}

If we put this in your max_by function, we get [10, 1], as desired.

But we can actually do one better. See, lexicographic ordering is fairly common, so common that it's built-in to Rust. We can use the method Ordering::then as follows.

a.0[0].cmp(&b.0[0]).then(b.0[1].cmp(&a.0[1]))

And this works identically to the previous example.

Complete example:

let mut queue : HashMap<Vec<u8>, Vec<u8>> = HashMap::new();

queue.insert(vec![5 as u8, queue.keys().len() as u8], vec![0]);
queue.insert(vec![10 as u8, queue.keys().len() as u8], vec![1]);
queue.insert(vec![10 as u8, queue.keys().len() as u8], vec![2]);
queue.insert(vec![3 as u8, queue.keys().len() as u8], vec![2]);
queue.insert(vec![4 as u8, queue.keys().len() as u8], vec![3]);
queue.insert(vec![6 as u8, queue.keys().len() as u8], vec![4]);

let key = queue
  .iter()
  .max_by(|a, b| {
    a.0[0].cmp(&b.0[0]).then(b.0[1].cmp(&a.0[1]))
  })
  .map(|(k, _v)| k);

println!("{:?}", key);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...