libzypp 17.38.15

SetTracker.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
11#ifndef ZYPP_BASE_SETTRACKER_H
12#define ZYPP_BASE_SETTRACKER_H
13
14#include <iosfwd>
15#include <utility>
16#include <algorithm>
17#include <ostream>
18
20 namespace zypp
21{
23 namespace base
24 {
37 template <class TSet>
38 struct SetTracker
39 {
40 typedef TSet set_type;
41 typedef typename TSet::key_type key_type;
42 typedef typename TSet::value_type value_type;
43
45 SetTracker()
46 {}
47
49 SetTracker( set_type initial_r )
50 : _current( std::move(initial_r) )
51 {}
52
54
55
58 bool setInitial()
59 { _added.clear(); _removed.clear(); return false; }
60
64 bool setInitial( set_type new_r )
65 {
66 setInitial();
67 bool changed = ( new_r != _current );
68 if ( changed )
69 {
70 _current = std::move(new_r);
71 }
72 return changed;
73 }
74
75
79 bool set( set_type new_r )
80 {
81 bool changed = ( new_r != _current );
82 if ( changed )
83 {
84 // build the initial (cur-add+rem) set in _current
85 setDifference( _current, _added, _removed );
86 _current.swap( _removed );
87 _added.clear();
88 _removed.clear();
89
90 const set_type & initial( _current );
91 setDifference( initial, new_r, _removed );
92 setDifference( new_r, initial, _added );
93 _current.swap( new_r );
94 }
95 return changed;
96 }
97
101 bool add( const value_type & val_r )
102 {
103 bool done = _current.insert( val_r ).second;
104 if ( done )
105 {
106 if ( ! _removed.erase( val_r ) )
107 _added.insert( val_r );
108 }
109 return done;
110 }
111
115 bool remove( const value_type & val_r )
116 {
117 bool done = _current.erase( val_r );
118 if ( done )
119 {
120 if ( ! _added.erase( val_r ) )
121 _removed.insert( val_r );
122 }
123 return done;
124 }
125
126
128
129
130 bool contains( const key_type & key_r ) const { return find( _current, key_r ); }
131
133 bool wasAdded( const key_type & key_r ) const { return find( _added, key_r ); }
134
136 bool wasRemoved( const key_type & key_r ) const { return find( _removed, key_r ); }
137
138
140 const set_type & current() const { return _current; }
141
143 const set_type & added() const { return _added; }
144
146 const set_type & removed() const { return _removed; }
148
150
151
152 set_type & current() { return _current; }
153
155 set_type & added() { return _added; }
156
158 set_type & removed() { return _removed; }
160
161 private:
162
163 static bool find( const set_type & set_r, const key_type & key_r )
164 { return set_r.find( key_r ) != set_r.end(); }
165
166 template <class TORDERED_SET, typename enable_if = typename TORDERED_SET::key_compare>
167 static void setDifference( const TORDERED_SET & lhs, const TORDERED_SET & rhs, TORDERED_SET & result_r )
168 {
169 // std::set_difference requires ordered sets!
170 std::set_difference( lhs.begin(), lhs.end(), rhs.begin(), rhs.end(),
171 std::inserter( result_r, result_r.end() ),
172 typename TORDERED_SET::key_compare() );
173 }
174
175 template <class TUNORDERED_SET, typename enable_if = typename TUNORDERED_SET::hasher, typename = void>
176 static void setDifference( const TUNORDERED_SET & lhs, const TUNORDERED_SET & rhs, TUNORDERED_SET & result_r )
177 {
178 // std::set_difference requires ordered sets!
179 for ( const auto & l : lhs )
180 { if ( rhs.find( l ) == rhs.end() ) result_r.insert( l ); }
181 }
182
183 private:
184 set_type _current;
185 set_type _added;
186 set_type _removed;
187 };
188
190 template <class TSet>
191 std::ostream & operator<<( std::ostream & str, const SetTracker<TSet> & obj )
192 { return str << "set(" << obj.current().size() << "|+" << obj.added().size() << "|-" << obj.removed().size() << ')'; }
193
194 } // namespace base
196} // namespace zypp
198#endif // ZYPP_BASE_SETTRACKER_H
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp::base
Definition Random.cc:7
zypp::base::operator<<
std::ostream & operator<<(std::ostream &str, const SetTracker< TSet > &obj)
relates: SetTracker Stream output
Definition SetTracker.h:191
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::base::SetTracker
Track added/removed set items based on an initial set.
Definition SetTracker.h:39
zypp::base::SetTracker::wasRemoved
bool wasRemoved(const key_type &key_r) const
Whether val_r is tracked as removed.
Definition SetTracker.h:136
zypp::base::SetTracker::setInitial
bool setInitial(set_type new_r)
Start tracking a new set (discards previously tracked changes).
Definition SetTracker.h:64
zypp::base::SetTracker< IdStringSet >::key_type
IdStringSet::key_type key_type
Definition SetTracker.h:41
zypp::base::SetTracker< IdStringSet >::value_type
IdStringSet::value_type value_type
Definition SetTracker.h:42
zypp::base::SetTracker::current
const set_type & current() const
Return the current set.
Definition SetTracker.h:140
zypp::base::SetTracker::current
set_type & current()
Return the current set.
Definition SetTracker.h:152
zypp::base::SetTracker< IdStringSet >::setDifference
static void setDifference(const TORDERED_SET &lhs, const TORDERED_SET &rhs, TORDERED_SET &result_r)
Definition SetTracker.h:167
zypp::base::SetTracker< IdStringSet >::find
static bool find(const set_type &set_r, const key_type &key_r)
Definition SetTracker.h:163
zypp::base::SetTracker::SetTracker
SetTracker(set_type initial_r)
Ctor taking an initial set.
Definition SetTracker.h:49
zypp::base::SetTracker::set
bool set(set_type new_r)
Set a new_r set and track changes.
Definition SetTracker.h:79
zypp::base::SetTracker< IdStringSet >::_removed
set_type _removed
Definition SetTracker.h:186
zypp::base::SetTracker::add
bool add(const value_type &val_r)
Add an element to the set and track changes.
Definition SetTracker.h:101
zypp::base::SetTracker< IdStringSet >::set_type
IdStringSet set_type
Definition SetTracker.h:40
zypp::base::SetTracker::wasAdded
bool wasAdded(const key_type &key_r) const
Whether val_r is tracked as added.
Definition SetTracker.h:133
zypp::base::SetTracker::added
const set_type & added() const
Return the set of added items.
Definition SetTracker.h:143
zypp::base::SetTracker::added
set_type & added()
Return the set of added items.
Definition SetTracker.h:155
zypp::base::SetTracker::removed
set_type & removed()
Return the set of removed items.
Definition SetTracker.h:158
zypp::base::SetTracker::remove
bool remove(const value_type &val_r)
Remove an element from the set and track changes.
Definition SetTracker.h:115
zypp::base::SetTracker< IdStringSet >::_added
set_type _added
Definition SetTracker.h:185
zypp::base::SetTracker::setDifference
static void setDifference(const TUNORDERED_SET &lhs, const TUNORDERED_SET &rhs, TUNORDERED_SET &result_r)
Definition SetTracker.h:176
zypp::base::SetTracker::SetTracker
SetTracker()
Default Ctor: empty set.
Definition SetTracker.h:45
zypp::base::SetTracker::removed
const set_type & removed() const
Return the set of removed items.
Definition SetTracker.h:146
zypp::base::SetTracker::setInitial
bool setInitial()
(Re-)Start tracking the current set (discards previously tracked changes).
Definition SetTracker.h:58
zypp::base::SetTracker< IdStringSet >::_current
set_type _current
Definition SetTracker.h:184
zypp::base::SetTracker::contains
bool contains(const key_type &key_r) const
Whether val_r is in the set.
Definition SetTracker.h:130