Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re-orderable keyed access structure?

by ikegami (Pope)
on Aug 14, 2004 at 09:48 UTC ( #382928=note: print w/replies, xml ) Need Help??


in reply to Re-orderable keyed access structure?

Here's a solution. Add methods to your liking.
use strict; use warnings; package BrowserUKStruct; sub IDX_HASH () { 0 } sub IDX_ARRAY () { 1 } sub NODE_IDX_VAL () { 0 } # sort() assumes this is 0. sub NODE_IDX_KEY () { 1 } sub new { my ($class) = @_; return bless([ {}, # IDX_HASH [], # IDX_ARRAY ], $class); } sub append { my $self = shift(@_); while (scalar(@_)) { my $key = shift(@_); my $val = shift(@_); my $node = [ $val, # NODE_IDX_VAL $key, # NODE_IDX_KEY ]; push(@{$self->[IDX_ARRAY]}, $node); $self->[IDX_HASH]->{$key} = $node; } } sub get_val_by_key { my ($self, $key) = @_; return $self->[IDX_HASH]->{$key}->[NODE_IDX_VAL]; } sub get_val_by_pos { my ($self, $pos) = @_; return $self->[IDX_ARRAY]->[$pos]->[NODE_IDX_VAL]; } sub get_vals { my ($self) = @_; return map { $_->[NODE_IDX_VAL] } @{$self->[IDX_ARRAY]}; } sub get_key_by_pos { my ($self, $pos) = @_; return $self->[IDX_ARRAY]->[$pos]->[NODE_IDX_KEY]; } sub get_keys { my ($self) = @_; return map { $_->[NODE_IDX_KEY] } @{$self->[IDX_ARRAY]}; } sub sort { my ($self, $cond) = @_; # $cond, if provided, must use $_[0] and $_[1] instead of $a and $b +. $cond ||= sub { $_[0] cmp $_[1] }; @{$self->[IDX_ARRAY]} = sort { &{$cond}($a->[0], $b->[0]) } @{$self +->[IDX_ARRAY]}; return $self; } sub swap { my ($self, $pos1, $pos2) = @_; my $array = $self->[IDX_ARRAY]; my $item1 = $array->[$pos1]; my $item2 = $array->[$pos2]; $array->[$pos1] = $item2; $array->[$pos2] = $item1; } package main; { my $s = BrowserUKStruct->new(); $s->append(chien => 'dog'); $s->append(chat => 'cat'); $s->append(phoque => 'seal'); $s->append(poulet => 'chicken'); printf("\@orig_vals = %s\n", join(', ', $s->get_vals())); print("\n"); $s->swap(2, 3); printf("\@t1_vals = %s\n", join(', ', $s->get_vals())); printf("\$t1_val_at2 = %s\n", $s->get_val_by_pos(2)); printf("\$t1_val_at3 = %s\n", $s->get_val_by_pos(3)); printf("\$t1_val_phoque = %s\n", $s->get_val_by_key('phoque')); printf("\$t1_val_poulet = %s\n", $s->get_val_by_key('poulet')); print("\n"); $s->sort(sub { $_[1] cmp $_[0] }); printf("\@t2_vals = %s\n", join(', ', $s->get_vals())); print("\n"); printf("\@t3_key_at0 = %s\n", join(', ', $s->get_key_by_pos(0))) +; printf("\@t3_key_at1 = %s\n", join(', ', $s->get_key_by_pos(1))) +; } __END__ output ====== @orig_vals = dog, cat, seal, chicken @t1_vals = dog, cat, chicken, seal $t1_val_at2 = chicken $t1_val_at3 = seal $t1_val_phoque = seal $t1_val_poulet = chicken @t2_vals = seal, dog, chicken, cat @t3_key_at0 = phoque @t3_key_at1 = chien

Replies are listed 'Best First'.
Re^2: Re-orderable keyed access structure?
by nothingmuch (Priest) on Aug 14, 2004 at 12:03 UTC
    You could also wrap this in a native interface:
    my $obj = new BrowserUKStruct; tie my %hash, "BrowserUKHash", $obj; tie my @array, "BrowserUKArray", $obj;
    It should be quite easy, with all the tie methods simply translating.

    You'd have to decide on a proper interface though, like is what you set also what you get (is $hash{key} an array reference of index and value?). You could also decide, for simplicities sake that the hash and array interfaces are read only, and modify the object directly.

    I think this is a wonderful example of where perl fscking kicks a**. The ability to glue an arbitrary store, like an object, into a hash and array interface, simultaneously is simply superb. Moreover, this is a good example of what tie comes to solve. By giving you the possibility of treating your hash-like and array-like data as real hashes and arrays, you learn a good lesson about the value of abstraction, and polymorphism. Well, I did, at least, when I was obsessed with tie.

    -nuffin
    zz zZ Z Z #!perl

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2020-02-18 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (74 votes). Check out past polls.

    Notices?