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
48PB_DS_CLASS_T_DEC
49typename PB_DS_CLASS_C_DEC::entry_allocator
50PB_DS_CLASS_C_DEC::s_entry_allocator;
51
52PB_DS_CLASS_T_DEC
53template<typename It>
54void
55PB_DS_CLASS_C_DEC::
56copy_from_range(It first_it, It last_it)
57{
58  while (first_it != last_it)
59    insert(*(first_it++));
60}
61
62PB_DS_CLASS_T_DEC
63PB_DS_CLASS_C_DEC::
64PB_DS_CLASS_NAME()
65: ranged_probe_fn_base(resize_base::get_nearest_larger_size(1)),
66  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
67  m_entries(s_entry_allocator.allocate(m_num_e))
68{
69  initialize();
70  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
71}
72
73PB_DS_CLASS_T_DEC
74PB_DS_CLASS_C_DEC::
75PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn)
76: ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
77  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
78  m_entries(s_entry_allocator.allocate(m_num_e))
79{
80  initialize();
81  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
82}
83
84PB_DS_CLASS_T_DEC
85PB_DS_CLASS_C_DEC::
86PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn)
87: hash_eq_fn_base(r_eq_fn),
88  ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
89  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
90  m_entries(s_entry_allocator.allocate(m_num_e))
91{
92  initialize();
93  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
94}
95
96PB_DS_CLASS_T_DEC
97PB_DS_CLASS_C_DEC::
98PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn,
99		 const Comb_Probe_Fn& r_comb_hash_fn)
100: hash_eq_fn_base(r_eq_fn),
101  ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
102		       r_hash_fn, r_comb_hash_fn),
103  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
104  m_entries(s_entry_allocator.allocate(m_num_e))
105{
106  initialize();
107  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
108}
109
110PB_DS_CLASS_T_DEC
111PB_DS_CLASS_C_DEC::
112PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn,
113		 const Comb_Probe_Fn& comb_hash_fn, const Probe_Fn& prober)
114: hash_eq_fn_base(r_eq_fn),
115  ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
116		       r_hash_fn, comb_hash_fn, prober),
117  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
118  m_entries(s_entry_allocator.allocate(m_num_e))
119{
120  initialize();
121  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
122}
123
124PB_DS_CLASS_T_DEC
125PB_DS_CLASS_C_DEC::
126PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn,
127		 const Comb_Probe_Fn& comb_hash_fn, const Probe_Fn& prober,
128		 const Resize_Policy& r_resize_policy)
129: hash_eq_fn_base(r_eq_fn), resize_base(r_resize_policy),
130  ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
131		       r_hash_fn, comb_hash_fn, prober),
132  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
133  m_entries(s_entry_allocator.allocate(m_num_e))
134{
135  initialize();
136  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
137}
138
139PB_DS_CLASS_T_DEC
140PB_DS_CLASS_C_DEC::
141PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
142#ifdef _GLIBCXX_DEBUG
143  map_debug_base(other),
144#endif
145  hash_eq_fn_base(other),
146  resize_base(other),
147  ranged_probe_fn_base(other),
148  m_num_e(other.m_num_e),
149  m_num_used_e(other.m_num_used_e),
150  m_entries(s_entry_allocator.allocate(m_num_e))
151{
152  for (size_type i = 0; i < m_num_e; ++i)
153    m_entries[i].m_stat = (entry_status)empty_entry_status;
154
155  try
156    {
157      for (size_type i = 0; i < m_num_e; ++i)
158        {
159	  m_entries[i].m_stat = other.m_entries[i].m_stat;
160	  if (m_entries[i].m_stat == valid_entry_status)
161	    new (m_entries + i) entry(other.m_entries[i]);
162        }
163    }
164  catch(...)
165    {
166      deallocate_all();
167      __throw_exception_again;
168    }
169  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
170}
171
172PB_DS_CLASS_T_DEC
173PB_DS_CLASS_C_DEC::
174~PB_DS_CLASS_NAME()
175{ deallocate_all(); }
176
177PB_DS_CLASS_T_DEC
178void
179PB_DS_CLASS_C_DEC::
180swap(PB_DS_CLASS_C_DEC& other)
181{
182  _GLIBCXX_DEBUG_ONLY(assert_valid());
183  _GLIBCXX_DEBUG_ONLY(other.assert_valid());
184  std::swap(m_num_e, other.m_num_e);
185  std::swap(m_num_used_e, other.m_num_used_e);
186  std::swap(m_entries, other.m_entries);
187  ranged_probe_fn_base::swap(other);
188  hash_eq_fn_base::swap(other);
189  resize_base::swap(other);
190  _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other));
191  _GLIBCXX_DEBUG_ONLY(assert_valid());
192  _GLIBCXX_DEBUG_ONLY(other.assert_valid());
193}
194
195PB_DS_CLASS_T_DEC
196void
197PB_DS_CLASS_C_DEC::
198deallocate_all()
199{
200  clear();
201  erase_all_valid_entries(m_entries, m_num_e);
202  s_entry_allocator.deallocate(m_entries, m_num_e);
203}
204
205PB_DS_CLASS_T_DEC
206void
207PB_DS_CLASS_C_DEC::
208erase_all_valid_entries(entry_array a_entries_resized, size_type len)
209{
210  for (size_type pos = 0; pos < len; ++pos)
211    {
212      entry_pointer p_e = &a_entries_resized[pos];
213      if (p_e->m_stat == valid_entry_status)
214	p_e->m_value.~value_type();
215    }
216}
217
218PB_DS_CLASS_T_DEC
219void
220PB_DS_CLASS_C_DEC::
221initialize()
222{
223  Resize_Policy::notify_resized(m_num_e);
224  Resize_Policy::notify_cleared();
225  ranged_probe_fn_base::notify_resized(m_num_e);
226  for (size_type i = 0; i < m_num_e; ++i)
227    m_entries[i].m_stat = empty_entry_status;
228}
229
230