ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
Revision: 1.3
Committed: Fri Sep 17 16:56:42 1999 UTC (25 years, 7 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.2: +33 -34 lines
Log Message:
Add match method to return objects - base on DataItem objects

File Contents

# Content
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 # match(@keys) : return the full DataItem object refs that match keys
15 # items() : return the number of seperate items in the store
16
17 package Utilities::HashDB;
18 use Utilities::DataItem;
19 require 5.001;
20
21 sub new {
22 my $class=shift;
23 $self={};
24 bless $self, $class;
25 $self->{dataitems}=();
26 return $self;
27 }
28
29 sub setdata {
30 my $self=shift;
31 my $data=shift;
32 my @keys=@_;
33
34 push @{$self->{dataitems}}, Utilities::DataItem->new($data, @keys);
35 }
36
37 sub items {
38 my $self=shift;
39 return $#{$self->{dataitems}};
40 }
41
42 sub deletedata {
43 my $self=shift;
44 my @keys=@_;
45
46 # first get all the keys we want to delete
47 my @match=$self->_match(@keys);
48 foreach $i ( @match ) {
49 splice (@{$self->{dataitems}}, $i, 1 );
50 }
51 }
52
53 sub match {
54 my $self=shift;
55 my @keys=@_;
56
57 my @data=();
58 my @match=$self->_match(@keys);
59 foreach $i ( @match ) {
60 push @data, $self->{dataitems}[$i];
61 }
62 return @data;
63 }
64
65 sub getdata {
66 my $self=shift;
67 my @keys=@_;
68
69 my @data=();
70 my @match=$self->_match(@keys);
71 my $i;
72 foreach $i ( @match ) {
73 push @data, ($self->{dataitems}[$i]->data());
74 }
75 return @data;
76 }
77
78 # ------------------- Support Routines ------------
79 # returns list of array indices of items matching the keys
80
81 sub _match {
82 my $self=shift;
83 my @keys=@_;
84
85 my @matches=();
86 my $data;
87 for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
88 $data=$self->{dataitems}[$i];
89 if ( $data->match(@keys) ) {
90 push @matches, $i;
91 }
92 }
93 return @matches;
94 }