1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import java
.util
.LinkedList
;
16 import java
.util
.List
;
19 * A CTF enum declaration.
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.
25 * @author Matthew Khouzam
26 * @author Simon Marchi
28 public class EnumDeclaration
implements IDeclaration
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 private final EnumTable table
= new EnumTable();
35 private IntegerDeclaration containerType
= null;
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
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.
50 public EnumDeclaration(IntegerDeclaration containerType
) {
51 this.containerType
= containerType
;
54 // ------------------------------------------------------------------------
55 // Getters/Setters/Predicates
56 // ------------------------------------------------------------------------
60 * @return The container type
62 public IntegerDeclaration
getContainerType() {
67 public long getAlignment() {
68 return this.getContainerType().getAlignment();
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 public EnumDefinition
createDefinition(IDefinitionScope definitionScope
,
78 return new EnumDefinition(this, definitionScope
, fieldName
);
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
88 public boolean add(long low
, long high
, String label
) {
89 return table
.add(low
, high
, label
);
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
97 public String
query(long value
) {
98 return table
.query(value
);
102 * Maps integer range -> string. A simple list for now, but feel free to
103 * optimize it. Babeltrace suggests an interval tree.
105 static private class EnumTable
{
107 List
<Range
> ranges
= new LinkedList
<Range
>();
112 public boolean add(long low
, long high
, String label
) {
113 Range newRange
= new Range(low
, high
, label
);
115 for (Range r
: ranges
) {
116 if (r
.intersects(newRange
)) {
121 ranges
.add(newRange
);
127 * return the first label that matches a value
128 * @param value the value to query
129 * @return the label corresponding to that value
131 public String
query(long value
) {
132 for (Range r
: ranges
) {
133 if (r
.intersects(value
)) {
140 static private class Range
{
145 public Range(long low
, long high
, String str
) {
151 public boolean intersects(long i
) {
152 return (i
>= this.low
) && (i
<= this.high
);
155 public boolean intersects(Range other
) {
156 return this.intersects(other
.low
)
157 || this.intersects(other
.high
);
163 public String
toString() {
164 /* Only used for debugging */
165 return "[declaration] enum[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$