Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / sat / Map.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/Map.cc
10  */
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
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28
29   template<>
30   struct ::_Map * rwcowClone<struct ::_Map>( const struct ::_Map * rhs )
31   {
32     struct ::_Map * ret = new ::_Map;
33     ::map_init_clone( ret, const_cast<struct ::_Map *>(rhs) );
34     return ret;
35   }
36
37   ///////////////////////////////////////////////////////////////////
38   namespace sat
39   { /////////////////////////////////////////////////////////////////
40
41     Map::Map()
42       : _pimpl( new ::_Map )
43     { ::map_init( _pimpl.get(), 0 ); }
44
45     Map::Map( size_type size_r )
46       : _pimpl( new ::_Map )
47     { ::map_init( _pimpl.get(), size_r ); }
48
49     Map::Map( PoolSizeType )
50       : _pimpl( new ::_Map )
51     { ::map_init( _pimpl.get(), Pool::instance().capacity() ); }
52
53     Map::~Map()
54     { ::map_free( _pimpl.get() ); }
55
56     bool Map::empty() const
57     { return( _pimpl->size == 0 ); }
58
59     Map::size_type Map::size() const
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
68     void Map::clearAll()
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 struct ::_Map *()     // COW: nonconst version can't be inlined
121     { return _pimpl.get(); }            // without exposing struct ::_Map
122
123     bool operator==( const Map & lhs, const Map & rhs )
124     {
125       const struct ::_Map * l = lhs;
126       const struct ::_Map * r = rhs;
127       return( l == r || ( l->size == r->size && ::memcmp( l->map, r->map, l->size ) == 0 ) );
128     }
129
130     /////////////////////////////////////////////////////////////////
131   } // namespace sat
132   ///////////////////////////////////////////////////////////////////
133   /////////////////////////////////////////////////////////////////
134 } // namespace zypp
135 ///////////////////////////////////////////////////////////////////