NAME
Data::Omap - Perl module to implement ordered mappings
SYNOPSIS
use Data::Omap;
# Simple OO style
my $omap = Data::Omap->new( [{a=>1},{b=>2},{c=>3}] );
$omap->set( a => 0 );
$omap->add( b2 => 2.5, 2 ); # insert at position 2 (between b and c)
my $value = $omap->get_values( 'c' ); # 3
my @keys = $omap->get_keys(); # (a, b, b2, c)
my @values = $omap->get_values(); # (0, 2, 2.5, 3)
my @subset = $omap->get_values(qw(c b)); # (2, 3) (values are data-ordered)
# Tied style
my %omap;
# recommend saving an object reference, too.
my $omap = tie %omap, 'Data::Omap', [{a=>1},{b=>2},{c=>3}];
$omap{ a } = 0;
$omap->add( b2 => 2.5, 2 ); # there's no tied hash equivalent
my $value = $omap{ c };
my @keys = keys %omap; # $omap->get_keys() is faster
my @values = values %omap; # $omap->get_values() is faster
my @slice = @omap{qw(c b)}; # (3, 2) (slice values are parameter-ordered)
# There are more methods/options, see below.
DESCRIPTION
This module implements the Data::Omap class. Objects in this class are ordered mappings, i.e., they are hashes in which the key/value pairs are in order. This is defined in shorthand as !!omap
in the YAML tag repository: http://yaml.org/type/omap.html.
The keys in Data::Omap objects are unique, like regular hashes.
A closely related class, Data::Pairs, implements the YAML !!pairs
data type, http://yaml.org/type/pairs.html. Data::Pairs objects are also ordered sequences of key:value pairs but they allow duplicate keys.
While ordered mappings are in order, they are not necessarily in a particular order, i.e., they are not necessarily sorted in any way. They simply have a predictable set order (unlike regular hashes whose key/value pairs are in no set order).
By default, Data::Omap will add new key/value pairs at the end of the mapping, but you may request that they be merged in a particular order with the order()
class method.
However, even though Data::Omap will honor the requested order, it will not attempt to keep the mapping in that order. By passing position values to the set()
and add()
methods, you may insert new pairs anywhere in the mapping and Data::Omap will not complain.
IMPLEMENTATION
Normally, the underlying structure of an OO object is encapsulated and not directly accessible (when you play nice). One key implementation detail of Data::Omap is the desire that the underlying ordered mapping data structure (an array of single-key hashes) be publically maintained as such and directly accessible, if desired.
To that end, no attributes but the data itself are stored in the objects. In the current version, that is why order()
is a class method rather than an object method. In the future, inside-out techniques may be used to enable object-level ordering.
This data structure is inefficient in several ways as compared to regular hashes: rather than one hash, it contains a separate hash per key/value pair; because it's an array, key lookups (in the current version) have to loop through it.
The advantage if using this structure is simply that it "natively" matches the structure defined in YAML. So if the (unblessed) structure is dumped using YAML (or perhaps JSON), it may be read as is by another program, perhaps in another language. It is true that this could be accomplished by passing the object through a formatting routine, but I wanted to see first how this implementation might work.
VERSION
Data::Omap version 0.03
CLASS METHODS
Data::Omap->new();
Constructs a new Data::Omap object.
Accepts array ref containing single-key hash refs, e.g.,
my $omap = Data::Omap->new( [ { a => 1 }, { b => 2 }, { c => 3 } ] );
When provided, this data will be loaded into the object.
Returns a reference to the Data::Omap object.
Data::Omap->order();
When ordering is ON, new key/value pairs will be added in the specified order. When ordering is OFF (the default), new pairs will be added to the end of the mapping.
When called with no parameters, order()
returns the current code reference (if ordering is ON) or a false value (if ordering is OFF); it does not change the ordering.
Data::Omap->order(); # leaves ordering as is
When called with the null string, ''
, ordering is turned OFF.
Data::Omap->order( '' ); # turn ordering OFF (the default)
Otherwise, accepts the predefined orderings: 'na', 'nd', 'sa', 'sd', 'sna', and 'snd', or a custom code reference, e.g.
Data::Omap->order( 'na' ); # numeric ascending
Data::Omap->order( 'nd' ); # numeric ascending
Data::Omap->order( 'sa' ); # string ascending
Data::Omap->order( 'sd' ); # string descending
Data::Omap->order( 'sna' ); # string/numeric ascending
Data::Omap->order( 'snd' ); # string/numeric descending
Data::Omap->order( sub{ int($_[0]/100) < int($_[1]/100) } ); # code
The predefined orderings, 'na' and 'nd', compare keys as numbers. The orderings, 'sa' and 'sd', compare keys as strings. The orderings, 'sna' and 'snd', compare keys as numbers when they are both numbers, as strings otherwise.
When defining a custom ordering, the convention is to use the operators <
or lt
between (functions of) $_[0]
and $_[1]
for ascending and between $_[1]
and $_[0]
for descending.
Returns the code reference if ordering is ON, a false value if OFF.
Note, when object-level ordering is implemented, it is expected that the class-level option will still be available. In that case, any new objects will inherite the class-level ordering unless overridden at the object level.
OBJECT METHODS
$omap->set( $key => $value[, $pos] );
Sets the value if $key
exists; adds a new key/value pair if not.
Accepts $key
, $value
, and optionally, $pos
.
If $pos
is given, and there is a key/value pair at that position, it will be set to $key
and $value
, even if the key is different. For example:
my $omap = Data::Omap->new( [{a=>1},{b=>2}] );
$omap->set( c => 3, 0 ); # omap is now [{c=>3},{b=>2}]
(As implied by the example, positions start at 0.)
If $pos
is given, and there isn't a pair there, a new pair is added there (perhaps overriding a defined ordering).
If $pos
is not given, the key will be located and if found, the value set. If the key is not found, a new pair is added to the end or merged according to the defined order()
.
Note that set()
will croak if a duplicate key would result. This would only happen if $pos
is given and the $key
is found--but not at that position.
Returns $value
(as a nod toward $hash{$key}=$value, which "returns" $value).
$omap->get_values( [$key[, @keys]] );
Get a value or values.
Regardless of parameters, if the object is empty, undef is returned in scalar context, an empty list in list context.
If no paramaters, gets all the values. In scalar context, gives number of values in the object.
my $omap = Data::Omap->new( [{a=>1},{b=>2},{c=>3}] );
my @values = $omap->get_values(); # (1, 2, 3)
my $howmany = $omap->get_values(); # 3
If one key is given, that value is returned--regardless of context--or if not found, undef
.
@values = $omap->get_values( 'b' ); # (2)
my $value = $omap->get_values( 'b' ); # 2
If multiple keys given, their values are returned in the order found in the object, not the order of the given keys (unlike hash slices which return values in the order requested).
In scalar context, gives the number of values found, e.g.,
@values = $omap->get_values( 'c', 'b', 'A' ); # (2, 3)
$howmany = $omap->get_values( 'c', 'b', 'A' ); # 2
The hash slice behavior is available if you use tie
, see below.
$omap->add( $key => $value[, $pos] );
Adds a key/value pair to the object.
Accepts $key
, $value
, and optionally, $pos
.
If $pos
is given, the key/value pair will be added (inserted) there (possibly overriding a defined order), e.g.,
my $omap = Data::Omap->new( [{a=>1},{b=>2}] );
$omap->add( c => 3, 1 ); # omap is now [{a=>1},{c=>3},{b=>2}]
(Positions start at 0.)
If $pos
is not given, a new pair is added to the end or merged according to the defined order()
.
Note that add()
will croak if a duplicate key would result, i.e., if the key being added is already in the object.
Returns $value
.
$omap->_add_ordered( $key => $value );
Private routine used by set()
and add()
.
Accepts $key
and $value
.
Adds a new key/value pair to the end or merged according to the defined order()
.
This routine should not be called directly, because it does not check for duplicates.
Has no defined return value.
$omap->get_pos( @keys );
Gets positions where keys are found.
Accepts one or more keys.
If one key is given, returns the position or undef (if key not found), regardless of context, e.g.,
my $omap = Data::Omap->new( [{a=>1},{b=>2},{c=>3}] );
my @pos = $omap->get_pos( 'b' ); # (1)
my $pos = $omap->get_pos( 'b' ); # 1
If multiple keys, returns a list of hash refs in list context, the number of keys found in scalar context. The positions are listed in the order that the keys were given (rather than in numerical order), e.g.,
@pos = $omap->get_pos( 'c', 'b' ); # @pos is ({c=>2},{b=>1})
my $howmany = $omap->get_pos( 'A', 'b', 'c' ); # $howmany is 2
Returns undef/()
if no keys given or object is empty.
$omap->get_keys( @keys );
Gets keys.
Accepts zero or more keys. If no keys are given, returns all the keys in the object (list context) or the number of keys (scalar context), e.g.,
my $omap = Data::Omap->new( [{a=>1},{b=>2},{c=>3}] );
my @keys = $omap->get_keys(); # @keys is (a, b, c)
my $howmany = $omap->get_keys(); # $howmany is 3
If one or more keys are given, returns all the keys that are found (list) or the number found (scalar). Keys returned are listed in the order found in the object, e.g.,
@keys = $omap->get_keys( 'c', 'b', 'A' ); # @keys is (b, c)
$howmany = $omap->get_keys( 'c', 'b', 'A' ); # $howmany is 2
$omap->get_array( @keys );
Gets an array of key/value pairs.
Accepts zero or more keys. If no keys are given, returns a list of all the key/value pairs in the object (list context) or an array reference to that list (scalar context), e.g.,
my $omap = Data::Omap->new( [{a=>1},{b=>2},{c=>3}] );
my @array = $omap->get_array(); # @array is ({a=>1}, {b=>2}, {c=>3})
my $aref = $omap->get_array(); # $aref is [{a=>1}, {b=>2}, {c=>3}]
If one or more keys are given, returns a list of key/value pairs for all the keys that are found (list) or an aref to that list (scalar). Pairs returned are in the order found in the object, e.g.,
@array = $omap->get_array( 'c', 'b', 'A' ); # @array is ({b->2}, {c=>3})
$aref = $omap->get_array( 'c', 'b', 'A' ); # @aref is [{b->2}, {c=>3}]
Note, conceivably this method might be used to make a copy (unblessed) of the object, but it would not be a deep copy (if values are references, the references would be copied, not the referents).
$omap->firstkey();
Expects no parameters. Returns the first key in the object (or undef if object is empty).
This routine supports the tied hash FIRSTKEY method.
$omap->nextkey( $lastkey );
Accepts one parameter, the last key gotten from FIRSTKEY or NEXTKEY.
Returns the next key in the object.
This routine supports the tied hash NEXTKEY method.
$omap->exists( $key );
Accepts one key.
Returns true if key is found in object, false if not.
This routine supports the tied hash EXISTS method, but may reasonably be called directly, too.
$omap->delete( $key );
Accepts one key. If key is found, removes the key/value pair from the object.
Returns the value from the deleted pair.
This routine supports the tied hash DELETE method, but may be called directly, too.
$omap->clear();
Expects no parameters. Removes all key/value pairs from the object.
Returns an empty list.
This routine supports the tied hash CLEAR method, but may be called directly, too.