PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/library/builtins/sets.py

http://pyjamas.googlecode.com/
Python | 83 lines | 82 code | 0 blank | 1 comment | 0 complexity | b3afabd60107f6e80c0d410629fbc17e MD5 | raw file
 1# TODO: __hash__
 2class Set:
 3    def __init__(self, data=None):
 4        JS("""
 5        this.d = {};
 6        this.update(data);
 7        """)
 8
 9    def add(self, value):
10        JS("""    this.d[pyjslib_hash(value)] = value;""")
11
12    def clear(self):
13        JS("""    this.d = {};""")
14
15    def __contains__(self, value):
16        JS("""    return (this.d[pyjslib_hash(value)]) ? true : false;""")
17
18    def discard(self, value):
19        JS("""    delete this.d[pyjslib_hash(value)];""")
20
21    def issubset(self, items):
22        JS("""
23        for (var i in this.d) {
24            if (!items.__contains__(i)) return false;
25            }
26        return true;
27        """)
28
29    def issuperset(self, items):
30        JS("""
31        for (var i in items) {
32            if (!this.__contains__(i)) return false;
33            }
34        return true;
35        """)
36
37    def __iter__(self):
38        JS("""
39        var items=new pyjslib_List();
40        for (var key in this.d) items.append(this.d[key]);
41        return items.__iter__();
42        """)
43
44    def __len__(self):
45        JS("""
46        var size=0;
47        for (var i in this.d) size++;
48        return size;
49        """)
50
51    def pop(self):
52        JS("""
53        for (var key in this.d) {
54            var value = this.d[key];
55            delete this.d[key];
56            return value;
57            }
58        """)
59
60    def remove(self, value):
61        self.discard(value)
62
63    def update(self, data):
64        JS("""
65        if (pyjslib_isArray(data)) {
66            for (var i in data) {
67                this.d[pyjslib_hash(data[i])]=data[i];
68            }
69        }
70        else if (pyjslib_isIteratable(data)) {
71            var iter=data.__iter__();
72            var i=0;
73            try {
74                while (true) {
75                    var item=iter.next();
76                    this.d[pyjslib_hash(item)]=item;
77                }
78            }
79            catch (e) {
80                if (e != StopIteration) throw e;
81            }
82        }
83        """)