btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / EnumDeclaration.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
22 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
23 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
24
25 /**
26 * A CTF enum declaration.
27 *
28 * The definition of a enum point basic data type. It will take the data from a
29 * trace and store it (and make it fit) as an integer and a string.
30 *
31 * @version 1.0
32 * @author Matthew Khouzam
33 * @author Simon Marchi
34 */
35 public final class EnumDeclaration extends Declaration implements ISimpleDatatypeDeclaration {
36
37 // ------------------------------------------------------------------------
38 // Attributes
39 // ------------------------------------------------------------------------
40
41 private final EnumTable fTable = new EnumTable();
42 private final IntegerDeclaration fContainerType;
43 private final Set<String> fLabels = new HashSet<>();
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 /**
50 * constructor
51 *
52 * @param containerType
53 * the enum is an int, this is the type that the data is
54 * contained in. If you have 1000 possible values, you need at
55 * least a 10 bit enum. If you store 2 values in a 128 bit int,
56 * you are wasting space.
57 */
58 public EnumDeclaration(IntegerDeclaration containerType) {
59 fContainerType = containerType;
60 }
61
62 // ------------------------------------------------------------------------
63 // Getters/Setters/Predicates
64 // ------------------------------------------------------------------------
65
66 /**
67 *
68 * @return The container type
69 */
70 public IntegerDeclaration getContainerType() {
71 return fContainerType;
72 }
73
74 @Override
75 public long getAlignment() {
76 return this.getContainerType().getAlignment();
77 }
78
79 /**
80 * @since 3.0
81 */
82 @Override
83 public int getMaximumSize() {
84 return fContainerType.getMaximumSize();
85 }
86
87 // ------------------------------------------------------------------------
88 // Operations
89 // ------------------------------------------------------------------------
90
91 /**
92 * @since 3.0
93 */
94 @Override
95 public EnumDefinition createDefinition(IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException {
96 alignRead(input);
97 IntegerDefinition value = getContainerType().createDefinition(definitionScope, fieldName, input);
98 return new EnumDefinition(this, definitionScope, fieldName, value);
99 }
100
101 /**
102 * Add a value. Do not overlap, this is <em><strong>not</strong></em> an
103 * interval tree.
104 *
105 * @param low
106 * lowest value that this int can be to have label as a return
107 * string
108 * @param high
109 * highest value that this int can be to have label as a return
110 * string
111 * @param label
112 * the name of the value.
113 * @return was the value be added? true == success
114 */
115 public boolean add(long low, long high, String label) {
116 fLabels.add(label);
117 return fTable.add(low, high, label);
118 }
119
120 /**
121 * Check if the label for a value (enum a{day=0,night=1} would return "day"
122 * for query(0)
123 *
124 * @param value
125 * the value to lookup
126 * @return the label of that value, can be null
127 */
128 public String query(long value) {
129 return fTable.query(value);
130 }
131
132 /**
133 * Gets a set of labels of the enum
134 *
135 * @return A set of labels of the enum, can be empty but not null
136 * @since 3.0
137 */
138 public Set<String> getLabels() {
139 return Collections.unmodifiableSet(fLabels);
140 }
141
142 /*
143 * Maps integer range -> string. A simple list for now, but feel free to
144 * optimize it. Babeltrace suggests an interval tree.
145 */
146 private class EnumTable {
147
148 private final List<LabelAndRange> ranges = new LinkedList<>();
149
150 public EnumTable() {
151 }
152
153 public boolean add(long low, long high, String label) {
154 LabelAndRange newRange = new LabelAndRange(low, high, label);
155
156 for (LabelAndRange r : ranges) {
157 if (r.intersects(newRange)) {
158 return false;
159 }
160 }
161
162 ranges.add(newRange);
163
164 return true;
165 }
166
167 /**
168 * Return the first label that matches a value
169 *
170 * @param value
171 * the value to query
172 * @return the label corresponding to that value
173 */
174 public String query(long value) {
175 for (LabelAndRange r : ranges) {
176 if (r.intersects(value)) {
177 return r.getLabel();
178 }
179 }
180 return null;
181 }
182
183 }
184
185 private static class LabelAndRange {
186
187 private final long low, high;
188 private final String fLabel;
189
190 /**
191 * Get the label
192 *
193 * @return the label
194 */
195 public String getLabel() {
196 return fLabel;
197 }
198
199 public LabelAndRange(long low, long high, String str) {
200 this.low = low;
201 this.high = high;
202 this.fLabel = str;
203 }
204
205 public boolean intersects(long i) {
206 return (i >= this.low) && (i <= this.high);
207 }
208
209 public boolean intersects(LabelAndRange other) {
210 return this.intersects(other.low)
211 || this.intersects(other.high);
212 }
213 }
214
215 @Override
216 public String toString() {
217 /* Only used for debugging */
218 return "[declaration] enum[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
219 }
220
221 }
This page took 0.035245 seconds and 5 git commands to generate.