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 | ||
15 | import java.util.LinkedList; | |
16 | import java.util.List; | |
17 | ||
18 | /** | |
d37aaa7f | 19 | * A CTF enum declaration. |
0594c61c | 20 | * |
d37aaa7f FC |
21 | * The definition of a enum point basic data type. It will take the data |
22 | * from a trace and store it (and make it fit) as an integer and a string. | |
23 | * | |
24 | * @version 1.0 | |
25 | * @author Matthew Khouzam | |
26 | * @author Simon Marchi | |
866e5b51 FC |
27 | */ |
28 | public class EnumDeclaration implements IDeclaration { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | private final EnumTable table = new EnumTable(); | |
35 | private IntegerDeclaration containerType = null; | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Constructors | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
9ac2eb62 MK |
41 | /** |
42 | * constructor | |
43 | * | |
44 | * @param containerType | |
45 | * the enum is an int, this is the type that the data is | |
46 | * contained in. If you have 1000 possible values, you need at | |
47 | * least a 10 bit enum. If you store 2 values in a 128 bit int, | |
48 | * you are wasting space. | |
49 | */ | |
866e5b51 FC |
50 | public EnumDeclaration(IntegerDeclaration containerType) { |
51 | this.containerType = containerType; | |
52 | } | |
53 | ||
54 | // ------------------------------------------------------------------------ | |
55 | // Getters/Setters/Predicates | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
9ac2eb62 MK |
58 | /** |
59 | * | |
60 | * @return The container type | |
61 | */ | |
866e5b51 FC |
62 | public IntegerDeclaration getContainerType() { |
63 | return containerType; | |
64 | } | |
65 | ||
fd74e6c1 MK |
66 | @Override |
67 | public long getAlignment() { | |
68 | return this.getContainerType().getAlignment(); | |
69 | } | |
9ac2eb62 | 70 | |
866e5b51 FC |
71 | // ------------------------------------------------------------------------ |
72 | // Operations | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
75 | @Override | |
76 | public EnumDefinition createDefinition(IDefinitionScope definitionScope, | |
77 | String fieldName) { | |
78 | return new EnumDefinition(this, definitionScope, fieldName); | |
79 | } | |
80 | ||
9ac2eb62 MK |
81 | /** |
82 | * Add a value. Do not overlap, this is <i><u><b>not</i></u></b> an interval tree. | |
83 | * @param low lowest value that this int can be to have label as a return string | |
84 | * @param high highest value that this int can be to have label as a return string | |
85 | * @param label the name of the value. | |
86 | * @return was the value be added? true == success | |
87 | */ | |
866e5b51 FC |
88 | public boolean add(long low, long high, String label) { |
89 | return table.add(low, high, label); | |
90 | } | |
91 | ||
9ac2eb62 MK |
92 | /** |
93 | * check if the label for a value (enum a{day=0,night=1} would return "day" for query(0) | |
94 | * @param value the value to lookup | |
95 | * @return the label of that value, can be null | |
96 | */ | |
866e5b51 FC |
97 | public String query(long value) { |
98 | return table.query(value); | |
99 | } | |
100 | ||
866e5b51 FC |
101 | /* |
102 | * Maps integer range -> string. A simple list for now, but feel free to | |
103 | * optimize it. Babeltrace suggests an interval tree. | |
104 | */ | |
0594c61c | 105 | private static class EnumTable { |
866e5b51 | 106 | |
0594c61c | 107 | private List<Range> ranges = new LinkedList<Range>(); |
866e5b51 FC |
108 | |
109 | public EnumTable() { | |
110 | } | |
111 | ||
866e5b51 FC |
112 | public boolean add(long low, long high, String label) { |
113 | Range newRange = new Range(low, high, label); | |
114 | ||
115 | for (Range r : ranges) { | |
116 | if (r.intersects(newRange)) { | |
117 | return false; | |
118 | } | |
119 | } | |
120 | ||
121 | ranges.add(newRange); | |
122 | ||
123 | return true; | |
124 | } | |
125 | ||
9ac2eb62 MK |
126 | /** |
127 | * return the first label that matches a value | |
128 | * @param value the value to query | |
129 | * @return the label corresponding to that value | |
130 | */ | |
866e5b51 FC |
131 | public String query(long value) { |
132 | for (Range r : ranges) { | |
133 | if (r.intersects(value)) { | |
134 | return r.str; | |
135 | } | |
136 | } | |
866e5b51 FC |
137 | return null; |
138 | } | |
139 | ||
0594c61c | 140 | private static class Range { |
866e5b51 | 141 | |
0594c61c AM |
142 | private long low, high; |
143 | private String str; | |
866e5b51 FC |
144 | |
145 | public Range(long low, long high, String str) { | |
146 | this.low = low; | |
147 | this.high = high; | |
148 | this.str = str; | |
149 | } | |
150 | ||
151 | public boolean intersects(long i) { | |
152 | return (i >= this.low) && (i <= this.high); | |
153 | } | |
154 | ||
155 | public boolean intersects(Range other) { | |
156 | return this.intersects(other.low) | |
157 | || this.intersects(other.high); | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | @Override | |
163 | public String toString() { | |
164 | /* Only used for debugging */ | |
165 | return "[declaration] enum[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
166 | } | |
167 | ||
168 | } |