~ubuntu-branches/ubuntu/raring/rakudo/raring

« back to all changes in this revision

Viewing changes to src/core/EnumMap.pm

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2012-11-29 01:00:04 UTC
  • mfrom: (7.1.5 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121129010004-3vmbbb2e53up4u14
Tags: 2012.10-1build1
Rebuild with the current version of the Not Quite Perl compiler.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
    #   has $!storage;         # Parrot Hash PMC of key->value mappings
4
4
 
5
5
    multi method Bool(EnumMap:D:) {
6
 
        nqp::p6bool(pir::defined($!storage) ?? nqp::elems($!storage) !! 0)
 
6
        nqp::p6bool(nqp::defined($!storage) ?? nqp::elems($!storage) !! 0)
7
7
    }
8
8
    method elems(EnumMap:D:) {
9
 
        pir::defined($!storage) ?? nqp::p6box_i(nqp::elems($!storage)) !! 0
 
9
        nqp::defined($!storage) ?? nqp::p6box_i(nqp::elems($!storage)) !! 0
10
10
    }
11
11
 
12
12
    multi method ACCEPTS(EnumMap:D: Any $topic) {
16
16
    multi method ACCEPTS(EnumMap:D: Cool:D $topic) {
17
17
        so self.exists($topic);
18
18
    }
 
19
 
 
20
    multi method ACCEPTS(EnumMap:D: Positional $topic) {
 
21
        so self.exists($topic.any);
 
22
    }
 
23
 
 
24
    multi method ACCEPTS(EnumMap:D: Regex $topic) {
 
25
        so self.keys.any.match($topic);
 
26
    }
19
27
    
20
 
    proto method exists(|$) {*}
21
 
    multi method exists(EnumMap:D: Str:D \$key) {
 
28
    proto method exists(|) {*}
 
29
    multi method exists(EnumMap:D: Str:D \key) {
22
30
        nqp::p6bool(
23
 
            pir::defined($!storage)
24
 
            && nqp::existskey($!storage, nqp::unbox_s($key))
 
31
            nqp::defined($!storage)
 
32
            && nqp::existskey($!storage, nqp::unbox_s(key))
25
33
        )
26
34
    }
27
 
    multi method exists(EnumMap:D: \$key) {
 
35
    multi method exists(EnumMap:D: \key) {
28
36
        nqp::p6bool(
29
 
            pir::defined($!storage)
30
 
            && nqp::existskey($!storage, nqp::unbox_s($key.Stringy))
 
37
            nqp::defined($!storage)
 
38
            && nqp::existskey($!storage, nqp::unbox_s(key.Stringy))
31
39
        )
32
40
    }
33
41
 
34
42
    multi method perl(EnumMap:D:) {
35
 
        'EnumMap.new('
 
43
        self.^name ~ '.new('
36
44
            ~ self.keys.map({ .perl ~ ', ' ~ self.at_key($_).perl ~ ', '}).join
37
45
            ~ ')';
38
46
    }
44
52
    method kv()     { self.pairs.map( { $_.kv } ) }
45
53
    method values() { self.pairs.map( { $_.value } ) }
46
54
    method pairs() {
47
 
        return unless pir::defined($!storage);
 
55
        return unless nqp::defined($!storage);
48
56
        gather {
49
57
            my Mu $iter := nqp::iterator($!storage);
50
58
            my Mu $pair;
74
82
            !! Any
75
83
    }
76
84
 
77
 
    method STORE_AT_KEY(\$key, Mu \$value) is rw {
78
 
        pir::defined($!storage) ||
79
 
            nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
80
 
        nqp::bindkey($!storage, nqp::unbox_s($key.Str), $value)
 
85
    method STORE_AT_KEY(\key, Mu \value) is rw {
 
86
        nqp::defined($!storage) ||
 
87
            nqp::bindattr(self, EnumMap, '$!storage', nqp::hash());
 
88
        nqp::bindkey($!storage, nqp::unbox_s(key.Str), value)
81
89
    }
82
90
    
83
91
    method Capture() {
88
96
    
89
97
    method FLATTENABLE_LIST() { nqp::list() }
90
98
    method FLATTENABLE_HASH() {
91
 
        pir::defined($!storage) ||
 
99
        nqp::defined($!storage) ||
92
100
            nqp::bindattr(self, EnumMap, '$!storage', nqp::hash());
93
101
        $!storage
94
102
    }
97
105
        self.pairs.fmt($format, $sep);
98
106
    }
99
107
    
100
 
    method hash(\$self:) is rw {
101
 
        $self
 
108
    method hash(\SELF:) is rw {
 
109
        SELF
102
110
    }
103
111
}
104
112
 
105
 
multi sub infix:<eqv>(EnumMap $a, EnumMap $b) {
 
113
multi sub infix:<eqv>(EnumMap:D $a, EnumMap:D $b) {
106
114
    if +$a != +$b { return Bool::False }
107
115
    for $a.kv -> $k, $v {
108
116
        unless $b.exists($k) && $b{$k} eqv $v {