Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event.types; | |
14 | ||
816fde81 MK |
15 | import java.util.Collections; |
16 | import java.util.HashSet; | |
866e5b51 FC |
17 | import java.util.LinkedList; |
18 | import java.util.List; | |
816fde81 | 19 | import java.util.Set; |
866e5b51 FC |
20 | |
21 | /** | |
d37aaa7f | 22 | * A CTF enum declaration. |
0594c61c | 23 | * |
816fde81 MK |
24 | * The definition of a enum point basic data type. It will take the data from a |
25 | * trace and store it (and make it fit) as an integer and a string. | |
d37aaa7f FC |
26 | * |
27 | * @version 1.0 | |
28 | * @author Matthew Khouzam | |
29 | * @author Simon Marchi | |
866e5b51 FC |
30 | */ |
31 | public class EnumDeclaration implements IDeclaration { | |
32 | ||
33 | // ------------------------------------------------------------------------ | |
34 | // Attributes | |
35 | // ------------------------------------------------------------------------ | |
36 | ||
816fde81 MK |
37 | private final EnumTable fTable = new EnumTable(); |
38 | private final IntegerDeclaration fContainerType; | |
3de23137 | 39 | private final Set<String> fLabels = new HashSet<>(); |
866e5b51 FC |
40 | |
41 | // ------------------------------------------------------------------------ | |
42 | // Constructors | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
9ac2eb62 MK |
45 | /** |
46 | * constructor | |
47 | * | |
48 | * @param containerType | |
49 | * the enum is an int, this is the type that the data is | |
50 | * contained in. If you have 1000 possible values, you need at | |
51 | * least a 10 bit enum. If you store 2 values in a 128 bit int, | |
52 | * you are wasting space. | |
53 | */ | |
866e5b51 | 54 | public EnumDeclaration(IntegerDeclaration containerType) { |
816fde81 | 55 | fContainerType = containerType; |
866e5b51 FC |
56 | } |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Getters/Setters/Predicates | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
9ac2eb62 MK |
62 | /** |
63 | * | |
64 | * @return The container type | |
65 | */ | |
866e5b51 | 66 | public IntegerDeclaration getContainerType() { |
816fde81 | 67 | return fContainerType; |
866e5b51 FC |
68 | } |
69 | ||
fd74e6c1 MK |
70 | @Override |
71 | public long getAlignment() { | |
72 | return this.getContainerType().getAlignment(); | |
73 | } | |
9ac2eb62 | 74 | |
866e5b51 FC |
75 | // ------------------------------------------------------------------------ |
76 | // Operations | |
77 | // ------------------------------------------------------------------------ | |
78 | ||
79 | @Override | |
80 | public EnumDefinition createDefinition(IDefinitionScope definitionScope, | |
81 | String fieldName) { | |
82 | return new EnumDefinition(this, definitionScope, fieldName); | |
83 | } | |
84 | ||
9ac2eb62 | 85 | /** |
816fde81 MK |
86 | * Add a value. Do not overlap, this is <i><u><b>not</i></u></b> an interval |
87 | * tree. | |
88 | * | |
89 | * @param low | |
90 | * lowest value that this int can be to have label as a return | |
91 | * string | |
92 | * @param high | |
93 | * highest value that this int can be to have label as a return | |
94 | * string | |
95 | * @param label | |
96 | * the name of the value. | |
9ac2eb62 MK |
97 | * @return was the value be added? true == success |
98 | */ | |
866e5b51 | 99 | public boolean add(long low, long high, String label) { |
816fde81 MK |
100 | fLabels.add(label); |
101 | return fTable.add(low, high, label); | |
866e5b51 FC |
102 | } |
103 | ||
9ac2eb62 | 104 | /** |
816fde81 MK |
105 | * Check if the label for a value (enum a{day=0,night=1} would return "day" |
106 | * for query(0) | |
107 | * | |
108 | * @param value | |
109 | * the value to lookup | |
9ac2eb62 MK |
110 | * @return the label of that value, can be null |
111 | */ | |
866e5b51 | 112 | public String query(long value) { |
816fde81 MK |
113 | return fTable.query(value); |
114 | } | |
115 | ||
116 | /** | |
117 | * Gets a set of labels of the enum | |
118 | * | |
119 | * @return A set of labels of the enum, can be empty but not null | |
c4767854 | 120 | * @since 3.0 |
816fde81 MK |
121 | */ |
122 | public Set<String> getLabels() { | |
123 | return Collections.unmodifiableSet(fLabels); | |
866e5b51 FC |
124 | } |
125 | ||
866e5b51 FC |
126 | /* |
127 | * Maps integer range -> string. A simple list for now, but feel free to | |
128 | * optimize it. Babeltrace suggests an interval tree. | |
129 | */ | |
816fde81 | 130 | private class EnumTable { |
866e5b51 | 131 | |
3de23137 | 132 | private final List<LabelAndRange> ranges = new LinkedList<>(); |
866e5b51 FC |
133 | |
134 | public EnumTable() { | |
135 | } | |
136 | ||
866e5b51 | 137 | public boolean add(long low, long high, String label) { |
816fde81 | 138 | LabelAndRange newRange = new LabelAndRange(low, high, label); |
866e5b51 | 139 | |
816fde81 | 140 | for (LabelAndRange r : ranges) { |
866e5b51 FC |
141 | if (r.intersects(newRange)) { |
142 | return false; | |
143 | } | |
144 | } | |
145 | ||
146 | ranges.add(newRange); | |
147 | ||
148 | return true; | |
149 | } | |
150 | ||
9ac2eb62 | 151 | /** |
a511da0d | 152 | * Return the first label that matches a value |
816fde81 MK |
153 | * |
154 | * @param value | |
155 | * the value to query | |
9ac2eb62 MK |
156 | * @return the label corresponding to that value |
157 | */ | |
866e5b51 | 158 | public String query(long value) { |
816fde81 | 159 | for (LabelAndRange r : ranges) { |
866e5b51 | 160 | if (r.intersects(value)) { |
816fde81 | 161 | return r.fLabel; |
866e5b51 FC |
162 | } |
163 | } | |
866e5b51 FC |
164 | return null; |
165 | } | |
166 | ||
816fde81 | 167 | } |
866e5b51 | 168 | |
816fde81 | 169 | private class LabelAndRange { |
866e5b51 | 170 | |
816fde81 MK |
171 | private final long low, high; |
172 | private final String fLabel; | |
866e5b51 | 173 | |
816fde81 MK |
174 | public LabelAndRange(long low, long high, String str) { |
175 | this.low = low; | |
176 | this.high = high; | |
177 | this.fLabel = str; | |
178 | } | |
866e5b51 | 179 | |
816fde81 MK |
180 | public boolean intersects(long i) { |
181 | return (i >= this.low) && (i <= this.high); | |
182 | } | |
183 | ||
184 | public boolean intersects(LabelAndRange other) { | |
185 | return this.intersects(other.low) | |
186 | || this.intersects(other.high); | |
866e5b51 FC |
187 | } |
188 | } | |
189 | ||
190 | @Override | |
191 | public String toString() { | |
192 | /* Only used for debugging */ | |
193 | return "[declaration] enum[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
194 | } | |
195 | ||
196 | } |