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