~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to extra/yassl/taocrypt/mySTL/vector.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2000-2007 MySQL AB
 
3
 
 
4
   This program is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation; version 2 of the License.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; see the file COPYING. If not, write to the
 
15
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 
16
   MA  02110-1301  USA.
 
17
*/
 
18
 
 
19
 
 
20
/* mySTL vector implements simple vector, w/ swap
 
21
 *
 
22
 */
 
23
 
 
24
#ifndef mySTL_VECTOR_HPP
 
25
#define mySTL_VECTOR_HPP
 
26
 
 
27
#include "helpers.hpp"    // construct, destory, fill, etc.
 
28
#include "algorithm.hpp"  // swap
 
29
#include <assert.h>       // assert
 
30
 
 
31
 
 
32
namespace mySTL {
 
33
 
 
34
 
 
35
template <typename T>
 
36
struct vector_base {
 
37
    T* start_;
 
38
    T* finish_;
 
39
    T* end_of_storage_;
 
40
 
 
41
    vector_base() : start_(0), finish_(0), end_of_storage_(0) {}
 
42
    vector_base(size_t n)
 
43
    {
 
44
        start_ = GetArrayMemory<T>(n);
 
45
        finish_ = start_;
 
46
        end_of_storage_ = start_ + n;
 
47
    }
 
48
 
 
49
    ~vector_base() 
 
50
    { 
 
51
        FreeArrayMemory(start_);
 
52
    }
 
53
 
 
54
    void Swap(vector_base& that) 
 
55
    {
 
56
        swap(start_, that.start_);
 
57
        swap(finish_, that.finish_);
 
58
        swap(end_of_storage_, that.end_of_storage_);
 
59
    }
 
60
};
 
61
 
 
62
 
 
63
 
 
64
template <typename T>
 
65
class vector {
 
66
public:
 
67
    typedef T*       iterator;
 
68
    typedef const T* const_iterator;
 
69
 
 
70
    vector() {}
 
71
    explicit vector(size_t n) : vec_(n) 
 
72
    { 
 
73
        vec_.finish_ = uninit_fill_n(vec_.start_, n, T()); 
 
74
    }
 
75
 
 
76
    ~vector() { destroy(vec_.start_, vec_.finish_); }
 
77
 
 
78
    vector(const vector& other) : vec_(other.size())
 
79
    {
 
80
        vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_,
 
81
                                   vec_.start_);   
 
82
    }
 
83
 
 
84
    size_t capacity() const { return vec_.end_of_storage_ - vec_.start_; }
 
85
 
 
86
    size_t size() const { return vec_.finish_ - vec_.start_; }
 
87
 
 
88
    T&       operator[](size_t idx)       { return *(vec_.start_ + idx); }
 
89
    const T& operator[](size_t idx) const { return *(vec_.start_ + idx); }
 
90
 
 
91
    const T* begin() const { return vec_.start_; }
 
92
    const T* end()   const { return vec_.finish_; }
 
93
 
 
94
    void push_back(const T& v)
 
95
    {
 
96
        if (vec_.finish_ != vec_.end_of_storage_) {
 
97
            construct(vec_.finish_, v);
 
98
            ++vec_.finish_;
 
99
        }
 
100
        else {
 
101
            vector tmp(size() * 2 + 1, *this);
 
102
            construct(tmp.vec_.finish_, v);
 
103
            ++tmp.vec_.finish_;
 
104
            Swap(tmp);
 
105
        }  
 
106
    }
 
107
 
 
108
    void resize(size_t n, const T& v)
 
109
    {
 
110
        if (n == size()) return;
 
111
 
 
112
        if (n < size()) {
 
113
            T* first = vec_.start_ + n;
 
114
            destroy(first, vec_.finish_);
 
115
            vec_.finish_ -= vec_.finish_ - first;
 
116
        }
 
117
        else {
 
118
            vector tmp(n, *this);
 
119
            tmp.vec_.finish_ = uninit_fill_n(tmp.vec_.finish_, n - size(), v);
 
120
            Swap(tmp);
 
121
        }
 
122
    }
 
123
 
 
124
    void reserve(size_t n)
 
125
    {
 
126
        if (capacity() < n) {
 
127
            vector tmp(n, *this);
 
128
            Swap(tmp);
 
129
        }
 
130
    }
 
131
 
 
132
    void Swap(vector& that)
 
133
    {
 
134
        vec_.Swap(that.vec_);
 
135
    }
 
136
private:
 
137
    vector_base<T> vec_;
 
138
 
 
139
    vector& operator=(const vector&);   // hide assign
 
140
 
 
141
    // for growing, n must be bigger than other size
 
142
    vector(size_t n, const vector& other) : vec_(n)
 
143
    {
 
144
        assert(n > other.size());
 
145
        vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_,
 
146
                                   vec_.start_);   
 
147
    }
 
148
};
 
149
 
 
150
 
 
151
 
 
152
} // namespace mySTL
 
153
 
 
154
#endif // mySTL_VECTOR_HPP