ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
Revision: 1.2
Committed: Fri Sep 17 15:40:26 1999 UTC (25 years, 7 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.1: +3 -5 lines
Log Message:
Working HashDB

File Contents

# User Rev Content
1 williamc 1.1 #
2     # HashDB.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7     #
8     # Interface
9     # ---------
10     # new() : A new HashDB object
11     # setdata(data, @keys) : set a data item to the given keys
12     # getdata(@keys) : return all data items that match the given keys
13     # deletedata(@keys) : detete all data items that match the given keys
14     # items() : return the number of seperate items in the store
15    
16     package Utilities::HashDB;
17     require 5.001;
18    
19     sub new {
20     my $class=shift;
21     $self={};
22     bless $self, $class;
23     $self->{datahash}=();
24     $self->{datakey}=();
25     return $self;
26     }
27    
28     sub setdata {
29     my $self=shift;
30     my $data=shift;
31     my @keys=@_;
32    
33     push @{$self->{datakey}}, [ @keys ];
34     push @{$self->{datahash}}, $data;
35     }
36    
37     sub items {
38     my $self=shift;
39     return $#{$self->{datahash}};
40     }
41    
42    
43     sub getdata {
44     my $self=shift;
45     my @keys=@_;
46    
47     my @data=();
48     my @match=$self->_match(@keys);
49     foreach $d ( @match ) {
50     push @data, $self->{datahash}[$d];
51     }
52     return @data;
53     }
54    
55     sub deletedata {
56     my $self=shift;
57     my @keys=@_;
58    
59     # first get all the keys we want to delete
60     my @match=$self->_match(@keys);
61     foreach $i ( @match ) {
62     splice ( @{$self->{datahash}}, $i, 1 );
63     splice ( @{$self->{datakey}}, $i, 1 );
64     }
65     }
66    
67     # ------------------- Support Routines ------------
68     # returns list of array indices of items matching the keys
69     sub _match {
70     my $self=shift;
71     my @keys=@_;
72    
73     my $i;
74     my @matchold;
75     my @matchnew;
76     # At the start everything matches
77     for ( $i=0; $i<=$self->items(); $i++ ) {
78     if ( $#{$self->{datakey}[$i]} >= $#keys ) {
79     push @matchold, $i;
80     }
81     }
82    
83     # now test against keys
84     for ( $i=0; $i <= $#keys; $i++ ) {
85     undef @matchnew;
86     foreach $dn ( @matchold ){
87 williamc 1.2 if ( $self->{datakey}[$dn][$i] eq $keys[$i] ) {
88     push @matchnew,$dn;
89     }
90 williamc 1.1 }
91     @matchold=@matchnew;
92     }
93     return @matchold; # list of array indecies
94     }
95