ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Bag.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------- File Bag.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
32/*--------------------------------------------------------------------------*/
33/*----------------------------- DEFINITIONS --------------------------------*/
34/*--------------------------------------------------------------------------*/
35
36#ifndef __ADS_Bag
37 #define __ADS_Bag
38/* self-identification: #endif constant */
39
40/*--------------------------------------------------------------------------*/
41/*------------------------------ INCLUDES ----------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#include "ADS/Contract.h"
46
47#include <cstddef>
48#include <functional>
49#include <utility>
50#include <vector>
51
52/*--------------------------------------------------------------------------*/
53/*--------------------------- NAMESPACE ------------------------------------*/
54/*--------------------------------------------------------------------------*/
55
56namespace ADS
57{
58
59/*--------------------------------------------------------------------------*/
60/*------------------------------- CLASS Bag --------------------------------*/
61/*--------------------------------------------------------------------------*/
62
65template< class T , class H = DefaultHash< T > ,
66 class Eq = std::equal_to< T > >
67class Bag
68{
69
70/*--------------------------------------------------------------------------*/
71 public:
72 using Item = T;
73
75 explicit Bag( H h = H() , Eq eq = Eq() ) : f_T( 0 , h , eq ) { }
76
78 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
79
81 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
82
84 [[nodiscard]] std::size_t distinct( void ) const { return( f_T.size() ); }
85
101 bool insert( const T & x );
102
119 bool remove( const T & x );
120
123 [[nodiscard]] const T * find( const T & x ) const;
124
126 [[nodiscard]] std::vector< T > findAll( const T & x ) const;
127
129 [[nodiscard]] std::size_t count( const T & x ) const;
130
131/*--------------------------------------------------------------------------*/
132 private:
134 [[nodiscard]] bool counted( void ) const;
135
137 std::size_t f_n = 0;
138
139 }; // end( class Bag )
140
141/*--------------------------------------------------------------------------*/
142
143} // end( namespace ADS )
144
145/*--------------------------------------------------------------------------*/
146/*-------------------------- IMPLEMENTATION --------------------------------*/
147/*--------------------------------------------------------------------------*/
148
149#include "ADS/set/impl/Bag.tpp"
150
151/*--------------------------------------------------------------------------*/
152
153#endif /* Bag.h included */
154
155/*--------------------------------------------------------------------------*/
156/*----------------------------- End File Bag.h -----------------------------*/
157/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A hash table with open addressing and control bytes, beyond the book: the engineered DICTIONARY of AD...
bool empty(void) const
true if there is no item
Definition Bag.h:81
std::size_t count(const T &x) const
the number of items equal to
T Item
the type of the items
Definition Bag.h:72
std::size_t distinct(void) const
the number of groups of equal items
Definition Bag.h:84
std::size_t size(void) const
the number of items, the equal ones counted each
Definition Bag.h:78
const T * find(const T &x) const
an item equal to , the first one added, nil if none, expected
Bag(H h=H(), Eq eq=Eq())
the empty bag
Definition Bag.h:75
bool insert(const T &x)
adds , expected amortized; always true
bool remove(const T &x)
removes the last item added equal to , expected amortized; true if there was one
std::vector< T > findAll(const T &x) const
all the items equal to , in the order of their insertions
a hash table with open addressing and control bytes in groups of 15
Definition FlatHashTable.h:252
Definition ADT.h:96