… | |
… | |
5 | # define is_constant(c) __builtin_constant_p (c) |
5 | # define is_constant(c) __builtin_constant_p (c) |
6 | #else |
6 | #else |
7 | # define is_constant(c) 0 |
7 | # define is_constant(c) 0 |
8 | #endif |
8 | #endif |
9 | |
9 | |
|
|
10 | #include <cstddef> |
|
|
11 | |
|
|
12 | #include <glib.h> |
|
|
13 | |
|
|
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
|
|
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
16 | |
10 | // makes dynamically allocated objects zero-initialised |
17 | // makes dynamically allocated objects zero-initialised |
11 | struct zero_initialised |
18 | struct zero_initialised |
12 | { |
19 | { |
|
|
20 | void *operator new (size_t s, void *p) |
|
|
21 | { |
|
|
22 | memset (p, 0, s); |
|
|
23 | return p; |
|
|
24 | } |
|
|
25 | |
13 | void *operator new (size_t s); |
26 | void *operator new (size_t s) |
|
|
27 | { |
|
|
28 | return g_slice_alloc0 (s); |
|
|
29 | } |
|
|
30 | |
14 | void *operator new [] (size_t s); |
31 | void *operator new[] (size_t s) |
|
|
32 | { |
|
|
33 | return g_slice_alloc0 (s); |
|
|
34 | } |
|
|
35 | |
15 | void operator delete (void *p, size_t s); |
36 | void operator delete (void *p, size_t s) |
|
|
37 | { |
|
|
38 | g_slice_free1 (s, p); |
|
|
39 | } |
|
|
40 | |
16 | void operator delete [] (void *p, size_t s); |
41 | void operator delete[] (void *p, size_t s) |
|
|
42 | { |
|
|
43 | g_slice_free1 (s, p); |
|
|
44 | } |
|
|
45 | }; |
|
|
46 | |
|
|
47 | // strictly the same as g_slice_alloc, but never returns 0 |
|
|
48 | void *alloc (int s) throw (std::bad_alloc); |
|
|
49 | // for symmetry |
|
|
50 | inline void dealloc (void *p, int s) throw () |
|
|
51 | { |
|
|
52 | g_slice_free1 (s, p); |
|
|
53 | } |
|
|
54 | |
|
|
55 | // a STL-compatible allocator that uses g_slice |
|
|
56 | // boy, this is verbose |
|
|
57 | template<typename Tp> |
|
|
58 | struct slice_allocator |
|
|
59 | { |
|
|
60 | typedef size_t size_type; |
|
|
61 | typedef ptrdiff_t difference_type; |
|
|
62 | typedef Tp *pointer; |
|
|
63 | typedef const Tp *const_pointer; |
|
|
64 | typedef Tp &reference; |
|
|
65 | typedef const Tp &const_reference; |
|
|
66 | typedef Tp value_type; |
|
|
67 | |
|
|
68 | template <class U> |
|
|
69 | struct rebind |
|
|
70 | { |
|
|
71 | typedef slice_allocator<U> other; |
|
|
72 | }; |
|
|
73 | |
|
|
74 | slice_allocator () throw () { } |
|
|
75 | slice_allocator (const slice_allocator &o) throw () { } |
|
|
76 | template<typename Tp2> |
|
|
77 | slice_allocator (const slice_allocator<Tp2> &) throw () { } |
|
|
78 | |
|
|
79 | ~slice_allocator () { } |
|
|
80 | |
|
|
81 | pointer address (reference x) const { return &x; } |
|
|
82 | const_pointer address (const_reference x) const { return &x; } |
|
|
83 | |
|
|
84 | pointer allocate (size_type n, const_pointer = 0) |
|
|
85 | { |
|
|
86 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
|
|
87 | } |
|
|
88 | |
|
|
89 | void deallocate (pointer p, size_type n) |
|
|
90 | { |
|
|
91 | dealloc (static_cast<void *>(p), n * sizeof (Tp)); |
|
|
92 | } |
|
|
93 | |
|
|
94 | size_type max_size ()const throw () |
|
|
95 | { |
|
|
96 | return size_t (-1) / sizeof (Tp); |
|
|
97 | } |
|
|
98 | |
|
|
99 | void construct (pointer p, const Tp &val) |
|
|
100 | { |
|
|
101 | ::new (p) Tp (val); |
|
|
102 | } |
|
|
103 | |
|
|
104 | void destroy (pointer p) |
|
|
105 | { |
|
|
106 | p->~Tp (); |
|
|
107 | } |
|
|
108 | }; |
|
|
109 | |
|
|
110 | struct refcounted |
|
|
111 | { |
|
|
112 | refcounted () : refcnt (0) { } |
|
|
113 | // virtual ~refcounted (); |
|
|
114 | void refcnt_inc () { ++refcnt; } |
|
|
115 | void refcnt_dec () { --refcnt; } |
|
|
116 | bool dead () { return refcnt == 0; } |
|
|
117 | mutable int refcnt; |
|
|
118 | #if 0 |
|
|
119 | private: |
|
|
120 | static refcounted *rc_first; |
|
|
121 | refcounted *rc_next; |
|
|
122 | #endif |
|
|
123 | }; |
|
|
124 | |
|
|
125 | template<class T> |
|
|
126 | struct refptr |
|
|
127 | { |
|
|
128 | T *p; |
|
|
129 | |
|
|
130 | refptr () : p(0) { } |
|
|
131 | refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
|
|
132 | refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
|
|
133 | ~refptr () { if (p) p->refcnt_dec (); } |
|
|
134 | |
|
|
135 | const refptr<T> &operator =(T *o) |
|
|
136 | { |
|
|
137 | if (p) p->refcnt_dec (); |
|
|
138 | p = o; |
|
|
139 | if (p) p->refcnt_inc (); |
|
|
140 | |
|
|
141 | return *this; |
|
|
142 | } |
|
|
143 | |
|
|
144 | const refptr<T> &operator =(const refptr<T> o) |
|
|
145 | { |
|
|
146 | *this = o.p; |
|
|
147 | return *this; |
|
|
148 | } |
|
|
149 | |
|
|
150 | T &operator * () const { return *p; } |
|
|
151 | T *operator ->() const { return p; } |
|
|
152 | |
|
|
153 | operator T *() const { return p; } |
17 | }; |
154 | }; |
18 | |
155 | |
19 | struct str_hash |
156 | struct str_hash |
20 | { |
157 | { |
21 | std::size_t operator ()(const char *s) const |
158 | std::size_t operator ()(const char *s) const |
22 | { |
159 | { |
23 | unsigned long hash = 0; |
160 | unsigned long hash = 0; |
24 | unsigned int i = 0; |
|
|
25 | |
161 | |
26 | /* use the one-at-a-time hash function, which supposedly is |
162 | /* use the one-at-a-time hash function, which supposedly is |
27 | * better than the djb2-like one used by perl5.005, but |
163 | * better than the djb2-like one used by perl5.005, but |
28 | * certainly is better then the bug used here before. |
164 | * certainly is better then the bug used here before. |
29 | * see http://burtleburtle.net/bob/hash/doobs.html |
165 | * see http://burtleburtle.net/bob/hash/doobs.html |
… | |
… | |
49 | { |
185 | { |
50 | return !strcmp (a, b); |
186 | return !strcmp (a, b); |
51 | } |
187 | } |
52 | }; |
188 | }; |
53 | |
189 | |
|
|
190 | #include <vector> |
|
|
191 | |
|
|
192 | template<class obj> |
|
|
193 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
|
|
194 | { |
|
|
195 | typedef typename unordered_vector::iterator iterator; |
|
|
196 | |
|
|
197 | void erase (unsigned int pos) |
|
|
198 | { |
|
|
199 | if (pos < this->size () - 1) |
|
|
200 | (*this)[pos] = (*this)[this->size () - 1]; |
|
|
201 | |
|
|
202 | this->pop_back (); |
|
|
203 | } |
|
|
204 | |
|
|
205 | void erase (iterator i) |
|
|
206 | { |
|
|
207 | erase ((unsigned int )(i - this->begin ())); |
|
|
208 | } |
|
|
209 | }; |
|
|
210 | |
|
|
211 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
212 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
213 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
|
|
214 | |
|
|
215 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
216 | |
|
|
217 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
|
|
218 | void assign (char *dst, const char *src, int maxlen); |
|
|
219 | |
|
|
220 | // type-safe version of assign |
|
|
221 | template<int N> |
|
|
222 | inline void assign (char (&dst)[N], const char *src) |
|
|
223 | { |
|
|
224 | assign ((char *)&dst, src, N); |
|
|
225 | } |
|
|
226 | |
54 | #endif |
227 | #endif |
55 | |
228 | |