libzypp 17.31.0
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
19namespace zypp
20{
22 namespace base
23 {
36 template <class TSet>
38 {
39 typedef TSet set_type;
40 typedef typename TSet::key_type key_type;
41 typedef typename TSet::value_type value_type;
42
45 {}
46
48 SetTracker( set_type initial_r )
49 : _current( std::move(initial_r) )
50 {}
51
53
54
58 { _added.clear(); _removed.clear(); return false; }
59
63 bool setInitial( set_type new_r )
64 {
65 setInitial();
66 bool changed = ( new_r != _current );
67 if ( changed )
68 {
69 _current = std::move(new_r);
70 }
71 return changed;
72 }
73
74
78 bool set( set_type new_r )
79 {
80 bool changed = ( new_r != _current );
81 if ( changed )
82 {
83 // build the initial (cur-add+rem) set in _current
85 _current.swap( _removed );
86 _added.clear();
87 _removed.clear();
88
89 const set_type & initial( _current );
90 setDifference( initial, new_r, _removed );
91 setDifference( new_r, initial, _added );
92 _current.swap( new_r );
93 }
94 return changed;
95 }
96
100 bool add( const value_type & val_r )
101 {
102 bool done = _current.insert( val_r ).second;
103 if ( done )
104 {
105 if ( ! _removed.erase( val_r ) )
106 _added.insert( val_r );
107 }
108 return done;
109 }
110
114 bool remove( const value_type & val_r )
115 {
116 bool done = _current.erase( val_r );
117 if ( done )
118 {
119 if ( ! _added.erase( val_r ) )
120 _removed.insert( val_r );
121 }
122 return done;
123 }
125
127
128
129 bool contains( const key_type & key_r ) const { return find( _current, key_r ); }
130
132 bool wasAdded( const key_type & key_r ) const { return find( _added, key_r ); }
133
135 bool wasRemoved( const key_type & key_r ) const { return find( _removed, key_r ); }
136
137
139 const set_type & current() const { return _current; }
140
142 const set_type & added() const { return _added; }
143
145 const set_type & removed() const { return _removed; }
147
149
150
151 set_type & current() { return _current; }
152
154 set_type & added() { return _added; }
155
157 set_type & removed() { return _removed; }
159
160 private:
161
162 static bool find( const set_type & set_r, const key_type & key_r )
163 { return set_r.find( key_r ) != set_r.end(); }
164
165 template <class TORDERED_SET, typename enable_if = typename TORDERED_SET::key_compare>
166 static void setDifference( const TORDERED_SET & lhs, const TORDERED_SET & rhs, TORDERED_SET & result_r )
167 {
168 // std::set_difference requires ordered sets!
169 std::set_difference( lhs.begin(), lhs.end(), rhs.begin(), rhs.end(),
170 std::inserter( result_r, result_r.end() ),
171 typename TORDERED_SET::key_compare() );
172 }
173
174 template <class TUNORDERED_SET, typename enable_if = typename TUNORDERED_SET::hasher, typename = void>
175 static void setDifference( const TUNORDERED_SET & lhs, const TUNORDERED_SET & rhs, TUNORDERED_SET & result_r )
176 {
177 // std::set_difference requires ordered sets!
178 for ( const auto & l : lhs )
179 { if ( rhs.find( l ) == rhs.end() ) result_r.insert( l ); }
180 }
181
182 private:
186 };
187
189 template <class TSet>
190 std::ostream & operator<<( std::ostream & str, const SetTracker<TSet> & obj )
191 { return str << "set(" << obj.current().size() << "|+" << obj.added().size() << "|-" << obj.removed().size() << ')'; }
192
193 } // namespace base
195} // namespace zypp
197#endif // ZYPP_BASE_SETTRACKER_H
Definition: Arch.h:352
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
Track added/removed set items based on an initial set.
Definition: SetTracker.h:38
std::ostream & operator<<(std::ostream &str, const SetTracker< TSet > &obj)
Stream output.
Definition: SetTracker.h:190
bool wasRemoved(const key_type &key_r) const
Whether val_r is tracked as removed.
Definition: SetTracker.h:135
bool setInitial(set_type new_r)
Start tracking a new set (discards previously tracked changes).
Definition: SetTracker.h:63
TSet::key_type key_type
Definition: SetTracker.h:40
TSet::value_type value_type
Definition: SetTracker.h:41
const set_type & current() const
Return the current set.
Definition: SetTracker.h:139
set_type & current()
Return the current set.
Definition: SetTracker.h:151
static void setDifference(const TORDERED_SET &lhs, const TORDERED_SET &rhs, TORDERED_SET &result_r)
Definition: SetTracker.h:166
static bool find(const set_type &set_r, const key_type &key_r)
Definition: SetTracker.h:162
SetTracker(set_type initial_r)
Ctor taking an initial set.
Definition: SetTracker.h:48
bool set(set_type new_r)
Set a new_r set and track changes.
Definition: SetTracker.h:78
bool add(const value_type &val_r)
Add an element to the set and track changes.
Definition: SetTracker.h:100
bool wasAdded(const key_type &key_r) const
Whether val_r is tracked as added.
Definition: SetTracker.h:132
const set_type & added() const
Return the set of added items.
Definition: SetTracker.h:142
set_type & added()
Return the set of added items.
Definition: SetTracker.h:154
set_type & removed()
Return the set of removed items.
Definition: SetTracker.h:157
bool remove(const value_type &val_r)
Remove an element from the set and track changes.
Definition: SetTracker.h:114
static void setDifference(const TUNORDERED_SET &lhs, const TUNORDERED_SET &rhs, TUNORDERED_SET &result_r)
Definition: SetTracker.h:175
SetTracker()
Default Ctor: empty set.
Definition: SetTracker.h:44
const set_type & removed() const
Return the set of removed items.
Definition: SetTracker.h:145
bool setInitial()
(Re-)Start tracking the current set (discards previously tracked changes).
Definition: SetTracker.h:57
bool contains(const key_type &key_r) const
Whether val_r is in the set.
Definition: SetTracker.h:129