libstdc++
list_update_map_/erase_fn_imps.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2020 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26 
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
34 // warranty.
35 
36 /**
37  * @file list_update_map_/erase_fn_imps.hpp
38  * Contains implementations of lu_map_.
39  */
40 
41 #ifdef PB_DS_CLASS_C_DEC
42 
43 PB_DS_CLASS_T_DEC
44 inline bool
45 PB_DS_CLASS_C_DEC::
46 erase(key_const_reference r_key)
47 {
48  PB_DS_ASSERT_VALID((*this))
49  if (m_p_l == 0)
50  return false;
51 
52  if (s_eq_fn(r_key, PB_DS_V2F(m_p_l->m_value)))
53  {
54  entry_pointer p_next = m_p_l->m_p_next;
55  actual_erase_entry(m_p_l);
56  m_p_l = p_next;
57  return true;
58  }
59 
60  entry_pointer p_l = m_p_l;
61  while (p_l->m_p_next != 0)
62  if (s_eq_fn(r_key, PB_DS_V2F(p_l->m_p_next->m_value)))
63  {
64  erase_next(p_l);
65  return true;
66  }
67  else
68  p_l = p_l->m_p_next;
69  return false;
70 }
71 
72 PB_DS_CLASS_T_DEC
73 void
74 PB_DS_CLASS_C_DEC::
75 clear()
76 {
77  deallocate_all();
78 }
79 
80 PB_DS_CLASS_T_DEC
81 template<typename Pred>
82 inline typename PB_DS_CLASS_C_DEC::size_type
83 PB_DS_CLASS_C_DEC::
84 erase_if(Pred pred)
85 {
86  PB_DS_ASSERT_VALID((*this))
87  size_type num_ersd = 0;
88  while (m_p_l != 0 && pred(m_p_l->m_value))
89  {
90  entry_pointer p_next = m_p_l->m_p_next;
91  ++num_ersd;
92  actual_erase_entry(m_p_l);
93  m_p_l = p_next;
94  }
95 
96  if (m_p_l == 0)
97  return num_ersd;
98 
99  entry_pointer p_l = m_p_l;
100  while (p_l->m_p_next != 0)
101  {
102  if (pred(p_l->m_p_next->m_value))
103  {
104  ++num_ersd;
105  erase_next(p_l);
106  }
107  else
108  p_l = p_l->m_p_next;
109  }
110 
111  PB_DS_ASSERT_VALID((*this))
112  return num_ersd;
113 }
114 
115 PB_DS_CLASS_T_DEC
116 void
117 PB_DS_CLASS_C_DEC::
118 erase_next(entry_pointer p_l)
119 {
120  _GLIBCXX_DEBUG_ASSERT(p_l != 0);
121  _GLIBCXX_DEBUG_ASSERT(p_l->m_p_next != 0);
122  entry_pointer p_next_l = p_l->m_p_next->m_p_next;
123  actual_erase_entry(p_l->m_p_next);
124  p_l->m_p_next = p_next_l;
125 }
126 
127 PB_DS_CLASS_T_DEC
128 void
129 PB_DS_CLASS_C_DEC::
130 actual_erase_entry(entry_pointer p_l)
131 {
132  _GLIBCXX_DEBUG_ONLY(debug_base::erase_existing(PB_DS_V2F(p_l->m_value));)
133  p_l->~entry();
134  s_entry_allocator.deallocate(p_l, 1);
135 }
136 
137 #endif