]>
Commit | Line | Data |
---|---|---|
fd1e1726 BK |
1 | // -*- C++ -*- |
2 | ||
3 | // Copyright (C) 2005 Free Software Foundation, Inc. | |
4 | // | |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 2, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
17 | // with this library; see the file COPYING. If not, write to the Free | |
83f51799 | 18 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
fd1e1726 BK |
19 | // USA. |
20 | ||
21 | // As a special exception, you may use this file as part of a free software | |
22 | // library without restriction. Specifically, if other files instantiate | |
23 | // templates or use macros or inline functions from this file, or you compile | |
24 | // this file and link it with other files to produce an executable, this | |
25 | // file does not by itself cause the resulting executable to be covered by | |
26 | // the GNU General Public License. This exception does not however | |
27 | // invalidate any other reasons why the executable file might be covered by | |
28 | // the GNU General Public License. | |
29 | ||
30 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
31 | ||
32 | // Permission to use, copy, modify, sell, and distribute this software | |
33 | // is hereby granted without fee, provided that the above copyright | |
34 | // notice appears in all copies, and that both that copyright notice and | |
35 | // this permission notice appear in supporting documentation. None of | |
36 | // the above authors, nor IBM Haifa Research Laboratories, make any | |
37 | // representation about the suitability of this software for any | |
38 | // purpose. It is provided "as is" without express or implied warranty. | |
39 | ||
40 | /** | |
41 | * @file split_join_fn_imps.hpp | |
42 | * Contains an implementation class for ov_tree_. | |
43 | */ | |
44 | ||
45 | PB_ASSOC_CLASS_T_DEC | |
46 | void | |
47 | PB_ASSOC_CLASS_C_DEC:: | |
48 | split(const_key_reference r_key, PB_ASSOC_CLASS_C_DEC& r_other) | |
49 | { | |
50 | PB_ASSOC_DBG_ONLY(assert_valid();) | |
51 | PB_ASSOC_DBG_ONLY(r_other.assert_valid();) | |
52 | ||
53 | if (m_size == 0) | |
54 | { | |
55 | r_other.clear(); | |
56 | ||
57 | PB_ASSOC_DBG_ONLY(assert_valid();) | |
58 | PB_ASSOC_DBG_ONLY(r_other.assert_valid();) | |
59 | ||
60 | return; | |
61 | } | |
62 | ||
63 | if (Cmp_Fn::operator()(r_key, PB_ASSOC_V2F(*begin()))) | |
64 | { | |
65 | swap(r_other); | |
66 | ||
67 | PB_ASSOC_DBG_ONLY(assert_valid();) | |
68 | PB_ASSOC_DBG_ONLY(r_other.assert_valid();) | |
69 | ||
70 | return; | |
71 | } | |
72 | ||
73 | if (!Cmp_Fn::operator()( | |
74 | r_key, | |
75 | PB_ASSOC_V2F(*(end() - 1)))) | |
76 | { | |
77 | PB_ASSOC_DBG_ONLY(assert_valid();) | |
78 | PB_ASSOC_DBG_ONLY(r_other.assert_valid();) | |
79 | ||
80 | return; | |
81 | } | |
82 | ||
83 | if (m_size == 1) | |
84 | { | |
85 | swap(r_other); | |
86 | ||
87 | PB_ASSOC_DBG_ONLY(assert_valid();) | |
88 | PB_ASSOC_DBG_ONLY(r_other.assert_valid();) | |
89 | ||
90 | return; | |
91 | } | |
92 | ||
93 | #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_ | |
94 | for (const_iterator it = begin(); it != end(); ++it) | |
95 | if (Cmp_Fn::operator()( | |
96 | r_key, | |
97 | PB_ASSOC_V2F(*it))) | |
98 | { | |
99 | my_map_debug_base::erase_existing(PB_ASSOC_V2F(*it)); | |
100 | r_other.my_map_debug_base::insert_new(PB_ASSOC_V2F(*it)); | |
101 | } | |
102 | #endif // PB_ASSOC_BIN_SEARCH_TREE_DEBUG_ | |
103 | ||
104 | iterator it = upper_bound(r_key); | |
105 | ||
106 | PB_ASSOC_CLASS_C_DEC new_other(r_other, r_other); | |
107 | ||
108 | new_other.copy_from_ordered_range(it, end()); | |
109 | ||
110 | PB_ASSOC_CLASS_C_DEC new_this(*this, * this); | |
111 | ||
112 | new_this.copy_from_ordered_range(begin(), it); | |
113 | ||
114 | // No exceptions from this point. | |
115 | ||
116 | r_other.update(r_other.node_begin(), (Node_Updator* )(&r_other)); | |
117 | ||
118 | update(node_begin(), (Node_Updator* )this); | |
119 | ||
120 | r_other.swap(new_other); | |
121 | ||
122 | swap(new_this); | |
123 | ||
124 | PB_ASSOC_DBG_ONLY(assert_valid();) | |
125 | PB_ASSOC_DBG_ONLY(r_other.assert_valid();) | |
126 | } | |
127 | ||
128 | PB_ASSOC_CLASS_T_DEC | |
129 | void | |
130 | PB_ASSOC_CLASS_C_DEC:: | |
131 | join(PB_ASSOC_CLASS_C_DEC& r_other) | |
132 | { | |
133 | PB_ASSOC_DBG_ONLY(assert_valid();) | |
134 | PB_ASSOC_DBG_ONLY(r_other.assert_valid();) | |
135 | ||
136 | if (r_other.m_size == 0) | |
137 | return; | |
138 | ||
139 | if (m_size == 0) | |
140 | { | |
141 | swap(r_other); | |
142 | ||
143 | return; | |
144 | } | |
145 | ||
146 | const bool greater = Cmp_Fn::operator()( | |
147 | PB_ASSOC_V2F(*(end() - 1)), | |
148 | PB_ASSOC_V2F(*r_other.begin())); | |
149 | ||
150 | const bool lesser = Cmp_Fn::operator()( | |
151 | PB_ASSOC_V2F(*(r_other.end() - 1)), | |
152 | PB_ASSOC_V2F(*begin())); | |
153 | ||
154 | if (!greater&& !lesser) | |
155 | throw cannot_join(); | |
156 | ||
157 | #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_ | |
158 | for (const_iterator other_it = r_other.begin(); other_it != r_other.end(); | |
159 | ++other_it) | |
160 | { | |
161 | my_map_debug_base::insert_new(PB_ASSOC_V2F(*other_it)); | |
162 | r_other.my_map_debug_base::erase_existing(PB_ASSOC_V2F(*other_it)); | |
163 | } | |
164 | #endif // PB_ASSOC_BIN_SEARCH_TREE_DEBUG_ | |
165 | ||
166 | PB_ASSOC_CLASS_C_DEC new_this(*this, * this); | |
167 | ||
168 | if (greater) | |
169 | new_this.copy_from_ordered_range( | |
170 | begin(), | |
171 | end(), | |
172 | r_other.begin(), | |
173 | r_other.end()); | |
174 | else | |
175 | new_this.copy_from_ordered_range( | |
176 | r_other.begin(), | |
177 | r_other.end(), | |
178 | begin(), | |
179 | end()); | |
180 | ||
181 | // No exceptions from this point. | |
182 | ||
183 | swap(new_this); | |
184 | ||
185 | r_other.clear(); | |
186 | ||
187 | PB_ASSOC_DBG_ONLY(assert_valid();) | |
188 | PB_ASSOC_DBG_ONLY(r_other.assert_valid();) | |
189 | } |