aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/source/flmap.h
blob: 4c3a1c9e386a96f70100da27905b3ebc2361b0b5 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* 

flext - C++ layer for Max/MSP and pd (pure data) externals

Copyright (c) 2001-2005 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

/*! \file flmap.h
	\brief special map class for all 32-bit key/value-pairs   
*/

#ifndef __FLMAP_H
#define __FLMAP_H

#include "flprefix.h"

/*!	\defgroup FLEXT_SUPPORT Flext support classes
	@{
*/

#if 0

#include <map>

//! Key/Value type for AnyMap... must have size of pointer!
typedef size_t AnyMapType;

//! Base class for maps
class AnyMap:
    public std::map<AnyMapType,AnyMapType>
{
    typedef std::map<AnyMapType,AnyMapType> Parent;
public:
    AnyMap();
    ~AnyMap();
    iterator find(AnyMapType k);
    AnyMapType &operator [](AnyMapType k);

    typedef std::pair<AnyMapType,AnyMapType> pair;
};

//! Specialized map class for any 32-bit key/value types
template <class K,class T>
class DataMap:
    public AnyMap
{
public:
    class iterator:
        public AnyMap::iterator
    {
    public:
        iterator() {}
#if defined(_MSC_VER) && (_MSC_VER < 0x1300)
        // with the MSVC6 STL implementation iterators can't be initialized...
        iterator(AnyMap::iterator it) { static_cast<AnyMap::iterator &>(*this) = it; }
#else
        // note: &it doesn't work for gcc (i don't know why it doesn't...)
        iterator(AnyMap::iterator it): AnyMap::iterator(it) {}
#endif

        inline K &key() const { return *(K *)&((*this)->first); }
        inline T &data() const { return *(T *)&((*this)->second); }
    };

    class pair:
        public AnyMap::pair
    {
	public:
        inline K &key() const { return *(K *)&first; }
        inline T &data() const { return *(T *)&second; }
	};

    inline iterator find(K k) { return AnyMap::find(*(AnyMapType *)&k); }
    inline T &operator [](K k) { return *(T *)&(AnyMap::operator [](*(AnyMapType *)&k)); }
    inline void erase(K k) { AnyMap::erase(*(AnyMapType *)&k); }
};

#endif

class FLEXT_SHARE TableAnyMap
{
protected:
    virtual TableAnyMap *New(TableAnyMap *parent) = 0;
    virtual void Free(void *ptr) = 0;

    struct Data {
        void operator()(size_t k,void *v) { key = k,value = v; }
        void operator =(void *v) { value = v; }

        size_t key;
        void *value;
    };

    TableAnyMap(TableAnyMap *p,int sz,Data *dt)
        : tsize(sz),data(dt)
        , n(0),count(0)
        , parent(p),left(NULL),right(NULL) 
    {}

    virtual ~TableAnyMap();

    int size() const { return count; }

    void insert(size_t k,void *t)
    {
//        FLEXT_ASSERT(t);
        if(n) 
            _set(k,t);
        else {
            data[n++](k,t);
            ++count;
        }
    }

    void *find(size_t k) const { return n?_find(k):NULL; }

    void erase(size_t k) { if(n) { void *s = _remove(k); if(s) Free(s); } }

    void *remove(size_t k) { return n?_remove(k):NULL; }

    void clear();

    class FLEXT_SHARE iterator
    {
    public:
        iterator(): map(NULL) {}
        iterator(const TableAnyMap &m): map(&m),ix(0) { leftmost(); }
        iterator(iterator &it): map(it.map),ix(it.ix) {}
    
        iterator &operator =(const iterator &it) { map = it.map,ix = it.ix; return *this; }

        operator bool() const { return map && /*ix >= 0 &&*/ ix < map->n; }

        // no checking here!
        void *data() const { return map->data[ix].value; }
        size_t key() const { return map->data[ix].key; }

        iterator &operator ++() { forward(); return *this; }  

    protected:
        void leftmost()
        {
            // search smallest branch (go left as far as possible)
            const TableAnyMap *nmap;
            while((nmap = map->left) != NULL) map = nmap;
        }

        void forward();

        const TableAnyMap *map;
        int ix;
    };

private:
    void _init(size_t k,void *t) { data[0](k,t); n = count = 1; }

    bool _toleft(size_t k,void *t)
    {
        if(left) {
            bool a = left->_set(k,t);
            if(a) ++count;
            return a;
        }
        else {
            (left = New(this))->_init(k,t);
            ++count;
            return true;
        }
    }

    bool _toright(size_t k,void *t)
    {
        if(right) {
            bool a = right->_set(k,t);
            if(a) ++count;
            return a;
        }
        else {
            (right = New(this))->_init(k,t);
            ++count;
            return true;
        }
    }

    bool _toleft(Data &v) { return _toleft(v.key,v.value); }
    bool _toright(Data &v) { return _toright(v.key,v.value); }

    bool _set(size_t k,void *t);
    void *_find(size_t k) const;
    void *_remove(size_t k);

    const int tsize;
    Data *const data;
    int n,count;
    TableAnyMap *parent,*left,*right;

    int _tryix(size_t k) const
    {
        //! return index of data item with key <= k

//        FLEXT_ASSERT(n);
        int ix = 0;
        {
            int b = n;
            while(ix != b) {
                const int c = (ix+b)/2;
                const size_t dk = data[c].key;
                if(k == dk)
                    return c;
                else if(k < dk)
                    b = c;
                else if(ix < c)
                    ix = c;
                else {
                    ix = b;
                    break;
                }
            }
        }
        return ix;
    }

    static void _eraseempty(TableAnyMap *&b)
    {
//        FLEXT_ASSERT(b);
        if(!b->n) { 
            // remove empty branch
            delete b; b = NULL; 
        }
    }

    void _getsmall(Data &dt);
    void _getbig(Data &dt);
};

template <typename K,typename T,int N = 8>
class TablePtrMap
    : TableAnyMap
{
public:
    TablePtrMap(): TableAnyMap(NULL,N,slots) {}
    virtual ~TablePtrMap() { clear(); }

    inline void clear() { TableAnyMap::clear(); }

    inline int size() const { return TableAnyMap::size(); }

    inline void insert(K k,T *t) { TableAnyMap::insert(*(size_t *)&k,t); }

    inline T *find(K k) const { return (T *)TableAnyMap::find(*(size_t *)&k); }

    inline void erase(K k) { TableAnyMap::erase(*(size_t *)&k); }
    inline T *remove(K k) { return (T *)TableAnyMap::remove(*(size_t *)&k); }

    class iterator
        : TableAnyMap::iterator
    {
    public:
        iterator() {}
        iterator(const TablePtrMap &m): TableAnyMap::iterator(m) {}
        iterator(iterator &it): TableAnyMap::iterator(it) {}

        inline iterator &operator =(const iterator &it) { TableAnyMap::operator =(it); return *this; }

        inline operator bool() const {return TableAnyMap::iterator::operator bool(); }
        inline T *data() const { return (T *)TableAnyMap::iterator::data(); }
        inline K key() const { return (K)TableAnyMap::iterator::key(); }

        inline iterator &operator ++() { TableAnyMap::iterator::operator ++(); return *this; }  

    };

protected:
    TablePtrMap(TableAnyMap *p): TableAnyMap(p,N,slots) {}

    virtual TableAnyMap *New(TableAnyMap *parent) { return new TablePtrMap(parent); }

    virtual void Free(void *ptr) {}

    Data slots[N];
};

template <typename K,typename T,int N = 8>
class TablePtrMapOwned
    : public TablePtrMap<K,T,N>
{
public:
    virtual ~TablePtrMapOwned() { TablePtrMapOwned<K,T,N>::clear(); }

protected:
    virtual void Free(void *ptr) 
    { 
//            FLEXT_ASSERT(ptr);
        delete (T *)ptr;
    }

};

//! @} // FLEXT_SUPPORT

#endif