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