« blib/lib/UR/Namespace/Command/Old/Info.pm
↑ Summary
blib/lib/UR/BoolExpr/Template/PropertyComparison/NotIn.pm »
| 1 | |||
| 2 | package UR::BoolExpr::Template::PropertyComparison::In; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | UR::Object::Type->define( | ||
| 10 | class_name => __PACKAGE__, | ||
| 11 | is => ['UR::BoolExpr::Template::PropertyComparison'], | ||
| 12 | doc => "Returns true if any of the property's values appears in the comparison value list", | ||
| 13 | ); | ||
| 14 | |||
| 15 | sub _compare { | ||
| 16 | my ($class,$comparison_values,@property_values) = @_; | ||
| 17 | |||
| 18 | if (@property_values == 1 and ref($property_values[0]) eq 'ARRAY') { | ||
| 19 | @property_values = @{$property_values[0]}; | ||
| 20 | } | ||
| 21 | |||
| 22 | # undef should match missing values, which will be sorted at the end - the sorter in | ||
| 23 | # UR::BoolExpr::resolve() takes care of the sorting for us | ||
| 24 | if (! @property_values and !defined($comparison_values->[-1])) { | ||
| 25 | return 1; | ||
| 26 | } | ||
| 27 | |||
| 28 | my($pv_idx, $cv_idx); | ||
| 29 | no warnings; | ||
| 30 | my $sorter = sub { return $property_values[$pv_idx] cmp $comparison_values->[$cv_idx] }; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | # Binary search within @$comparison_values | ||
| 34 | for ( $pv_idx = 0; $pv_idx < @property_values; $pv_idx++ ) { | ||
| 35 | my $cv_min = 0; | ||
| 36 | my $cv_max = $#$comparison_values; | ||
| 37 | do { | ||
| 38 | $cv_idx = ($cv_min + $cv_max) >> 1; | ||
| 39 | my $result = &$sorter; | ||
| 40 | if (!$result) { | ||
| 41 | return 1; | ||
| 42 | } elsif ($result > 0) { | ||
| 43 | $cv_min = $cv_idx + 1; | ||
| 44 | } else { | ||
| 45 | $cv_max = $cv_idx - 1; | ||
| 46 | } | ||
| 47 | } until ($cv_min > $cv_max); | ||
| 48 | } | ||
| 49 | return ''; | ||
| 50 | } | ||
| 51 | |||
| 52 | 1; | ||
| 53 | |||
| 54 | =pod | ||
| 55 | |||
| 56 | =head1 NAME | ||
| 57 | |||
| 58 | UR::BoolExpr::Template::PropertyComparison::In - perform an In test | ||
| 59 | |||
| 60 | =head1 DESCRIPTION | ||
| 61 | |||
| 62 | Returns true if any of the property's values appears in the comparison value list. | ||
| 63 | Think of 'in' as short for 'intersect', and not just SQL's 'IN' operator. | ||
| 64 | |||
| 65 | =cut |