1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package org.apache.thrift.helper;
21
22import Map;
23
24
25class ObjectSet<K:{}> {
26
27    private var _elements = new haxe.ds.ObjectMap<K,Int>();
28    private var _size : Int = 0;
29    public var size(get,never) : Int;
30
31    public function new( values : Array<K> = null) {
32        if ( values != null) {
33            for ( value in values) {
34                 add(value);
35            }
36        }
37    }
38
39    public function iterator():Iterator<K> {
40        return _elements.keys();
41    }
42
43    public function traceAll() : Void {
44        trace('$_size entries');
45        for(entry in this) {
46            var yes = contains(entry);
47            trace('- $entry, contains() = $yes');
48        }
49    }
50
51    public function add(o : K) : Bool {
52        if( _elements.exists(o)) {
53            return false;
54        }
55        _size++;
56        _elements.set(o,_size);
57        return true;
58    }
59
60    public function clear() : Void {
61        while( _size > 0) {
62            remove( _elements.keys().next());
63        }
64    }
65
66    public function contains(o : K) : Bool {
67        return _elements.exists(o);
68    }
69
70    public function isEmpty() : Bool {
71        return _size == 0;
72    }
73
74    public function remove(o : K) : Bool {
75        if (contains(o)) {
76            _elements.remove(o);
77            _size--;
78            return true;
79        } else {
80            return false;
81        }
82    }
83
84    public function toArray() : Array<K> {
85        var ret : Array<K> = new Array<K>();
86        for (key in _elements.keys()) {
87            ret.push(key);
88        }
89        return ret;
90    }
91
92    public function get_size() : Int {
93        return _size;
94    }
95}
96