From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pve-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 0EF951FF164 for <inbox@lore.proxmox.com>; Fri, 20 Jun 2025 16:31:35 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6D817DA9E; Fri, 20 Jun 2025 16:32:04 +0200 (CEST) From: Daniel Kral <d.kral@proxmox.com> To: pve-devel@lists.proxmox.com Date: Fri, 20 Jun 2025 16:31:09 +0200 Message-Id: <20250620143148.218469-2-d.kral@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250620143148.218469-1-d.kral@proxmox.com> References: <20250620143148.218469-1-d.kral@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.010 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH common v2 1/1] introduce HashTools module X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com> Add a new package PVE::HashTools to provide helpers for common operations done on hashes. These initial helper subroutines implement basic set operations done on hash sets, i.e. hashes with elements set to a true value, e.g. 1. Signed-off-by: Daniel Kral <d.kral@proxmox.com> --- changes since v1: - moved from pve-ha-manager PVE::HA::Tools to pve-common as PVE::HashTools - improved implementations - added documentation src/Makefile | 1 + src/PVE/HashTools.pm | 101 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/PVE/HashTools.pm diff --git a/src/Makefile b/src/Makefile index 2d8bdc4..ee114d1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -17,6 +17,7 @@ LIB_SOURCES = \ Daemon.pm \ Exception.pm \ Format.pm \ + HashTools.pm \ INotify.pm \ JSONSchema.pm \ Job/Registry.pm \ diff --git a/src/PVE/HashTools.pm b/src/PVE/HashTools.pm new file mode 100644 index 0000000..463fe7c --- /dev/null +++ b/src/PVE/HashTools.pm @@ -0,0 +1,101 @@ +package PVE::HashTools; + +use strict; +use warnings; + +=head1 NAME + +PVE::HashTools - Helpers for Hashes + +=head1 DESCRIPTION + +This packages provides helpers for common operations on hashes. + +Even though these operations' implementation are often one-liners, they are +meant to improve code readability by stating a operation name instead of the +more verbose implementation. + +=cut + +=head1 FUNCTIONS + +=cut + +=head3 set_intersect($hash1, $hash2) + +Returns a hash set of the intersection of the hash sets C<$hash1> and +C<$hash2>, i.e. the elements that are both in C<$hash1> and C<$hash2>. + +The hashes C<$hash1> and C<$hash2> are expected to be hash sets, i.e. +key-value pairs are always set to C<1> or another truthy value. + +=cut + +sub set_intersect : prototype($$) { + my ($hash1, $hash2) = @_; + + my $result = { map { $hash1->{$_} && $hash2->{$_} ? ($_ => 1) : () } keys %$hash1 }; + + return $result; +} + +=head3 set_difference($hash1, $hash2) + +Returns a hash set of the set difference between the hash sets C<$hash1> and +C<$hash2>, i.e. the elements that are in C<$hash1> without the elements that +are in C<$hash2>. + +The hashes C<$hash1> and C<$hash2> are expected to be hash sets, i.e. +key-value pairs are always set to C<1> or another truthy value. + +=cut + +sub set_difference : prototype($$) { + my ($hash1, $hash2) = @_; + + my $result = { map { $hash2->{$_} ? () : ($_ => 1) } keys %$hash1 }; + + return $result; +} + +=head3 set_union($hash1, $hash2) + +Returns a hash set of the union of the hash sets C<$hash1> and C<$hash2>, i.e. +the elements that are in either C<$hash1> or C<$hash2>. + +The hashes C<$hash1> and C<$hash2> are expected to be hash sets, i.e. +key-value pairs are always set to C<1> or another truthy value. + +=cut + +sub set_union : prototype($$) { + my ($hash1, $hash2) = @_; + + my $result = { map { $_ => 1 } keys %$hash1, keys %$hash2 }; + + return $result; +} + +=head3 sets_are_disjoint($hash1, $hash2) + +Checks whether the two given hash sets C<$hash1> and C<$hash2> are disjoint, +i.e. have no common element in both of them. + +The hashes C<$hash1> and C<$hash2> are expected to be hash sets, i.e. +key-value pairs are always set to C<1> or another truthy value. + +Returns C<1> if they are disjoint, C<0> otherwise. + +=cut + +sub sets_are_disjoint : prototype($$) { + my ($hash1, $hash2) = @_; + + for my $key (keys %$hash1) { + return 0 if $hash2->{$key}; + } + + return 1; +} + +1; -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel