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