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 | ||
34 | public EnumDeclaration(IntegerDeclaration containerType) { | |
35 | this.containerType = containerType; | |
36 | } | |
37 | ||
38 | // ------------------------------------------------------------------------ | |
39 | // Getters/Setters/Predicates | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
42 | public IntegerDeclaration getContainerType() { | |
43 | return containerType; | |
44 | } | |
45 | ||
fd74e6c1 MK |
46 | @Override |
47 | public long getAlignment() { | |
48 | return this.getContainerType().getAlignment(); | |
49 | } | |
866e5b51 FC |
50 | // ------------------------------------------------------------------------ |
51 | // Operations | |
52 | // ------------------------------------------------------------------------ | |
53 | ||
54 | @Override | |
55 | public EnumDefinition createDefinition(IDefinitionScope definitionScope, | |
56 | String fieldName) { | |
57 | return new EnumDefinition(this, definitionScope, fieldName); | |
58 | } | |
59 | ||
60 | public boolean add(long low, long high, String label) { | |
61 | return table.add(low, high, label); | |
62 | } | |
63 | ||
64 | public String query(long value) { | |
65 | return table.query(value); | |
66 | } | |
67 | ||
68 | public String getLabel(long i) { | |
69 | return table.getLabel(i); | |
70 | } | |
71 | ||
72 | /* | |
73 | * Maps integer range -> string. A simple list for now, but feel free to | |
74 | * optimize it. Babeltrace suggests an interval tree. | |
75 | */ | |
76 | static private class EnumTable { | |
77 | ||
78 | List<Range> ranges = new LinkedList<Range>(); | |
79 | ||
80 | public EnumTable() { | |
81 | } | |
82 | ||
83 | public String getLabel(long i) { | |
84 | for (Range r : ranges) { | |
85 | if (r.intersects(i)) { | |
86 | return r.str; | |
87 | } | |
88 | } | |
89 | return null; | |
90 | } | |
91 | ||
92 | public boolean add(long low, long high, String label) { | |
93 | Range newRange = new Range(low, high, label); | |
94 | ||
95 | for (Range r : ranges) { | |
96 | if (r.intersects(newRange)) { | |
97 | return false; | |
98 | } | |
99 | } | |
100 | ||
101 | ranges.add(newRange); | |
102 | ||
103 | return true; | |
104 | } | |
105 | ||
106 | public String query(long value) { | |
107 | for (Range r : ranges) { | |
108 | if (r.intersects(value)) { | |
109 | return r.str; | |
110 | } | |
111 | } | |
112 | ||
113 | return null; | |
114 | } | |
115 | ||
116 | static private class Range { | |
117 | ||
118 | long low, high; | |
119 | String str; | |
120 | ||
121 | public Range(long low, long high, String str) { | |
122 | this.low = low; | |
123 | this.high = high; | |
124 | this.str = str; | |
125 | } | |
126 | ||
127 | public boolean intersects(long i) { | |
128 | return (i >= this.low) && (i <= this.high); | |
129 | } | |
130 | ||
131 | public boolean intersects(Range other) { | |
132 | return this.intersects(other.low) | |
133 | || this.intersects(other.high); | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | @Override | |
139 | public String toString() { | |
140 | /* Only used for debugging */ | |
141 | return "[declaration] enum[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
142 | } | |
143 | ||
fd74e6c1 | 144 | |
866e5b51 | 145 | } |