summaryrefslogtreecommitdiff
path: root/o3tl/inc/o3tl/sorted_vector.hxx
blob: 153e9950b77a6ba8ba90e6f59a6cfff366c85f5d (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_O3TL_SORTED_VECTOR_HXX
#define INCLUDED_O3TL_SORTED_VECTOR_HXX

#include <vector>
#include <functional>
#include <algorithm>

namespace o3tl
{

/** Helper template */
template <class Value, class Compare>
class sorted_vector_compare : public Compare
{
public:
    bool operator()(const Value& lhs, const Value& rhs) const
    {
        return Compare::operator()(lhs, rhs);
    }
};

/** Represents a sorted vector of values.

    @tpl Value class of item to be stored in container
    @tpl Compare comparison method
*/
template <class Value, class Compare = std::less<Value> >
class sorted_vector
    : private std::vector<Value>
    , private sorted_vector_compare<Value, Compare>
{
private:
    typedef typename std::vector<Value>::iterator  iterator;
public:
    typedef typename std::vector<Value>::const_iterator const_iterator;
    typedef typename std::vector<Value>::size_type size_type;
    typedef sorted_vector_compare<Value, Compare> MyCompare;

    using std::vector<Value>::clear;
    using std::vector<Value>::erase;
    using std::vector<Value>::empty;
    using std::vector<Value>::size;

    // MODIFIERS

    std::pair<const_iterator,bool> insert( const Value& x )
    {
        iterator it = _lower_bound( x );
        if( it == std::vector<Value>::end() || less_than(x, *it) )
        {
            it = std::vector<Value>::insert( it, x );
            return std::make_pair( it, true );
        }
        return std::make_pair( it, false );
    }

    size_type erase( const Value& x )
    {
        iterator it = _lower_bound( x );
        if( it != std::vector<Value>::end() && !less_than(x, *it) )
        {
            erase( it );
            return 1;
        }
        return 0;
    }

    // ACCESSORS

    // Only return a const iterator, so that the vector cannot be directly updated.
    const_iterator begin() const
    {
        return std::vector<Value>::begin();
    }

    // Only return a const iterator, so that the vector cannot be directly updated.
    const_iterator end() const
    {
        return std::vector<Value>::end();
    }

    // Return a value rather than a reference, so that the vector cannot be directly updated,
    // and the sorted invariant violated.
    Value front()
    {
        return std::vector<Value>::front();
    }

    const Value& front() const
    {
        return std::vector<Value>::front();
    }

    // Return a value rather than a reference, so that the vector cannot be directly updated,
    // and the sorted invariant violated.
    Value back()
    {
        return std::vector<Value>::back();
    }

    const Value& back() const
    {
        return std::vector<Value>::back();
    }

    // Return a value rather than a reference, so that the vector cannot be directly updated,
    // and the sorted invariant violated.
    Value operator[]( size_t index )
    {
        return std::vector<Value>::operator[]( index );
    }

    const Value& operator[]( size_t index ) const
    {
        return std::vector<Value>::operator[]( index );
    }

    // OPERATIONS

    const_iterator lower_bound( const Value& x ) const
    {
        const MyCompare& me = *this;
        return std::lower_bound( std::vector<Value>::begin(), std::vector<Value>::end(), x, me );
    }

    /* Searches the container for an element with a value of x
     * and returns an iterator to it if found, otherwise it returns an
     * iterator to sorted_vector::end (the element past the end of the container).
     *
     * Only return a const iterator, so that the vector cannot be directly updated.
     */
    const_iterator find( const Value& x ) const
    {
        const_iterator it = lower_bound( x );
        if( it == std::vector<Value>::end() || less_than(x, *it) )
        {
            return std::vector<Value>::end();
        }
        return it;
    }

    void insert( sorted_vector<Value,Compare> &rOther )
    {
       // optimisation for the rather common case that we are overwriting this with the contents
       // of another sorted vector
       if ( empty() )
       {
           std::vector<Value>::insert( _begin(), rOther._begin(), rOther._end() );
       }
       else
           for( const_iterator it = rOther.begin(); it != rOther.end(); ++it )
               insert( *it );
    }

    /* Clear() elements in the vector, and free them one by one. */
    void DeleteAndDestroyAll()
    {
        for( const_iterator it = begin(); it != end(); ++it )
            delete *it;
        clear();
    }

private:
    /** just makes the code easier to read */
    bool less_than(const Value& lhs, const Value& rhs) const
    {
        const MyCompare& me = *this;
        return me.operator()(lhs, rhs);
    }

    iterator _lower_bound( const Value& x )
    {
        const MyCompare& me = *this;
        return std::lower_bound( std::vector<Value>::begin(), std::vector<Value>::end(), x, me );
    }

    typename std::vector<Value>::iterator _begin() { return std::vector<Value>::begin(); }
    typename std::vector<Value>::iterator _end() { return std::vector<Value>::end(); }

};


/** Implements an ordering function over a pointer, where the comparison uses the < operator on the pointed-to types.
    Very useful for the cases where we put pointers to objects inside a sorted_vector.
*/
template <class T> struct less_ptr_to : public std::binary_function <T*,T*,bool>
{
    bool operator() ( T* const& lhs, T* const& rhs ) const
    {
        return (*lhs) < (*rhs);
    }
};


}   // namespace o3tl
#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */