OSDN Git Service

817bcd0ca70c39b50beb5f9d52986315e0ff2027
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / pb_ds / detail / gp_hash_table_map_ / constructor_destructor_fn_imps.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006 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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
9 // version.
10
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
20
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction.  Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License.  This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
41
42 /**
43  * @file constructor_destructor_fn_imps.hpp
44  * Contains implementations of gp_ht_map_'s constructors, destructor,
45  *    and related functions.
46  */
47
48 PB_DS_CLASS_T_DEC
49 typename PB_DS_CLASS_C_DEC::entry_allocator
50 PB_DS_CLASS_C_DEC::s_entry_allocator;
51
52 PB_DS_CLASS_T_DEC
53 template<typename It>
54 void
55 PB_DS_CLASS_C_DEC::
56 copy_from_range(It first_it, It last_it)
57 {
58   while (first_it != last_it)
59     insert(*(first_it++));
60 }
61
62 PB_DS_CLASS_T_DEC
63 PB_DS_CLASS_C_DEC::
64 PB_DS_CLASS_NAME() :
65   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1)),
66   m_a_entries(s_entry_allocator.allocate(resize_base::get_nearest_larger_size(1))),
67   m_num_e(resize_base::get_nearest_larger_size(1)),
68   m_num_used_e(0)
69 {
70   initialize();
71   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
72 }
73
74 PB_DS_CLASS_T_DEC
75 PB_DS_CLASS_C_DEC::
76 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn)    :
77   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
78   m_a_entries(s_entry_allocator.allocate(resize_base::get_nearest_larger_size(1))),
79   m_num_e(resize_base::get_nearest_larger_size(1)),
80   m_num_used_e(0)
81 {
82   initialize();
83   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
84 }
85
86 PB_DS_CLASS_T_DEC
87 PB_DS_CLASS_C_DEC::
88 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn) :
89   hash_eq_fn_base(r_eq_fn),
90   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
91   m_a_entries(s_entry_allocator.allocate(resize_base::get_nearest_larger_size(1))),
92   m_num_e(resize_base::get_nearest_larger_size(1)),
93   m_num_used_e(0)
94 {
95   initialize();
96   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
97 }
98
99 PB_DS_CLASS_T_DEC
100 PB_DS_CLASS_C_DEC::
101 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Probe_Fn& r_comb_hash_fn) :
102   hash_eq_fn_base(r_eq_fn),
103   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
104                        r_hash_fn, r_comb_hash_fn),
105   m_a_entries(s_entry_allocator.allocate(resize_base::get_nearest_larger_size(1))),
106   m_num_e(resize_base::get_nearest_larger_size(1)),
107   m_num_used_e(0)
108 {
109   initialize();
110   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
111 }
112
113 PB_DS_CLASS_T_DEC
114 PB_DS_CLASS_C_DEC::
115 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Probe_Fn& r_comb_hash_fn, const Probe_Fn& r_probe_fn) :
116   hash_eq_fn_base(r_eq_fn),
117   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
118                        r_hash_fn, r_comb_hash_fn, r_probe_fn),
119   m_a_entries(s_entry_allocator.allocate(resize_base::get_nearest_larger_size(1))),
120   m_num_e(resize_base::get_nearest_larger_size(1)),
121   m_num_used_e(0)
122 {
123   initialize();
124   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
125 }
126
127 PB_DS_CLASS_T_DEC
128 PB_DS_CLASS_C_DEC::
129 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Probe_Fn& r_comb_hash_fn, const Probe_Fn& r_probe_fn, const Resize_Policy& r_resize_policy) :
130   hash_eq_fn_base(r_eq_fn),
131   resize_base(r_resize_policy),
132   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
133                        r_hash_fn, r_comb_hash_fn, r_probe_fn),
134   m_a_entries(s_entry_allocator.allocate(resize_base::get_nearest_larger_size(1))),
135   m_num_e(resize_base::get_nearest_larger_size(1)),
136   m_num_used_e(0)
137 {
138   initialize();
139   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
140 }
141
142 PB_DS_CLASS_T_DEC
143 PB_DS_CLASS_C_DEC::
144 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
145 #ifdef _GLIBCXX_DEBUG
146   map_debug_base(other),
147 #endif 
148   hash_eq_fn_base(other),
149   resize_base(other),
150   ranged_probe_fn_base(other),
151   m_a_entries(s_entry_allocator.allocate(other.m_num_e)),
152   m_num_e(other.m_num_e),
153   m_num_used_e(other.m_num_used_e)
154 {
155   for (size_type i = 0; i < m_num_e; ++i)
156     m_a_entries[i].m_stat = (entry_status)empty_entry_status;
157
158   try
159     {
160       for (size_type i = 0; i < m_num_e; ++i)
161         {
162           m_a_entries[i].m_stat = other.m_a_entries[i].m_stat;
163           if (m_a_entries[i].m_stat == valid_entry_status)
164             new (m_a_entries + i) entry(other.m_a_entries[i]);
165         }
166     }
167   catch(...)
168     {
169       deallocate_all();
170       throw;
171     }
172
173   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
174     }
175
176 PB_DS_CLASS_T_DEC
177 PB_DS_CLASS_C_DEC::
178 ~PB_DS_CLASS_NAME()
179 {
180   deallocate_all();
181 }
182
183 PB_DS_CLASS_T_DEC
184 void
185 PB_DS_CLASS_C_DEC::
186 swap(PB_DS_CLASS_C_DEC& other)
187 {
188   _GLIBCXX_DEBUG_ONLY(assert_valid());
189   _GLIBCXX_DEBUG_ONLY(other.assert_valid());
190   std::swap(m_a_entries, other.m_a_entries);
191   std::swap(m_num_e, other.m_num_e);
192   std::swap(m_num_used_e, other.m_num_used_e);
193   ranged_probe_fn_base::swap(other);
194   hash_eq_fn_base::swap(other);
195   resize_base::swap(other);
196   _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other));
197   _GLIBCXX_DEBUG_ONLY(assert_valid());
198   _GLIBCXX_DEBUG_ONLY(other.assert_valid());
199 }
200
201 PB_DS_CLASS_T_DEC
202 void
203 PB_DS_CLASS_C_DEC::
204 deallocate_all()
205 {
206   clear();
207   erase_all_valid_entries(m_a_entries, m_num_e);
208   s_entry_allocator.deallocate(m_a_entries, m_num_e);
209 }
210
211 PB_DS_CLASS_T_DEC
212 void
213 PB_DS_CLASS_C_DEC::
214 erase_all_valid_entries(entry_array a_entries_resized, size_type size)
215 {
216   for (size_type pos = 0; pos < size; ++pos)
217     {
218       entry_pointer p_e =& a_entries_resized[pos];
219       if (p_e->m_stat == valid_entry_status)
220         p_e->m_value.~value_type();
221     }
222 }
223
224 PB_DS_CLASS_T_DEC
225 void
226 PB_DS_CLASS_C_DEC::
227 initialize()
228 {
229   Resize_Policy::notify_resized(m_num_e);
230   Resize_Policy::notify_cleared();
231   ranged_probe_fn_base::notify_resized(m_num_e);
232   for (size_type i = 0; i < m_num_e; ++i)
233     m_a_entries[i].m_stat = (entry_status)empty_entry_status;
234 }
235