Quiq/Hash/Ordered.pm

filter: Statementstmt 100% 88 / 88 Branchbran 89.2% 25 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 13 / 13 totaltotal 97.9% 144 / 147 CC 18 SCAR 10.2
CC
18
Cov
97%
delete
5.0
get
4.0
setDelete
4.0
CRAP 2.8
« Quiq/Math.pm ↑ Summary Quiq/Gd/Component/ColorLegend.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Hash::Ordered - Hash mit geordneten Elementen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen geordneten Hash. Ein
16 geordneter Hash ist ein Hash, bei dem die Schlüssel/Wert-Paare
17 eine definierte Reihenfolge haben. Initial ist dies die
18 Hinzufügereihenfolge.
19  
20 =cut
21  
22 # -----------------------------------------------------------------------------
23  
24 package Quiq::Hash::Ordered;
25 7 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 7 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 7 our $VERSION = '1.240';
32  
33 7 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # -----------------------------------------------------------------------------
36  
37 =head1 METHODS
38  
39 =head2 Konstruktor
40  
41 =head3 new() - Konstruktor
42  
43 =head4 Synopsis
44  
45 $oh = $class->new(@keyVal);
46  
47 =head4 Description
48  
49 Instantiiere einen geordneten Hash, setze die betreffenden
50 Schlüssel/Wert-Paare und liefere eine Referenz auf dieses Objekt
51 zurück.
52  
53 =cut
54  
55 # -----------------------------------------------------------------------------
56  
57 17 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
58 17 my $class = shift;
59  
60 17 my $self = bless [Quiq::Hash->new->unlockKeys,[]],$class;
61 17 $self->set(@_);
62  
63 17 return $self;
64 }
65  
66 # -----------------------------------------------------------------------------
67  
68 =head2 Getter/Setter
69  
70 =head3 get() - Liefere Werte
71  
72 =head4 Synopsis
73  
74 @arr|$val = $oh->get(@keys);
75  
76 =head4 Description
77  
78 Liefere die Liste der Werte zu den angebenen Schlüsseln. Ist kein
79 Schlüssel angegeben, liefere alle Werte. In Skalarkontext liefere
80 keine Liste, sondern den Wert des ersten Schlüssels.
81  
82 =cut
83  
84 # -----------------------------------------------------------------------------
85  
86 15 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod documented
87 15 my $self = shift;
88  
89 15 my $hash = $self->[0];
90 15 ▶ my $keys = @_? \@_: $self->[1];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 15 0 15
91  
92 15 my @arr;
93 15 for (@$keys) {
94 17 push @arr,$hash->{$_};
95 }
96  
97 15 ▶ return wantarray? @arr: $arr[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 13 15
98 }
99  
100 # -----------------------------------------------------------------------------
101  
102 =head3 set() - Setze Schlüssel/Wert-Paare
103  
104 =head4 Synopsis
105  
106 $oh->set(@keyVal);
107  
108 =head4 Returns
109  
110 nichts
111  
112 =head4 Description
113  
114 Setze die angegebenen Schlüssel/Wert-Paare.
115  
116 =cut
117  
118 # -----------------------------------------------------------------------------
119  
120 30 ▶sub set {
Subroutine set: called CC=3 SCAR=11.0
Pod documented
121 30 my $self = shift;
122  
123 30 my ($hash,$arr) = @$self;
124  
125 30 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 70 30 100
126 70 my $key = shift;
127 70 my $val = shift;
128  
129 70 ▶ push @$arr,$key if !exists $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{$key} 64 6 70
130 70 $hash->{$key} = $val;
131 }
132  
133 30 return;
134 }
135  
136 # -----------------------------------------------------------------------------
137  
138 =head3 setDelete() - Setze bzw. lösche Schlüssel/Wert-Paare
139  
140 =head4 Synopsis
141  
142 $oh->setDelete(@keyVal);
143  
144 =head4 Description
145  
146 Setze die angegebenen Schlüssel auf die angegebenen Werte. Wenn $val
147 undef ist, lösche den betreffenden Schlüssel. Die Methode liefert
148 keinen Wert zurück.
149  
150 =cut
151  
152 # -----------------------------------------------------------------------------
153  
154 1 ▶sub setDelete {
Subroutine setDelete: called CC=4 SCAR=13.9
Pod documented
155 1 my $self = shift;
156  
157 1 my ($hash,$arr) = @$self;
158  
159 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
160 3 my $key = shift;
161 3 my $val = shift;
162  
163 3 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 1 2 3
164 1 $self->delete($key);
165 1 next;
166 }
167  
168 2 ▶ push @$arr,$key unless exists $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{$key} 1 1 2
169 2 $hash->{$key} = $val;
170 }
171  
172 1 return;
173 }
174  
175 # -----------------------------------------------------------------------------
176  
177 =head2 Miscellaneous
178  
179 =head3 clear() - Lösche Inhalt und setze Schlüssel/Wert-Paare
180  
181 =head4 Synopsis
182  
183 $obj->clear(@keyVal);
184  
185 =head4 Description
186  
187 Lösche Inhalt und setze Schlüssel/Wert-Paare. Ist kein
188 Schlüssel/Wert-Paar angegeben, wird nur der Inhalt gelöscht.
189 Die Methode liefert keinen Wert zurück.
190  
191 =cut
192  
193 # -----------------------------------------------------------------------------
194  
195 2 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
196 2 my $self = shift;
197  
198 2 $self->[0] = {};
199 2 $self->[1] = [];
200  
201 2 return $self->set(@_);
202 }
203  
204 # -----------------------------------------------------------------------------
205  
206 =head3 copy() - Kopiere Hash
207  
208 =head4 Synopsis
209  
210 $oh2 = $oh->copy;
211  
212 =head4 Description
213  
214 Kopiere Hashobjekt und liefere eine Referenz auf die Kopie zurück.
215  
216 =cut
217  
218 # -----------------------------------------------------------------------------
219  
220 1 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
221 1 my $self = shift;
222  
223 1 my %hash = %{$self->[0]};
224 1 my @arr = @{$self->[1]};
225  
226 1 return bless [\%hash,\@arr],ref $self;
227 }
228  
229 # -----------------------------------------------------------------------------
230  
231 =head3 delete() - Lösche Schlüssel
232  
233 =head4 Synopsis
234  
235 $oh->delete(@keys);
236  
237 =head4 Description
238  
239 Lösche die angegebenen Schlüssel. Die Methode liefert keinen Wert zurück.
240  
241 =cut
242  
243 # -----------------------------------------------------------------------------
244  
245 2 ▶sub delete {
Subroutine delete: called CC=5 SCAR=16.1
Pod documented
246 2 my $self = shift;
247 # @_: @keys
248  
249 2 my ($hash,$arr) = @$self;
250  
251 2 for my $key (@_) {
252 5 ▶ if (exists $hash->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$key}) 3 2 5
253 3 ▶ for (my $i = 0; $i < @$arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 5 0 5
254 5 ▶ if ($arr->[$i] eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr->[$i] eq $key) 3 2 5
255 3 splice @$arr,$i,1;
256 3 last;
257 }
258 }
259 3 delete $hash->{$key};
260 }
261 }
262  
263 2 return;
264 }
265  
266 # -----------------------------------------------------------------------------
267  
268 =head3 increment() - Inkrementiere Wert
269  
270 =head4 Synopsis
271  
272 $n = $hash->increment($key);
273  
274 =head4 Description
275  
276 Inkrementiere Wert zu Schlüssel $key und liefere das Resultat zurück.
277  
278 =cut
279  
280 # -----------------------------------------------------------------------------
281  
282 3 ▶sub increment {
Subroutine increment: called CC=1 SCAR=0.0
Pod documented
283 3 my ($self,$key) = @_;
284 3 return $self->[0]->increment($key);
285 }
286  
287 # -----------------------------------------------------------------------------
288  
289 =head3 keys() - Liefere die Liste aller Schlüssel
290  
291 =head4 Synopsis
292  
293 @keys|$keys = $oh->keys;
294  
295 =head4 Description
296  
297 Liefere die Liste der Schlüssel des Hash. In skalarem Kontext
298 liefere eine Referenz auf die Liste.
299  
300 =cut
301  
302 # -----------------------------------------------------------------------------
303  
304 18 ▶sub keys {
Subroutine keys: called CC=2 SCAR=6.9
Pod documented
305 18 my $self = shift;
306 18 my $arr = $self->[1];
307 18 ▶ return wantarray? @$arr: $arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 14 18
308 }
309  
310 # -----------------------------------------------------------------------------
311  
312 =head3 hashSize() - Anzahl der Elemente
313  
314 =head4 Synopsis
315  
316 $n = $oh->hashSize;
317  
318 =head4 Description
319  
320 Liefere die Anzahl der Elemente.
321  
322 =cut
323  
324 # -----------------------------------------------------------------------------
325  
326 2 ▶sub hashSize {
Subroutine hashSize: called CC=1 SCAR=0.0
Pod documented
327 2 my $self = shift;
328 2 return scalar @{$self->[1]};
329 }
330  
331 # -----------------------------------------------------------------------------
332  
333 =head3 unshift() - Setze Schlüssel/Wert-Paar an den Anfang
334  
335 =head4 Synopsis
336  
337 $oh->unshift($key=>$val);
338  
339 =head4 Description
340  
341 Setze das angegebene Schlüssel/Wert-Paar, sofern der Schlüssel noch nicht
342 existiert, an den Anfang. Existiert der Schlüssel, wird der Wert
343 ersetzt. Die Methode liefert keinen Wert zurück.
344  
345 =cut
346  
347 # -----------------------------------------------------------------------------
348  
349 2 ▶sub unshift {
Subroutine unshift: called CC=2 SCAR=6.9
Pod documented
350 2 my ($self,$key,$val) = @_;
351  
352 2 my ($hash,$arr) = @$self;
353 2 ▶ unshift @$arr,$key if !exists $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{$key} 1 1 2
354 2 $hash->{$key} = $val;
355  
356 2 return;
357 }
358  
359 # -----------------------------------------------------------------------------
360  
361 =head3 values() - Liefere die Liste der Werte
362  
363 =head4 Synopsis
364  
365 @arr|$arr = $oh->values;
366  
367 =head4 Description
368  
369 Liefere die Liste der Werte in Schlüsselreihenfolge. In skalarem Kontext
370 liefere eine Referenz auf die Liste.
371  
372 =cut
373  
374 # -----------------------------------------------------------------------------
375  
376 11 ▶sub values {
Subroutine values: called CC=3 SCAR=11.0
Pod documented
377 11 my $self = shift;
378  
379 11 my ($hash,$arr) = @$self;
380  
381 11 my @arr;
382 11 for (@$arr) {
383 42 push @arr,$hash->{$_};
384 }
385  
386 11 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 11 11
387 }
388  
389 # -----------------------------------------------------------------------------
390  
391 =head2 Test Methods
392  
393 =head3 exists() - Prüfe Existenz eines Schlüssels
394  
395 =head4 Synopsis
396  
397 $oh->exists($key);
398  
399 =head4 Description
400  
401 Liefere "wahr", wenn der Hash den Schlüssel $key enthält,
402 andernfalls "falsch".
403  
404 =cut
405  
406 # -----------------------------------------------------------------------------
407  
408 2 ▶sub exists {
Subroutine exists: called CC=2 SCAR=6.9
Pod documented
409 2 my $self = shift;
410 2 my $key = shift;
411 2 ▶ return exists $self->[0]->{$key}? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[0]{$key} ? : 1 1 2
412 }
413  
414 # -----------------------------------------------------------------------------
415  
416 =head1 VERSION
417  
418 1.240
419  
420 =head1 AUTHOR
421  
422 Frank Seitz, L<http://fseitz.de/>
423  
424 =head1 COPYRIGHT
425  
426 Copyright (C) 2026 Frank Seitz
427  
428 =head1 LICENSE
429  
430 This code is free software; you can redistribute it and/or modify
431 it under the same terms as Perl itself.
432  
433 =cut
434  
435 # -----------------------------------------------------------------------------
436  
437 7 1;
438  
439 # eof
« Quiq/Math.pm ↑ Summary Quiq/Gd/Component/ColorLegend.pm »