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
|
/*
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 mx,Data *dt,bool o)
: owned(o),max(mx),data(dt)
, n(0),parent(p),left(NULL),right(NULL)
{}
virtual ~TableAnyMap();
int size() const;
void insert(size_t k,void *t)
{
FLEXT_ASSERT(t);
if(n) _set(k,t);
else data[n++](k,t);
}
void *find(size_t k) { return n?_find(k):NULL; }
void clear();
void _toleft(size_t k,void *t)
{
if(left)
left->_set(k,t);
else {
left = New(this);
left->data[0](k,t);
left->n = 1;
}
}
void _toright(size_t k,void *t)
{
if(right)
right->_set(k,t);
else {
right = New(this);
right->data[0](k,t);
right->n = 1;
}
}
void _toleft(Data &v) { _toleft(v.key,v.value); }
void _toright(Data &v) { _toright(v.key,v.value); }
void _set(size_t k,void *t);
void *_find(size_t k);
const bool owned;
const int max;
Data *const data;
int n;
TableAnyMap *parent,*left,*right;
class iterator
{
public:
iterator(): map(NULL) {}
iterator(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)
TableAnyMap *nmap;
while((nmap = map->left) != NULL) map = nmap;
}
void forward();
TableAnyMap *map;
int ix;
};
};
template <typename K,typename T,int N = 8,bool O = false>
class TableMap
: TableAnyMap
{
public:
TableMap(): TableAnyMap(NULL,N,slots,O) {}
virtual ~TableMap() { 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) { return (T *)TableAnyMap::find(*(size_t *)&k); }
class iterator
: TableAnyMap::iterator
{
public:
iterator() {}
iterator(TableMap &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:
TableMap(TableAnyMap *p): TableAnyMap(p,N,slots,O) {}
virtual TableAnyMap *New(TableAnyMap *parent) { return new TableMap(parent); }
virtual void Free(void *ptr) { delete (T *)ptr; }
Data slots[N];
};
//! @} // FLEXT_SUPPORT
#endif
|