ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SetInterfaces.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File SetInterfaces.h --------------------------*/
3/*--------------------------------------------------------------------------*/
53/*--------------------------------------------------------------------------*/
54/*----------------------------- DEFINITIONS --------------------------------*/
55/*--------------------------------------------------------------------------*/
56
57#ifndef __ADS_SetInterfaces
58 #define __ADS_SetInterfaces
59/* self-identification: #endif constant */
60
61/*--------------------------------------------------------------------------*/
62/*------------------------------ INCLUDES ----------------------------------*/
63/*--------------------------------------------------------------------------*/
64
65#include "ADS/Contract.h"
66#include "ADS/Portable.h"
67
68#include <algorithm>
69#include <concepts>
70#include <cstddef>
71#include <functional>
72#include <utility>
73#include <vector>
74
75/*--------------------------------------------------------------------------*/
76/*--------------------------- NAMESPACE ------------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79namespace ADS
80{
81
82/*--------------------------------------------------------------------------*/
83/*-------------------------------- CONCEPTS --------------------------------*/
84/*--------------------------------------------------------------------------*/
85
88template< class S >
89concept USet = requires( S s , const S cs , const typename S::Item & x )
90{
91 typename S::Item;
92 { cs.size() } -> std::convertible_to< std::size_t >;
93 { s.insert( x ) } -> std::same_as< bool >;
94 { s.remove( x ) } -> std::same_as< bool >;
95 { cs.find( x ) } -> std::convertible_to< const typename S::Item * >;
96 };
97
99template< class S >
100concept SSet =
101 USet< S > && requires( const S cs , const typename S::Item & x )
102{
103 { cs.lowerBound( x ) } -> std::convertible_to< const typename S::Item * >;
104 };
105
109template< class D >
111 requires( D d , const typename D::Key & k , const typename D::Value & v )
112{
113 typename D::Key;
114 typename D::Value;
115 { d.findKey( k ) } -> std::convertible_to< const typename D::Key * >;
116 d.insert( k , v );
117 d.remove( k );
118 };
119
122template< class D >
124 KeyedDictionary< D > && requires( D d , const typename D::Key & k )
125{
126 { d.lowerBoundKey( k ) } -> std::convertible_to< const typename D::Key * >;
127 };
128
129/*--------------------------------------------------------------------------*/
130/*---------------------------- CLASS ArrayUSet -----------------------------*/
131/*--------------------------------------------------------------------------*/
132
134template< class T , class Eq = std::equal_to<> >
136{
137
138/*--------------------------------------------------------------------------*/
139 public:
140 using Item = T;
141
143 explicit ArrayUSet( Eq eq = Eq() ) : f_eq( std::move( eq ) ) { }
144
146 [[nodiscard]] std::size_t size( void ) const { return( f_A.size() ); }
147
149 [[nodiscard]] const T * find( const T & x ) const;
150
154 bool insert( const T & x );
155
172 bool remove( const T & x );
173
175 [[nodiscard]] const std::vector< T > & items( void ) const { return( f_A ); }
176
177/*--------------------------------------------------------------------------*/
178 private:
179 std::vector< T > f_A;
180 ADS_NO_UNIQUE_ADDRESS Eq f_eq;
181
182 }; // end( class ArrayUSet )
183
184/*--------------------------------------------------------------------------*/
185/*---------------------------- CLASS ArraySSet -----------------------------*/
186/*--------------------------------------------------------------------------*/
187
189template< class T , class C = std::less<> >
191{
192
193/*--------------------------------------------------------------------------*/
194 public:
195 using Item = T;
196
198 explicit ArraySSet( C less = C() ) : f_less( std::move( less ) ) { }
199
201 [[nodiscard]] std::size_t size( void ) const { return( f_A.size() ); }
202
204 [[nodiscard]] const T * find( const T & x ) const;
205
208 [[nodiscard]] const T * lowerBound( const T & x ) const;
209
228 bool insert( const T & x );
229
233 bool remove( const T & x );
234
236 [[nodiscard]] const std::vector< T > & items( void ) const { return( f_A ); }
237
238/*--------------------------------------------------------------------------*/
239 private:
241 [[nodiscard]] bool sorted( void ) const;
242
243 std::vector< T > f_A;
244 ADS_NO_UNIQUE_ADDRESS C f_less;
245
246 }; // end( class ArraySSet )
247
248/*--------------------------------------------------------------------------*/
249
250} // end( namespace ADS )
251
252/*--------------------------------------------------------------------------*/
253/*-------------------------- IMPLEMENTATION --------------------------------*/
254/*--------------------------------------------------------------------------*/
255
256#include "ADS/set/impl/SetInterfaces.tpp"
257
258/*--------------------------------------------------------------------------*/
259
260#endif /* SetInterfaces.h included */
261
262/*--------------------------------------------------------------------------*/
263/*------------------------ End File SetInterfaces.h ------------------------*/
264/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
bool insert(const T &x)
added in its place if no item equal to it is there, the items after it moved by one place,...
T Item
the type of the items
Definition SetInterfaces.h:195
bool remove(const T &x)
the item equal to removed, ; true if there was one
const std::vector< T > & items(void) const
the items, increasing
Definition SetInterfaces.h:236
std::size_t size(void) const
the number of items
Definition SetInterfaces.h:201
const T * lowerBound(const T &x) const
the least item not smaller than , nil if none, by binary search in (Morin's find( x ) of an SSet)
const T * find(const T &x) const
the item equal to , nil if none,
ArraySSet(C less=C())
the empty set
Definition SetInterfaces.h:198
bool insert(const T &x)
added if no item equal to it is there, ; true if added
const T * find(const T &x) const
the item equal to , nil if none,
ArrayUSet(Eq eq=Eq())
the empty set
Definition SetInterfaces.h:143
bool remove(const T &x)
the item equal to removed, the last one moved into its place, ; true if there was one
T Item
the type of the items
Definition SetInterfaces.h:140
const std::vector< T > & items(void) const
the items, in no order
Definition SetInterfaces.h:175
std::size_t size(void) const
the number of items
Definition SetInterfaces.h:146
a dictionary that gives the address of the key equal to the one asked, on which DictionarySet is a US...
Definition SetInterfaces.h:110
a KeyedDictionary that gives the address of the smallest key not smaller than the one asked,...
Definition SetInterfaces.h:123
the sorted set of Morin, a USet with the successor search
Definition SetInterfaces.h:100
the unordered set of Morin, whose find() gives the item equal to the one asked
Definition SetInterfaces.h:89
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
Definition ADT.h:96