aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/EclipseCollectionsLongSetMemory.java
blob: fceb54fcd1367e39ad0444970c5dadb983e2e8a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*******************************************************************************
 * Copyright (c) 2010-2018, Gabor Bergmann, IncQueryLabs Ltd.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.viatra.runtime.matchers.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.collections.api.LongIterable;
import org.eclipse.collections.api.iterator.LongIterator;
import org.eclipse.collections.api.set.primitive.LongSet;
import org.eclipse.collections.impl.set.mutable.primitive.LongHashSet;

/**
 * @author Gabor Bergmann
 * @since 2.0
 */
public class EclipseCollectionsLongSetMemory extends LongHashSet implements ISetMemory<Long> {

    @Override
    public boolean addOne(Long value) {
        return super.add(value);
    }

    @Override
    public boolean addSigned(Long value, int count) {
        if (count == 1) return addOne(value);
        else if (count == -1) return removeOne(value); 
        else throw new IllegalStateException();
    }

    @Override
    public boolean removeOne(Long value) {
        // Kept for binary compatibility
        return ISetMemory.super.removeOne(value);
    }
    
    /**
     * @since 2.3
     */
    @Override
    public boolean removeOneOrNop(Long value) {
        return super.remove(value);
    }

    @Override
    public void clearAllOf(Long value) {
        super.remove(value);
    }

    @Override
    public int getCount(Long value) {
        return super.contains(value) ? 1 : 0;
    }
    
    @Override
    public int getCountUnsafe(Object value) {
        return value instanceof Long ? getCount((Long) value) : 0;
    }

    @Override
    public boolean containsNonZero(Long value) {
        return super.contains(value);
    }
    
    @Override
    public boolean containsNonZeroUnsafe(Object value) {
        return value instanceof Long && containsNonZero((Long) value);
    }

    @Override
    public Iterator<Long> iterator() {
        return iteratorOf(this);
    }

    @Override
    public Set<Long> distinctValues() {
        return new SetWrapper(this);
    }
    
    @Override
    public boolean isEmpty() {
        return super.isEmpty();
    }
    
    /**
     * Helper for iterating a LongIterable
     */
    public static Iterator<Long> iteratorOf(LongIterable wrapped) {
        return new Iterator<Long>() {
            LongIterator longIterator = wrapped.longIterator();

            @Override
            public boolean hasNext() {
                return longIterator.hasNext();
            }

            @Override
            public Long next() {
                return longIterator.next();
            }
        };
    }
    
    @Override
    public int hashCode() {
        return IMemoryView.hashCode(this);
    }
    @Override
    public boolean equals(Object obj) {
        return IMemoryView.equals(this, obj);
    }

    
    /**
     * Helper that presents a primitive collection as a Set view
     * @author Gabor Bergmann
     */
    public static final class SetWrapper implements Set<Long> {
        private LongSet wrapped;

        /**
         * @param wrapped
         */
        public SetWrapper(LongSet wrapped) {
            this.wrapped = wrapped;
        }

        @Override
        public int size() {
            return wrapped.size();
        }

        @Override
        public boolean isEmpty() {
            return wrapped.isEmpty();
        }

        @Override
        public boolean contains(Object o) {
            return o instanceof Long &&  wrapped.contains((Long)o);
        }

        @Override
        public Iterator<Long> iterator() {
            return iteratorOf(wrapped);
        }

        @Override
        public boolean containsAll(Collection<?> c) {
            for (Object object : c) {
                if (contains(object))
                    return true;
            }
            return false;
        }

        @Override
        public Object[] toArray() {
            return toArray(new Long[wrapped.size()]);
        }

        @Override
        @SuppressWarnings("unchecked")
        public <T> T[] toArray(T[] a) {
            int k = 0;
            LongIterator iterator = wrapped.longIterator();
            while (iterator.hasNext()) 
                a[k++] = (T) Long.valueOf(iterator.next());
            return a;
        }

        @Override
        public boolean add(Long e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean remove(Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean addAll(Collection<? extends Long> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean retainAll(Collection<?> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeAll(Collection<?> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void clear() {
            throw new UnsupportedOperationException();
        }
        
        
    }

}