Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / sat / Map.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/Map.h
10  */
11 #ifndef ZYPP_SAT_MAP_H
12 #define ZYPP_SAT_MAP_H
13
14 extern "C"
15 {
16   struct _Map;
17 }
18 #include <iosfwd>
19 #include <string>
20
21 #include "zypp/base/PtrTypes.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 {
26   ///////////////////////////////////////////////////////////////////
27   namespace sat
28   {
29     ///////////////////////////////////////////////////////////////////
30     /// \class Map
31     /// \brief Libsolv (bit)Map wrapper.
32     ///
33     /// \Note Requested sizes are filled up to the next multiple of eight.
34     /// Libsolv bitmaps are not shrinkable.
35     ///////////////////////////////////////////////////////////////////
36     class Map
37     {
38     public:
39       typedef unsigned long size_type;
40
41       /** Type to indicate the bitmap should match the current pools capacity. */
42       struct PoolSizeType {};
43       /** An object indicating the bitmap should match the current pools capacity. */
44       static constexpr PoolSizeType poolSize = PoolSizeType();
45
46     public:
47       /** Default ctor: empty Map */
48       Map();
49
50       /** Ctor taking the Map size */
51       explicit Map( size_type size_r );
52
53       /** Ctor creating a Map matching the current pools capacity */
54       Map( PoolSizeType );
55
56       /** Dtor */
57       ~Map();
58
59     public:
60       /** Whether Map is empty. */
61       bool empty() const;
62
63       /** Size of the Map. */
64       size_type size() const;
65
66       /** Grow the Map if necessary. */
67       void grow( size_type size_r );
68
69     public:
70       /** Set all bits. */
71       void setAll();
72
73       /** Clear all bits. */
74       void clearAll();
75
76       /** Assign \c val_r to all bits. */
77       void assignAll( bool val_r );
78
79       /** Set bit \c idx_r.
80        * \throws std::out_of_range if \a idx_r is out of range
81        */
82       void set( size_type idx_r );
83
84       /** Clear bit \c idx_r.
85        * \throws std::out_of_range if \a idx_r is out of range
86        */
87       void clear( size_type idx_r );
88
89       /** Assign \c val_r to bit \c idx_r.
90        * \throws std::out_of_range if \a idx_r is out of range
91        */
92       void assign( size_type idx_r, bool val_r );
93
94     public:
95       /** Test bit \c idx_r.
96        * \throws std::out_of_range if \a idx_r is out of range
97        */
98       bool test( size_type idx_r ) const;
99
100       /** Test bit \c idx_r.
101        * \throws std::out_of_range if \a idx_r is out of range
102        */
103       bool operator[]( size_type idx_r ) const
104       { return test( idx_r ); }
105
106     public:
107       /** String representation */
108       std::string asString( const char on_r = '1', const char off_r = '0' ) const;
109
110     public:
111       operator struct ::_Map *();               ///< libsolv backdoor
112       operator const struct ::_Map *() const    ///< libsolv backdoor
113       { return _pimpl.get(); }
114     private:
115       RWCOW_pointer<struct ::_Map> _pimpl;      ///< Pointer to implementation
116     };
117
118     /** \relates Map Stream output */
119     inline std::ostream & operator<<( std::ostream & str, const Map & obj )
120     { return str << obj.asString(); }
121
122     /** \relates Map */
123     bool operator==( const Map & lhs, const Map & rhs );
124
125     /** \relates Map */
126     inline bool operator!=( const Map & lhs, const Map & rhs )
127     { return !( lhs == rhs ); }
128
129   } // namespace sat
130   ///////////////////////////////////////////////////////////////////
131
132   /** \relates Map Clone function for RWCOW_pointer */
133   template<> struct ::_Map * rwcowClone<struct ::_Map>( const struct ::_Map * rhs );
134
135   typedef sat::Map Bitmap;
136
137 } // namespace zypp
138 ///////////////////////////////////////////////////////////////////
139 #endif // ZYPP_SAT_MAP_H