Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Efficient selection mechanism?

by choroba (Cardinal)
on Jan 14, 2014 at 14:36 UTC ( [id://1070562]=note: print w/replies, xml ) Need Help??


in reply to Efficient selection mechanism?

Just an idea using bit vectors: create a bit vector for each small integer, 1 means the integer is present in the current quadruple. Then just do logical OR an the four vectors corresponding to the selected integers and look for zeroes.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Efficient selection mechanism?
by choroba (Cardinal) on Jan 14, 2014 at 15:58 UTC
    Working code:
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use constant MAX => 20; use constant EMPTY => "\0" x ((MAX / 8) + 1); my @AoA = map [ map int rand 1 + MAX, 1 .. 4 ], 0 .. 1000; my @vectors = (EMPTY) x (MAX + 1); for my $num (0 .. MAX) { for my $pos (0 .. $#AoA) { vec($vectors[$num], $pos, 1) = grep($num == $_, @{ $AoA[$pos] +}) ? 1 : 0; } } print "@$_; " for @AoA; print "\n"; my $subarray = $AoA[0]; say "Selecting: @$subarray."; my $result = EMPTY; for my $num (@$subarray) { $result |= $vectors[$num]; } for my $i (0 .. $#AoA) { say "@{ $AoA[$i] }" unless vec($result, $i, 1); }

    Update: Runs under 0.3s for 10_000 quadruples.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1070562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-26 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found