~ubuntu-branches/ubuntu/wily/tora/wily-proposed

« back to all changes in this revision

Viewing changes to ext/loki/loki-0.1.6/include/loki/flex/smallstringopt.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2009-04-07 13:16:05 UTC
  • mfrom: (1.2.7 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090407131605-u422yigfv7jgg0l0
Tags: 2.0.0-3
* Cleaned up packaging a little bit.
* Added homepage information to control file.
* Bumped Standards-Version to 3.8.1.
* Released to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////////
2
 
// flex_string
3
 
// Copyright (c) 2001 by Andrei Alexandrescu
4
 
// Permission to use, copy, modify, distribute and sell this software for any
5
 
//     purpose is hereby granted without fee, provided that the above copyright
6
 
//     notice appear in all copies and that both that copyright notice and this
7
 
//     permission notice appear in supporting documentation.
8
 
// The author makes no representations about the
9
 
//     suitability of this software for any purpose. It is provided "as is"
10
 
//     without express or implied warranty.
11
 
////////////////////////////////////////////////////////////////////////////////
12
 
 
13
 
#ifndef SMALL_STRING_OPT_INC_
14
 
#define SMALL_STRING_OPT_INC_
15
 
 
16
 
// $Id: smallstringopt.h 754 2006-10-17 19:59:11Z syntheticpp $
17
 
 
18
 
 
19
 
////////////////////////////////////////////////////////////////////////////////
20
 
// class template SmallStringOpt
21
 
// Builds the small string optimization over any other storage
22
 
////////////////////////////////////////////////////////////////////////////////
23
 
 
24
 
/* This is the template for a storage policy
25
 
////////////////////////////////////////////////////////////////////////////////
26
 
template <typename E, class A = @>
27
 
class StoragePolicy
28
 
{
29
 
    typedef E value_type;
30
 
    typedef @ iterator;
31
 
    typedef @ const_iterator;
32
 
    typedef A allocator_type;
33
 
    typedef @ size_type;
34
 
    
35
 
    StoragePolicy(const StoragePolicy& s);
36
 
    StoragePolicy(const A&);
37
 
    StoragePolicy(const E* s, size_type len, const A&);
38
 
    StoragePolicy(size_type len, E c, const A&);
39
 
    ~StoragePolicy();
40
 
 
41
 
    iterator begin();
42
 
    const_iterator begin() const;
43
 
    iterator end();
44
 
    const_iterator end() const;
45
 
    
46
 
    size_type size() const;
47
 
    size_type max_size() const;
48
 
    size_type capacity() const;
49
 
 
50
 
    void reserve(size_type res_arg);
51
 
 
52
 
    void append(const E* s, size_type sz);
53
 
    
54
 
    template <class InputIterator>
55
 
    void append(InputIterator b, InputIterator e);
56
 
 
57
 
    void resize(size_type newSize, E fill);
58
 
 
59
 
    void swap(StoragePolicy& rhs);
60
 
    
61
 
    const E* c_str() const;
62
 
    const E* data() const;
63
 
    
64
 
    A get_allocator() const;
65
 
};
66
 
////////////////////////////////////////////////////////////////////////////////
67
 
*/
68
 
 
69
 
#include <memory>
70
 
#include <algorithm>
71
 
#include <functional>
72
 
#include <cassert>
73
 
#include <limits>
74
 
#include <stdexcept>
75
 
#include "flex_string_details.h"
76
 
 
77
 
////////////////////////////////////////////////////////////////////////////////
78
 
// class template SmallStringOpt
79
 
// Builds the small string optimization over any other storage
80
 
////////////////////////////////////////////////////////////////////////////////
81
 
 
82
 
template <class Storage, unsigned int threshold, 
83
 
    typename Align = typename Storage::value_type*>
84
 
class SmallStringOpt
85
 
{
86
 
public:
87
 
    typedef typename Storage::value_type value_type;
88
 
    typedef value_type* iterator;
89
 
    typedef const value_type* const_iterator;
90
 
    typedef typename Storage::allocator_type allocator_type;
91
 
    typedef typename allocator_type::size_type size_type;
92
 
    typedef typename Storage::reference reference;
93
 
    
94
 
private:
95
 
    enum { temp1 = threshold * sizeof(value_type) > sizeof(Storage) 
96
 
        ? threshold  * sizeof(value_type) 
97
 
        : sizeof(Storage) };
98
 
    
99
 
    enum { temp2 = temp1 > sizeof(Align) ? temp1 : sizeof(Align) };
100
 
 
101
 
public:
102
 
    enum { maxSmallString = 
103
 
        (temp2 + sizeof(value_type) - 1) / sizeof(value_type) };
104
 
    
105
 
private:
106
 
    enum { magic = maxSmallString + 1 };
107
 
    
108
 
    union
109
 
    {
110
 
        mutable value_type buf_[maxSmallString + 1];
111
 
        Align align_;
112
 
    };
113
 
    
114
 
    Storage& GetStorage()
115
 
    {
116
 
        assert(buf_[maxSmallString] == magic);
117
 
        Storage* p = reinterpret_cast<Storage*>(&buf_[0]);
118
 
        return *p;
119
 
    }
120
 
    
121
 
    const Storage& GetStorage() const
122
 
    {
123
 
        assert(buf_[maxSmallString] == magic);
124
 
        const Storage *p = reinterpret_cast<const Storage*>(&buf_[0]);
125
 
        return *p;
126
 
    }
127
 
    
128
 
    bool Small() const
129
 
    {
130
 
        return buf_[maxSmallString] != magic;
131
 
    }
132
 
        
133
 
public:
134
 
    SmallStringOpt(const SmallStringOpt& s)
135
 
    {
136
 
        if (s.Small())
137
 
        {
138
 
            flex_string_details::pod_copy(
139
 
                s.buf_, 
140
 
                s.buf_ + s.size(), 
141
 
                buf_);
142
 
        }
143
 
        else
144
 
        {
145
 
            new(buf_) Storage(s.GetStorage());
146
 
        }
147
 
        buf_[maxSmallString] = s.buf_[maxSmallString];
148
 
    }
149
 
    
150
 
    SmallStringOpt(const allocator_type&)
151
 
    {
152
 
        buf_[maxSmallString] = maxSmallString;
153
 
    }
154
 
    
155
 
    SmallStringOpt(const value_type* s, size_type len, const allocator_type& a)
156
 
    {
157
 
        if (len <= maxSmallString)
158
 
        {
159
 
            flex_string_details::pod_copy(s, s + len, buf_);
160
 
            buf_[maxSmallString] = value_type(maxSmallString - len);
161
 
        }
162
 
        else
163
 
        {
164
 
            new(buf_) Storage(s, len, a);
165
 
            buf_[maxSmallString] = magic;
166
 
        }
167
 
    }
168
 
 
169
 
    SmallStringOpt(size_type len, value_type c, const allocator_type& a)
170
 
    {
171
 
        if (len <= maxSmallString)
172
 
        {
173
 
            flex_string_details::pod_fill(buf_, buf_ + len, c);
174
 
            buf_[maxSmallString] = value_type(maxSmallString - len);
175
 
        }
176
 
        else
177
 
        {
178
 
            new(buf_) Storage(len, c, a);
179
 
            buf_[maxSmallString] = magic;
180
 
        }
181
 
    }
182
 
    
183
 
    SmallStringOpt& operator=(const SmallStringOpt& rhs)
184
 
    {
185
 
        if (&rhs != this)
186
 
        {
187
 
            reserve(rhs.size());
188
 
            resize(0, 0);
189
 
            append(rhs.data(), rhs.data() + rhs.size());
190
 
        }
191
 
        return *this;
192
 
    }
193
 
 
194
 
    ~SmallStringOpt()
195
 
    {
196
 
        if (!Small()) GetStorage().~Storage();
197
 
    }
198
 
 
199
 
    iterator begin()
200
 
    {
201
 
        if (Small()) return buf_;
202
 
        return &*GetStorage().begin(); 
203
 
    }
204
 
    
205
 
    const_iterator begin() const
206
 
    {
207
 
        if (Small()) return buf_;
208
 
        return &*GetStorage().begin(); 
209
 
    }
210
 
    
211
 
    iterator end()
212
 
    {
213
 
        if (Small()) return buf_ + maxSmallString - buf_[maxSmallString];
214
 
        return &*GetStorage().end(); 
215
 
    }
216
 
    
217
 
    const_iterator end() const
218
 
    {
219
 
        if (Small()) return buf_ + maxSmallString - buf_[maxSmallString];
220
 
        return &*GetStorage().end(); 
221
 
    }
222
 
    
223
 
    size_type size() const
224
 
    {
225
 
        assert(!Small() || maxSmallString >= buf_[maxSmallString]);
226
 
        return Small() 
227
 
            ? maxSmallString - buf_[maxSmallString] 
228
 
            : GetStorage().size();
229
 
    }
230
 
 
231
 
    size_type max_size() const
232
 
    { return get_allocator().max_size(); }
233
 
 
234
 
    size_type capacity() const
235
 
    { return Small() ? maxSmallString : GetStorage().capacity(); }
236
 
 
237
 
    void reserve(size_type res_arg)
238
 
    {
239
 
        if (Small())
240
 
        {
241
 
            if (res_arg <= maxSmallString) return;
242
 
            SmallStringOpt temp(*this);
243
 
            this->~SmallStringOpt();
244
 
            new(buf_) Storage(temp.data(), temp.size(), 
245
 
                temp.get_allocator());
246
 
            buf_[maxSmallString] = magic;
247
 
            GetStorage().reserve(res_arg);
248
 
        }
249
 
        else
250
 
        {
251
 
            GetStorage().reserve(res_arg);
252
 
        }
253
 
        assert(capacity() >= res_arg);
254
 
    }
255
 
    
256
 
    template <class FwdIterator>
257
 
    void append(FwdIterator b, FwdIterator e)
258
 
    {
259
 
        if (!Small())
260
 
        {
261
 
            GetStorage().append(b, e);
262
 
        }
263
 
        else
264
 
        {
265
 
            // append to a small string
266
 
            const size_type 
267
 
                sz = std::distance(b, e),
268
 
                neededCapacity = maxSmallString - buf_[maxSmallString] + sz;
269
 
 
270
 
            if (maxSmallString < neededCapacity)
271
 
            {
272
 
                // need to change storage strategy
273
 
                allocator_type alloc;
274
 
                Storage temp(alloc);
275
 
                temp.reserve(neededCapacity);
276
 
                temp.append(buf_, buf_ + maxSmallString - buf_[maxSmallString]);
277
 
                temp.append(b, e);
278
 
                buf_[maxSmallString] = magic;
279
 
                new(buf_) Storage(temp.get_allocator());
280
 
                GetStorage().swap(temp);
281
 
            }
282
 
            else
283
 
            {
284
 
                std::copy(b, e, buf_ + maxSmallString - buf_[maxSmallString]);
285
 
                buf_[maxSmallString] = buf_[maxSmallString] - value_type(sz);
286
 
            }
287
 
        }
288
 
    }
289
 
 
290
 
    void resize(size_type n, value_type c)
291
 
    {
292
 
        if (Small())
293
 
        {
294
 
            if (n > maxSmallString)
295
 
            {
296
 
                // Small string resized to big string
297
 
                SmallStringOpt temp(*this); // can't throw
298
 
                // 11-17-2001: correct exception safety bug
299
 
                Storage newString(temp.data(), temp.size(), 
300
 
                    temp.get_allocator());
301
 
                newString.resize(n, c);
302
 
                // We make the reasonable assumption that an empty Storage
303
 
                //     constructor won't throw
304
 
                this->~SmallStringOpt();
305
 
                new(&buf_[0]) Storage(temp.get_allocator());
306
 
                buf_[maxSmallString] = value_type(magic);
307
 
                GetStorage().swap(newString);
308
 
            }
309
 
            else
310
 
            {
311
 
                // Small string resized to small string
312
 
                // 11-17-2001: bug fix: terminating zero not copied
313
 
                size_type toFill = n > size() ? n - size() : 0;
314
 
                flex_string_details::pod_fill(end(), end() + toFill, c);
315
 
                buf_[maxSmallString] = value_type(maxSmallString - n);
316
 
            }
317
 
        }
318
 
        else
319
 
        {
320
 
            if (n > maxSmallString)
321
 
            {
322
 
                // Big string resized to big string
323
 
                GetStorage().resize(n, c);
324
 
            }
325
 
            else
326
 
            {
327
 
                // Big string resized to small string
328
 
                // 11-17=2001: bug fix in the assertion below
329
 
                assert(capacity() > n);
330
 
                SmallStringOpt newObj(data(), n, get_allocator());
331
 
                newObj.swap(*this);
332
 
            }
333
 
        }
334
 
    }
335
 
 
336
 
    void swap(SmallStringOpt& rhs)
337
 
    {
338
 
        if (Small())
339
 
        {
340
 
            if (rhs.Small())
341
 
            {
342
 
                // Small swapped with small
343
 
                std::swap_ranges(buf_, buf_ + maxSmallString + 1, 
344
 
                    rhs.buf_);
345
 
            }
346
 
            else
347
 
            {
348
 
                // Small swapped with big
349
 
                // Make a copy of myself - can't throw
350
 
                SmallStringOpt temp(*this);
351
 
                // Nuke myself
352
 
                this->~SmallStringOpt();
353
 
                // Make an empty storage for myself (likely won't throw)
354
 
                new(buf_) Storage(0, value_type(), rhs.get_allocator());
355
 
                buf_[maxSmallString] = magic;
356
 
                // Recurse to this same function
357
 
                swap(rhs);
358
 
                // Nuke rhs
359
 
                rhs.~SmallStringOpt();
360
 
                // Build the new small string into rhs
361
 
                new(&rhs) SmallStringOpt(temp);
362
 
            }
363
 
        }
364
 
        else
365
 
        {
366
 
            if (rhs.Small())
367
 
            {
368
 
                // Big swapped with small
369
 
                // Already implemented, recurse with reversed args
370
 
                rhs.swap(*this);
371
 
            }
372
 
            else
373
 
            {
374
 
                // Big swapped with big
375
 
                GetStorage().swap(rhs.GetStorage());
376
 
            }
377
 
        }
378
 
    }
379
 
    
380
 
    const value_type* c_str() const
381
 
    { 
382
 
        if (!Small()) return GetStorage().c_str(); 
383
 
        buf_[maxSmallString - buf_[maxSmallString]] = value_type();
384
 
        return buf_;
385
 
    }
386
 
 
387
 
    const value_type* data() const
388
 
    { return Small() ? buf_ : GetStorage().data(); }
389
 
    
390
 
    allocator_type get_allocator() const
391
 
    { return allocator_type(); }
392
 
};
393
 
 
394
 
 
395
 
#endif // SMALL_STRING_OPT_INC_