From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer 3/7] tui: simplify duplicate disk checking logic
Date: Thu, 13 Jul 2023 11:49:27 +0200 [thread overview]
Message-ID: <20230713094941.475486-4-c.heiss@proxmox.com> (raw)
In-Reply-To: <20230713094941.475486-1-c.heiss@proxmox.com>
This reduces the logic from O(n^2) to O(n), which is always a good
thing.
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
proxmox-tui-installer/src/views/bootdisk.rs | 39 ++++++++++++---------
1 file changed, 23 insertions(+), 16 deletions(-)
diff --git a/proxmox-tui-installer/src/views/bootdisk.rs b/proxmox-tui-installer/src/views/bootdisk.rs
index 6ef814f..b0557a6 100644
--- a/proxmox-tui-installer/src/views/bootdisk.rs
+++ b/proxmox-tui-installer/src/views/bootdisk.rs
@@ -1,4 +1,4 @@
-use std::{cell::RefCell, marker::PhantomData, rc::Rc};
+use std::{cell::RefCell, collections::HashSet, marker::PhantomData, rc::Rc};
use cursive::{
view::{Nameable, Resizable, ViewWrapper},
@@ -536,21 +536,11 @@ fn advanced_options_view(disks: &[Disk], options: Rc<RefCell<BootdiskOptions>>)
})
.flatten();
- if let Some(disks) = options.as_ref().map(|opts| &opts.disks) {
- if disks.len() > 1 {
- for i in 0..(disks.len() - 1) {
- let check_disk = &disks[i];
- for disk in &disks[(i + 1)..] {
- if disk.index == check_disk.index {
- siv.add_layer(Dialog::info(format!(
- "cannot select same disk ({}) twice",
- disk.path
- )));
- return;
- }
- }
- }
- }
+ if let Err(duplicate) = check_for_duplicate_disks(&options.disks) {
+ siv.add_layer(Dialog::info(format!(
+ "Cannot select same disk twice: {duplicate}"
+ )));
+ return;
}
siv.pop_layer();
@@ -562,3 +552,20 @@ fn advanced_options_view(disks: &[Disk], options: Rc<RefCell<BootdiskOptions>>)
.with_name("advanced-bootdisk-options-dialog")
.max_size((120, 40))
}
+
+/// Checks a list of disks for duplicate entries, using their index as key.
+///
+/// # Arguments
+///
+/// * `disks` - A list of disks to check for duplicates.
+fn check_for_duplicate_disks(disks: &[Disk]) -> Result<(), &Disk> {
+ let mut set = HashSet::new();
+
+ for disk in disks {
+ if !set.insert(&disk.index) {
+ return Err(disk);
+ }
+ }
+
+ Ok(())
+}
--
2.41.0
next prev parent reply other threads:[~2023-07-13 9:49 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-13 9:49 [pve-devel] [PATCH installer 0/7] tui: add ZFS/Btrfs RAID setup checks Christoph Heiss
2023-07-13 9:49 ` [pve-devel] [PATCH installer 1/7] gitignore: add cd-info.test Christoph Heiss
2023-07-13 9:49 ` [pve-devel] [PATCH installer 2/7] tui: fix small typo in error message Christoph Heiss
2023-07-13 9:49 ` Christoph Heiss [this message]
2023-07-13 9:49 ` [pve-devel] [PATCH installer 4/7] tui: deserialize boot type and disk blocksize from runtime env info Christoph Heiss
2023-07-13 9:49 ` [pve-devel] [PATCH installer 5/7] tui: improve bootdisk dialog error handling Christoph Heiss
2023-07-13 9:49 ` [pve-devel] [PATCH installer 6/7] tui: add RAID setup checks for ZFS/Btrfs Christoph Heiss
2023-07-13 9:49 ` [pve-devel] [PATCH installer 7/7] tui: add tests for RAID setup checks Christoph Heiss
2023-07-21 14:20 ` [pve-devel] partially-applied: [PATCH installer 0/7] tui: add ZFS/Btrfs " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230713094941.475486-4-c.heiss@proxmox.com \
--to=c.heiss@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox