Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f1fa82cb44acd7960acbff8a90d63871db1efcc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
 *
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Matthew Khouzam - Initial API and implementation
 * Contributors: Simon Marchi - Initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.ctf.core.event.types;

import java.util.LinkedList;
import java.util.List;

/**
 * <b><u>EnumDeclaration</u></b>
 */
public class EnumDeclaration implements IDeclaration {

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------

    private final EnumTable table = new EnumTable();
    private IntegerDeclaration containerType = null;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    public EnumDeclaration(IntegerDeclaration containerType) {
        this.containerType = containerType;
    }

    // ------------------------------------------------------------------------
    // Getters/Setters/Predicates
    // ------------------------------------------------------------------------

    public IntegerDeclaration getContainerType() {
        return containerType;
    }

    @Override
    public long getAlignment() {
        return this.getContainerType().getAlignment();
    }
    // ------------------------------------------------------------------------
    // Operations
    // ------------------------------------------------------------------------

    @Override
    public EnumDefinition createDefinition(IDefinitionScope definitionScope,
            String fieldName) {
        return new EnumDefinition(this, definitionScope, fieldName);
    }

    public boolean add(long low, long high, String label) {
        return table.add(low, high, label);
    }

    public String query(long value) {
        return table.query(value);
    }

    public String getLabel(long i) {
        return table.getLabel(i);
    }

    /*
     * Maps integer range -> string. A simple list for now, but feel free to
     * optimize it. Babeltrace suggests an interval tree.
     */
    static private class EnumTable {

        List<Range> ranges = new LinkedList<Range>();

        public EnumTable() {
        }

        public String getLabel(long i) {
            for (Range r : ranges) {
                if (r.intersects(i)) {
                    return r.str;
                }
            }
            return null;
        }

        public boolean add(long low, long high, String label) {
            Range newRange = new Range(low, high, label);

            for (Range r : ranges) {
                if (r.intersects(newRange)) {
                    return false;
                }
            }

            ranges.add(newRange);

            return true;
        }

        public String query(long value) {
            for (Range r : ranges) {
                if (r.intersects(value)) {
                    return r.str;
                }
            }

            return null;
        }

        static private class Range {

            long low, high;
            String str;

            public Range(long low, long high, String str) {
                this.low = low;
                this.high = high;
                this.str = str;
            }

            public boolean intersects(long i) {
                return (i >= this.low) && (i <= this.high);
            }

            public boolean intersects(Range other) {
                return this.intersects(other.low)
                        || this.intersects(other.high);
            }
        }
    }

    @Override
    public String toString() {
        /* Only used for debugging */
        return "[declaration] enum[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
    }


}

Back to the top