libzypp: zypp/sat/Map.cc Source File

libzypp 17.38.14

Map.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
11extern "C"
12{
13#include <solv/bitmap.h>
14}
15#include <iostream>
16#include <exception>
17#include <zypp-core/base/LogTools.h>
18#include <zypp-core/base/String.h>
19
20#include <zypp/sat/Map.h>
21#include <zypp/sat/Pool.h>
22
23using std::endl;
24
26namespace zypp
27{
28
29 template<>
30 sat::detail::CMap * rwcowClone<sat::detail::CMap>( const sat::detail::CMap * rhs )
31 {
32 sat::detail::CMap * ret = new sat::detail::CMap;
33 ::map_init_clone( ret, const_cast<sat::detail::CMap *>(rhs) );
34 return ret;
35 }
36
38 namespace sat
39 {
40
41 constexpr Map::PoolSizeType Map::poolSize;
42
43 Map::Map()
44 : _pimpl( new detail::CMap )
45 { ::map_init( _pimpl.get(), 0 ); }
46
47 Map::Map( size_type size_r )
48 : _pimpl( new detail::CMap )
49 { ::map_init( _pimpl.get(), size_r ); }
50
51 Map::Map( PoolSizeType )
52 : _pimpl( new detail::CMap )
53 { ::map_init( _pimpl.get(), Pool::instance().capacity() ); }
54
55 Map::~Map()
56 { ::map_free( _pimpl.get() ); }
57
58 bool Map::empty() const
59 { return( _pimpl->size == 0 ); }
60
61 Map::size_type Map::size() const
62 { return _pimpl->size << 3; }
63
64 void Map::grow( size_type size_r )
65 { ::map_grow( _pimpl.get(), size_r ); }
66
67 void Map::setAll()
68 { assignAll( true ); }
69
70 void Map::clearAll()
71 { assignAll( false ); }
72
73 void Map::assignAll( bool val_r )
74 {
75 if ( _pimpl->size )
76 ::memset( _pimpl->map, (val_r?-1:0), _pimpl->size );
77 }
78
79#define M_RANGE_CKECK(IDX,LOC) if ( ((IDX) >> 3) >= size_type(_pimpl->size) ) throw std::out_of_range( "zypp::sat::Map::" LOC )
80
81 void Map::set( size_type idx_r )
82 {
83 M_RANGE_CKECK( idx_r, "set" );
84 MAPSET( _pimpl, idx_r );
85 }
86
87 void Map::clear( size_type idx_r )
88 {
89 M_RANGE_CKECK( idx_r, "clear" );
90 MAPCLR( _pimpl, idx_r );
91 }
92
93 void Map::assign( size_type idx_r, bool val_r )
94 {
95 M_RANGE_CKECK( idx_r, "assign" );
96 if ( val_r )
97 { MAPSET( _pimpl, idx_r ); }
98 else
99 { MAPCLR( _pimpl, idx_r ); }
100 }
101
102 bool Map::test( size_type idx_r ) const
103 {
104 M_RANGE_CKECK( idx_r, "test" );
105 return MAPTST( _pimpl, idx_r );
106 }
107
108 std::string Map::asString( const char on_r, const char off_r ) const
109 {
110 if ( empty() )
111 return std::string();
112
113 std::string ret( size(), off_r );
114 for_( idx, size_type(0), size() )
115 {
116 if ( test( idx ) )
117 ret[idx] = on_r;
118 }
119 return ret;
120 }
121
122 Map::operator detail::CMap *() // COW: nonconst version can't be inlined
123 { return _pimpl.get(); } // without exposing detail::CMap
124
125 bool operator==( const Map & lhs, const Map & rhs )
126 {
127 const detail::CMap * l = lhs;
128 const detail::CMap * r = rhs;
129 return( l == r || ( l->size == r->size && ::memcmp( l->map, r->map, l->size ) == 0 ) );
130 }
131
133 } // namespace sat
136} // namespace zypp
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
LogTools.h
M_RANGE_CKECK
#define M_RANGE_CKECK(IDX, LOC)
Definition Map.cc:79
Map.h
Pool.h
String.h
zypp::sat::Map
Libsolv (bit)Map wrapper.
Definition Map.h:34
zypp::sat::Map::asString
std::string asString(const char on_r='1', const char off_r='0') const
String representation.
Definition Map.cc:108
zypp::sat::Map::clear
void clear(size_type idx_r)
Clear bit idx_r.
Definition Map.cc:87
zypp::sat::Map::size
size_type size() const
Size of the Map.
Definition Map.cc:61
zypp::sat::Map::grow
void grow(size_type size_r)
Grow the Map if necessary.
Definition Map.cc:64
zypp::sat::Map::assign
void assign(size_type idx_r, bool val_r)
Assign val_r to bit idx_r.
Definition Map.cc:93
zypp::sat::Map::clearAll
void clearAll()
Clear all bits.
Definition Map.cc:70
zypp::sat::Map::poolSize
static constexpr PoolSizeType poolSize
An object indicating the bitmap should match the current pools capacity.
Definition Map.h:41
zypp::sat::Map::Map
Map()
Default ctor: empty Map.
Definition Map.cc:43
zypp::sat::Map::test
bool test(size_type idx_r) const
Test bit idx_r.
Definition Map.cc:102
zypp::sat::Map::set
void set(size_type idx_r)
Set bit idx_r.
Definition Map.cc:81
zypp::sat::Map::_pimpl
RWCOW_pointer< detail::CMap > _pimpl
Pointer to implementation.
Definition Map.h:112
zypp::sat::Map::assignAll
void assignAll(bool val_r)
Assign val_r to all bits.
Definition Map.cc:73
zypp::sat::Map::empty
bool empty() const
Whether Map is empty.
Definition Map.cc:58
zypp::sat::Map::~Map
~Map()
Dtor.
Definition Map.cc:55
zypp::sat::Map::size_type
unsigned long size_type
Definition Map.h:36
zypp::sat::Map::setAll
void setAll()
Set all bits.
Definition Map.cc:67
zypp::sat::Pool::instance
static Pool instance()
Singleton ctor.
Definition Pool.h:55
zypp::sat::detail
Definition PoolImpl.cc:73
zypp::sat::detail::CMap
::s_Map CMap
Wrapped libsolv C data type exposed as backdoor.
Definition PoolDefines.h:33
zypp::sat::operator==
bool operator==(const FileConflicts &lhs, const FileConflicts &rhs)
relates: FileConflicts
Definition FileConflicts.h:85
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::rwcowClone< sat::detail::CMap >
sat::detail::CMap * rwcowClone< sat::detail::CMap >(const sat::detail::CMap *rhs)
relates: Map Clone function for RWCOW_pointer
Definition Map.cc:30
zypp::RWCOW_pointer::get
const D * get() const
Definition PtrTypes.h:519
zypp::sat::Map::PoolSizeType
Type to indicate the bitmap should match the current pools capacity.
Definition Map.h:39