diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/AbstractTuple.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/AbstractTuple.java | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/AbstractTuple.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/AbstractTuple.java new file mode 100644 index 00000000..a26d9193 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/AbstractTuple.java | |||
@@ -0,0 +1,136 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2017, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.tuple; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.HashMap; | ||
13 | import java.util.HashSet; | ||
14 | import java.util.List; | ||
15 | import java.util.Map; | ||
16 | import java.util.Objects; | ||
17 | import java.util.Set; | ||
18 | |||
19 | /** | ||
20 | * Common implementation methods for immutable and volatile tuples. The class should not be used directly in client | ||
21 | * code, except for the definition of new tuple implementations. | ||
22 | * | ||
23 | * @author Zoltan Ujhelyi | ||
24 | * @since 1.7 | ||
25 | */ | ||
26 | public abstract class AbstractTuple implements ITuple { | ||
27 | |||
28 | /** | ||
29 | * As the tuple is supposed to be immutable, do not modify the returned array. | ||
30 | * | ||
31 | * @return the array containing all elements of this Tuple | ||
32 | */ | ||
33 | @Override | ||
34 | public Object[] getElements() { | ||
35 | Object[] allElements = new Object[getSize()]; | ||
36 | for (int i = 0; i < allElements.length; ++i) | ||
37 | allElements[i] = get(i); | ||
38 | return allElements; | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * @return the set containing all distinct elements of this Tuple, cast as type T | ||
43 | */ | ||
44 | @Override | ||
45 | @SuppressWarnings("unchecked") | ||
46 | public <T> Set<T> getDistinctElements() { | ||
47 | Set<T> result = new HashSet<T>(); | ||
48 | Object[] elements = getElements(); | ||
49 | for (Object object : elements) { | ||
50 | result.add((T) object); | ||
51 | } | ||
52 | return result; | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * Calculates an inverted index of the elements of this pattern. For each element, the index of the (last) | ||
57 | * occurrence is calculated. | ||
58 | * | ||
59 | * @return the inverted index mapping each element of this pattern to its index in the array | ||
60 | */ | ||
61 | @Override | ||
62 | public Map<Object, Integer> invertIndex() { | ||
63 | Map<Object, Integer> result = new HashMap<Object, Integer>(); | ||
64 | for (int i = 0; i < getSize(); i++) | ||
65 | result.put(get(i), i); | ||
66 | return result; | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Calculates an inverted index of the elements of this pattern. For each element, the index of all of its | ||
71 | * occurrences is calculated. | ||
72 | * | ||
73 | * @return the inverted index mapping each element of this pattern to its index in the array | ||
74 | */ | ||
75 | @Override | ||
76 | public Map<Object, List<Integer>> invertIndexWithMupliplicity() { | ||
77 | Map<Object, List<Integer>> result = new HashMap<Object, List<Integer>>(); | ||
78 | for (int i = 0; i < getSize(); i++) { | ||
79 | Object value = get(i); | ||
80 | List<Integer> indices = result.computeIfAbsent(value, v -> new ArrayList<>()); | ||
81 | indices.add(i); | ||
82 | } | ||
83 | return result; | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * @since 1.7 | ||
88 | */ | ||
89 | protected IndexOutOfBoundsException raiseIndexingError(int index) { | ||
90 | return new IndexOutOfBoundsException( | ||
91 | String.format("No value at position %d for %s instance %s", index, getClass().getSimpleName(), this)); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Compares the elements stored in this tuple to another tuple | ||
96 | */ | ||
97 | protected boolean internalEquals(ITuple other) { | ||
98 | if (getSize() != other.getSize()) | ||
99 | return false; | ||
100 | boolean result = true; | ||
101 | for (int i = 0; result && i < getSize(); ++i) { | ||
102 | Object ours = get(i); | ||
103 | Object theirs = other.get(i); | ||
104 | result = result && Objects.equals(ours, theirs); | ||
105 | } | ||
106 | return result; | ||
107 | } | ||
108 | |||
109 | @Override | ||
110 | public String toString() { | ||
111 | StringBuilder s = new StringBuilder(); | ||
112 | s.append("T("); | ||
113 | for (Object o : getElements()) { | ||
114 | s.append(o == null ? "null" : o.toString()); | ||
115 | s.append(';'); | ||
116 | } | ||
117 | s.append(')'); | ||
118 | return s.toString(); | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * @since 1.7 | ||
123 | */ | ||
124 | protected int doCalcHash() { | ||
125 | final int PRIME = 31; | ||
126 | int hash = 1; | ||
127 | for (int i = 0; i < getSize(); i++) { | ||
128 | hash = PRIME * hash; | ||
129 | Object element = get(i); | ||
130 | if (element != null) | ||
131 | hash += element.hashCode(); | ||
132 | } | ||
133 | return hash; | ||
134 | } | ||
135 | |||
136 | } \ No newline at end of file | ||