libzypp 17.31.23
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/base/LogTools.h>
18#include <zypp/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<>
31 {
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
44 : _pimpl( new detail::CMap )
45 { ::map_init( _pimpl.get(), 0 ); }
46
48 : _pimpl( new detail::CMap )
49 { ::map_init( _pimpl.get(), size_r ); }
50
52 : _pimpl( new detail::CMap )
53 { ::map_init( _pimpl.get(), Pool::instance().capacity() ); }
54
56 { ::map_free( _pimpl.get() ); }
57
58 bool Map::empty() const
59 { return( _pimpl->size == 0 ); }
60
62 { return _pimpl->size << 3; }
63
64 void Map::grow( size_type size_r )
65 { ::map_grow( _pimpl.get(), size_r ); }
66
68 { assignAll( true ); }
69
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
#define M_RANGE_CKECK(IDX, LOC)
Definition: Map.cc:79
Libsolv (bit)Map wrapper.
Definition: Map.h:34
std::string asString(const char on_r='1', const char off_r='0') const
String representation.
Definition: Map.cc:108
void clear(size_type idx_r)
Clear bit idx_r.
Definition: Map.cc:87
size_type size() const
Size of the Map.
Definition: Map.cc:61
void grow(size_type size_r)
Grow the Map if necessary.
Definition: Map.cc:64
void assign(size_type idx_r, bool val_r)
Assign val_r to bit idx_r.
Definition: Map.cc:93
unsigned long size_type
Definition: Map.h:36
void clearAll()
Clear all bits.
Definition: Map.cc:70
static constexpr PoolSizeType poolSize
An object indicating the bitmap should match the current pools capacity.
Definition: Map.h:41
Map()
Default ctor: empty Map.
Definition: Map.cc:43
bool test(size_type idx_r) const
Test bit idx_r.
Definition: Map.cc:102
void set(size_type idx_r)
Set bit idx_r.
Definition: Map.cc:81
RWCOW_pointer< detail::CMap > _pimpl
Pointer to implementation.
Definition: Map.h:112
void assignAll(bool val_r)
Assign val_r to all bits.
Definition: Map.cc:73
bool empty() const
Whether Map is empty.
Definition: Map.cc:58
~Map()
Dtor.
Definition: Map.cc:55
void setAll()
Set all bits.
Definition: Map.cc:67
size_type capacity() const
Internal array size for stats only.
Definition: Pool.cc:52
static Pool instance()
Singleton ctor.
Definition: Pool.h:55
::s_Map CMap
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolMember.h:60
bool operator==(const Map &lhs, const Map &rhs)
Definition: Map.cc:125
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
sat::detail::CMap * rwcowClone< sat::detail::CMap >(const sat::detail::CMap *rhs)
Definition: Map.cc:30
const D * get() const
Definition: PtrTypes.h:503
Type to indicate the bitmap should match the current pools capacity.
Definition: Map.h:39
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28