libzypp  13.10.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 
22 using std::endl;
23 
25 namespace zypp
26 {
27 
28  template<>
29  struct ::_Map * rwcowClone<struct ::_Map>( const struct ::_Map * rhs )
30  {
31  struct ::_Map * ret = new ::_Map;
32  ::map_init_clone( ret, const_cast<struct ::_Map *>(rhs) );
33  return ret;
34  }
35 
37  namespace sat
38  {
39 
41  : _pimpl( new ::_Map )
42  { ::map_init( _pimpl.get(), 0 ); }
43 
44  Map::Map( size_type size_r )
45  : _pimpl( new ::_Map )
46  { ::map_init( _pimpl.get(), size_r ); }
47 
49  { ::map_free( _pimpl.get() ); }
50 
51  bool Map::empty() const
52  { return( _pimpl->size == 0 ); }
53 
55  { return _pimpl->size << 3; }
56 
57  void Map::grow( size_type size_r )
58  { ::map_grow( _pimpl.get(), size_r ); }
59 
60  void Map::setAll()
61  { assignAll( true ); }
62 
64  { assignAll( false ); }
65 
66  void Map::assignAll( bool val_r )
67  {
68  if ( _pimpl->size )
69  ::memset( _pimpl->map, (val_r?-1:0), _pimpl->size );
70  }
71 
72 #define M_RANGE_CKECK(IDX,LOC) if ( ((IDX) >> 3) >= size_type(_pimpl->size) ) throw std::out_of_range( "zypp::sat::Map::" LOC )
73 
74  void Map::set( size_type idx_r )
75  {
76  M_RANGE_CKECK( idx_r, "set" );
77  MAPSET( _pimpl, idx_r );
78  }
79 
80  void Map::clear( size_type idx_r )
81  {
82  M_RANGE_CKECK( idx_r, "clear" );
83  MAPCLR( _pimpl, idx_r );
84  }
85 
86  void Map::assign( size_type idx_r, bool val_r )
87  {
88  M_RANGE_CKECK( idx_r, "assign" );
89  if ( val_r )
90  { MAPSET( _pimpl, idx_r ); }
91  else
92  { MAPCLR( _pimpl, idx_r ); }
93  }
94 
95  bool Map::test( size_type idx_r ) const
96  {
97  M_RANGE_CKECK( idx_r, "test" );
98  return MAPTST( _pimpl, idx_r );
99  }
100 
101  std::string Map::asString( const char on_r, const char off_r ) const
102  {
103  if ( empty() )
104  return std::string();
105 
106  std::string ret( size(), off_r );
107  for_( idx, size_type(0), size() )
108  {
109  if ( test( idx ) )
110  ret[idx] = on_r;
111  }
112  return ret;
113  }
114 
115  Map::operator struct ::_Map *() // COW: nonconst version can't be inlined
116  { return _pimpl.get(); } // without exposing struct ::_Map
117 
118  bool operator==( const Map & lhs, const Map & rhs )
119  {
120  const struct ::_Map * l = lhs;
121  const struct ::_Map * r = rhs;
122  return( l == r || ( l->size == r->size && ::memcmp( l->map, r->map, l->size ) == 0 ) );
123  }
124 
126  } // namespace sat
129 } // namespace zypp
RWCOW_pointer< struct::_Map > _pimpl
Pointer to implementation.
Definition: Map.h:97
void clear(size_type idx_r)
Clear bit idx_r.
Definition: Map.cc:80
size_type size() const
Size of the Map.
Definition: Map.cc:54
bool operator==(const Map &lhs, const Map &rhs)
Definition: Map.cc:118
#define M_RANGE_CKECK(IDX, LOC)
Definition: Map.cc:72
void set(size_type idx_r)
Set bit idx_r.
Definition: Map.cc:74
#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:95
Map()
Default ctor: empty Map.
Definition: Map.cc:40
const _D * get() const
Definition: PtrTypes.h:483
void clearAll()
Clear all bits.
Definition: Map.cc:63
~Map()
Dtor.
Definition: Map.cc:48
void setAll()
Set all bits.
Definition: Map.cc:60
unsigned long size_type
Definition: Map.h:39
void assign(size_type idx_r, bool val_r)
Assign val_r to bit idx_r.
Definition: Map.cc:86
std::string asString(const char on_r= '1', const char off_r= '0') const
String representation.
Definition: Map.cc:101
void grow(size_type size_r)
Grow the Map if necessary.
Definition: Map.cc:57
bool empty() const
Whether Map is empty.
Definition: Map.cc:51
Libsolv (bit)Map wrapper.
Definition: Map.h:36
void assignAll(bool val_r)
Assign val_r to all bits.
Definition: Map.cc:66