[pve-devel] [RFC common 2/6] Add array_intersect and array_unique functions
Stefan Reiter
s.reiter at proxmox.com
Wed Jul 17 15:03:44 CEST 2019
Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---
src/PVE/Tools.pm | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 4dd073f..c31ebeb 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -1649,4 +1649,33 @@ sub dev_t_minor($) {
return (($dev_t >> 12) & 0xfff00) | ($dev_t & 0xff);
}
+# Given an array of array refs [ \[a b c], \[a b b], \[e b a] ]
+# Returns the intersection of elements as a single array [a b]
+sub array_intersect {
+ my ($arrays) = @_;
+
+ return [] if @$arrays == 0;
+ return $arrays->[0] if @$arrays == 1;
+
+ my $return_arr;
+ @$return_arr = array_unique(@{$arrays->[0]});
+ for my $i (1 .. $#$arrays) {
+ my %count = ();
+ foreach my $element (@$return_arr, array_unique(@{$arrays->[$i]})) {
+ $count{$element}++;
+ }
+ $return_arr = [];
+ foreach my $element (keys %count) {
+ push @$return_arr, $element if $count{$element} > 1;
+ }
+ }
+
+ return $return_arr;
+}
+
+sub array_unique {
+ my %seen = ();
+ return grep { ! $seen{ $_ }++ } @_;
+}
+
1;
--
2.20.1
More information about the pve-devel
mailing list