summaryrefslogtreecommitdiffstats
blob: 85a3141026e7279f951bcfe6f4f65a76a3c9d7f2 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

// Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
// Copyright (C) 2005-2009 Daniel James
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// This contains the basic data structure, apart from the actual values. There's
// no construction or deconstruction here. So this only depends on the pointer
// type.

#ifndef BOOST_UNORDERED_DETAIL_NODE_HPP_INCLUDED
#define BOOST_UNORDERED_DETAIL_NODE_HPP_INCLUDED

#include <boost/config.hpp>
#include <boost/assert.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/unordered/detail/fwd.hpp>

#if BOOST_WORKAROUND(__BORLANDC__, <= 0X0582)
#define BOOST_UNORDERED_BORLAND_BOOL(x) (bool)(x)
#else
#define BOOST_UNORDERED_BORLAND_BOOL(x) x
#endif

namespace boost { namespace unordered_detail {

    ////////////////////////////////////////////////////////////////////////////
    // ungrouped node implementation
    
    template <class A>
    inline BOOST_DEDUCED_TYPENAME ungrouped_node_base<A>::node_ptr&
        ungrouped_node_base<A>::next_group(node_ptr ptr)
    {
        return ptr->next_;
    }

    template <class A>
    inline std::size_t ungrouped_node_base<A>::group_count(node_ptr)
    {
        return 1;
    }

    template <class A>
    inline void ungrouped_node_base<A>::add_to_bucket(node_ptr n, bucket& b)
    {
        n->next_ = b.next_;
        b.next_ = n;
    }

    template <class A>
    inline void ungrouped_node_base<A>::add_after_node(node_ptr n,
        node_ptr position)
    {
        n->next_ = position->next_;
        position->next_ = position;
    }
    
    template <class A>
    inline void ungrouped_node_base<A>::unlink_nodes(bucket& b,
        node_ptr begin, node_ptr end)
    {
        node_ptr* pos = &b.next_;
        while(*pos != begin) pos = &(*pos)->next_;
        *pos = end;
    }

    template <class A>
    inline void ungrouped_node_base<A>::unlink_nodes(bucket& b, node_ptr end)
    {
        b.next_ = end;
    }

    template <class A>
    inline void ungrouped_node_base<A>::unlink_node(bucket& b, node_ptr n)
    {
        unlink_nodes(b, n, n->next_);
    }

    ////////////////////////////////////////////////////////////////////////////
    // grouped node implementation
    
    // If ptr is the first element in a group, return pointer to next group.
    // Otherwise returns a pointer to ptr.
    template <class A>
    inline BOOST_DEDUCED_TYPENAME grouped_node_base<A>::node_ptr&
        grouped_node_base<A>::next_group(node_ptr ptr)
    {
        return get(ptr).group_prev_->next_;
    }

    template <class A>
    inline BOOST_DEDUCED_TYPENAME grouped_node_base<A>::node_ptr
        grouped_node_base<A>::first_in_group(node_ptr ptr)
    {
        while(next_group(ptr) == ptr)
            ptr = get(ptr).group_prev_;
        return ptr;
    }

    template <class A>
    inline std::size_t grouped_node_base<A>::group_count(node_ptr ptr)
    {
        node_ptr start = ptr;
        std::size_t size = 0;
        do {
            ++size;
            ptr = get(ptr).group_prev_;
        } while(ptr != start);
        return size;
    }

    template <class A>
    inline void grouped_node_base<A>::add_to_bucket(node_ptr n, bucket& b)
    {
        n->next_ = b.next_;
        get(n).group_prev_ = n;
        b.next_ = n;
    }

    template <class A>
    inline void grouped_node_base<A>::add_after_node(node_ptr n, node_ptr pos)
    {
        n->next_ = next_group(pos);
        get(n).group_prev_ = get(pos).group_prev_;
        next_group(pos) = n;
        get(pos).group_prev_ = n;
    }

    // Break a ciruclar list into two, with split as the beginning
    // of the second group (if split is at the beginning then don't
    // split).
    template <class A>
    inline BOOST_DEDUCED_TYPENAME grouped_node_base<A>::node_ptr
        grouped_node_base<A>::split_group(node_ptr split)
    {
        node_ptr first = first_in_group(split);
        if(first == split) return split;

        node_ptr last = get(first).group_prev_;
        get(first).group_prev_ = get(split).group_prev_;
        get(split).group_prev_ = last;

        return first;
    }

    template <class A>
    void grouped_node_base<A>::unlink_node(bucket& b, node_ptr n)
    {
        node_ptr next = n->next_;
        node_ptr* pos = &next_group(n);

        if(*pos != n) {
            // The node is at the beginning of a group.

            // Find the previous node pointer:
            pos = &b.next_;
            while(*pos != n) pos = &next_group(*pos);

            // Remove from group
            if(BOOST_UNORDERED_BORLAND_BOOL(next) &&
                get(next).group_prev_ == n)
            {
                get(next).group_prev_ = get(n).group_prev_;
            }
        }
        else if(BOOST_UNORDERED_BORLAND_BOOL(next) &&
            get(next).group_prev_ == n)
        {
            // The deleted node is not at the end of the group, so
            // change the link from the next node.
            get(next).group_prev_ = get(n).group_prev_;
        }
        else {
            // The deleted node is at the end of the group, so the
            // first node in the group is pointing to it.
            // Find that to change its pointer.
            node_ptr x = get(n).group_prev_;
            while(get(x).group_prev_ != n) {
                x = get(x).group_prev_;
            }
            get(x).group_prev_ = get(n).group_prev_;
        }
        *pos = next;
    }

    template <class A>
    void grouped_node_base<A>::unlink_nodes(bucket& b,
        node_ptr begin, node_ptr end)
    {
        node_ptr* pos = &next_group(begin);

        if(*pos != begin) {
            // The node is at the beginning of a group.

            // Find the previous node pointer:
            pos = &b.next_;
            while(*pos != begin) pos = &next_group(*pos);

            // Remove from group
            if(BOOST_UNORDERED_BORLAND_BOOL(end)) split_group(end);
        }
        else {
            node_ptr group1 = split_group(begin);
            if(BOOST_UNORDERED_BORLAND_BOOL(end)) {
                node_ptr group2 = split_group(end);

                if(begin == group2) {
                    node_ptr end1 = get(group1).group_prev_;
                    node_ptr end2 = get(group2).group_prev_;
                    get(group1).group_prev_ = end2;
                    get(group2).group_prev_ = end1;
                }
            }
        }
        *pos = end;
    }

    template <class A>
    void grouped_node_base<A>::unlink_nodes(bucket& b, node_ptr end)
    {
        split_group(end);
        b.next_ = end;
    }
}}

#endif