source: trunk/soclib/soclib/lib/mapping_table/include/int_tab.h @ 2200

Revision 362, 4.8 KB checked in by nipo, 4 years ago (diff)

Move each lib/ component to its own directory with its own metadata, this will ease component extraction and reuse

Remove soclib-simulate-any for now, it will be reintroduced later on, in a less intrusive version.

Line 
1/* -*- c++ -*-
2 *
3 * SOCLIB_LGPL_HEADER_BEGIN
4 *
5 * This file is part of SoCLib, GNU LGPLv2.1.
6 *
7 * SoCLib is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; version 2.1 of the License.
10 *
11 * SoCLib is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with SoCLib; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 *
21 * SOCLIB_LGPL_HEADER_END
22 *
23 * Copyright (c) UPMC, Lip6, Asim
24 *         Nicolas Pouillon <nipo@ssji.net>, 2007
25 *
26 * Maintainers: nipo
27 */
28#ifndef SOCLIB_INT_TAB_H_
29#define SOCLIB_INT_TAB_H_
30
31#include <vector>
32#include "exception.h"
33
34namespace soclib { namespace common {
35
36class IntTab
37{
38public:
39    typedef int value_t;
40
41private:
42    static const size_t s_max_level = 10;
43    value_t m_values[s_max_level];
44    size_t m_level;
45
46public:
47    IntTab( value_t val0 = -1,
48            value_t val1 = -1,
49            value_t val2 = -1,
50            value_t val3 = -1,
51            value_t val4 = -1,
52            value_t val5 = -1,
53            value_t val6 = -1,
54            value_t val7 = -1,
55            value_t val8 = -1,
56            value_t val9 = -1 )
57    {
58        value_t vals[s_max_level];
59        vals[0] = val0;
60        vals[1] = val1;
61        vals[2] = val2;
62        vals[3] = val3;
63        vals[4] = val4;
64        vals[5] = val5;
65        vals[6] = val6;
66        vals[7] = val7;
67        vals[8] = val8;
68        vals[9] = val9;
69        init(vals);
70    }
71
72    IntTab( const IntTab &ref )
73    {
74        init(&ref.m_values[0]);
75    }
76
77    IntTab( const IntTab &ref, int nindex )
78    {
79        init(&ref.m_values[0]);
80        m_values[m_level] = nindex;
81        m_values[++m_level] = -1;
82    }
83
84    template<typename T>
85    IntTab( const std::vector<T> &v )
86    {
87        value_t vals[s_max_level];
88        size_t i=0;
89        while ( i < v.size() ) {
90            vals[i] = v[i];
91            ++i;
92        }
93        while ( i < s_max_level ) {
94            vals[i] = -1;
95            ++i;
96        }
97        init(vals);
98    }
99
100    IntTab( const value_t *vals )
101    {
102        init(vals);
103    }
104
105    const IntTab &operator=( const IntTab &ref )
106    {
107        init(&ref.m_values[0]);
108        return *this;
109    }
110
111private:
112    void init( const value_t *vals )
113    {
114        for ( size_t i=0; i<s_max_level; ++i ) {
115            m_values[i] = vals[i];
116            if ( vals[i] == -1 ) {
117                m_level = i;
118                return;
119            }
120        }
121        throw soclib::exception::ValueError("Too much levels");
122    }
123
124public:
125    value_t operator[]( size_t level ) const
126    {
127        if ( level > m_level )
128            throw soclib::exception::ValueError("Level out of bounds");
129        return m_values[level];
130    }
131
132    value_t operator*( const IntTab &widths ) const
133    {
134        if ( widths.level() != m_level )
135            throw soclib::exception::ValueError("Levels not matching");
136        value_t ret = 0;
137
138        for ( size_t l=0; l<m_level; ++l ) {
139            ret <<= widths[l];
140            ret += m_values[l];
141        }
142        return ret;
143    }
144
145    value_t sum( size_t level = s_max_level ) const
146    {
147                if ( m_level < level )
148                        level = m_level;
149        value_t s = 0;
150        for ( size_t i=0; i<level; ++i )
151            s += m_values[i];
152        return s;       
153    }
154
155    inline size_t level() const
156    {
157        return m_level;
158    }
159
160    bool operator==( const IntTab &other ) const
161    {
162        if ( m_level != other.m_level )
163            return false;
164        return idMatches(other);
165    }
166
167    bool operator!=( const IntTab &other ) const
168    {
169        return !(*this == other);
170    }
171
172    bool idMatches( const IntTab &other ) const
173    {
174        size_t m = (m_level < other.m_level)?m_level:other.m_level;
175
176        for ( size_t i=0; i<m; ++i )
177            if ( m_values[i] != other.m_values[i] )
178                return false;
179        return true;
180    }
181
182    void print( std::ostream &o ) const
183    {
184        o << '(';
185        for ( size_t i=0; i<m_level; ++i ) {
186            o << m_values[i];
187            if ( i < m_level-1 )
188                o << ',';
189        }
190        o << ')';
191    }
192
193    friend std::ostream &operator << (std::ostream &o, const IntTab &it)
194    {
195        it.print(o);
196        return o;
197    }
198};
199
200}}
201
202#endif /* SOCLIB_INT_TAB_H_ */
203
204// Local Variables:
205// tab-width: 4
206// c-basic-offset: 4
207// c-file-offsets:((innamespace . 0)(inline-open . 0))
208// indent-tabs-mode: nil
209// End:
210
211// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
212
Note: See TracBrowser for help on using the repository browser.