ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PrefixSearch.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File PrefixSearch.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
61/*--------------------------------------------------------------------------*/
62/*----------------------------- DEFINITIONS --------------------------------*/
63/*--------------------------------------------------------------------------*/
64
65#ifndef __ADS_PrefixSearch
66 #define __ADS_PrefixSearch
67/* self-identification: #endif constant */
68
69/*--------------------------------------------------------------------------*/
70/*------------------------------ INCLUDES ----------------------------------*/
71/*--------------------------------------------------------------------------*/
72
73#include "ADS/Contract.h"
75
76#include <algorithm>
77#include <cstddef>
78#include <cstdint>
79#include <optional>
80#include <string>
81#include <string_view>
82#include <utility>
83#include <vector>
84
85/*--------------------------------------------------------------------------*/
86/*--------------------------- NAMESPACE ------------------------------------*/
87/*--------------------------------------------------------------------------*/
88
89namespace ADS
90{
91
111[[nodiscard]] inline std::size_t
112lexicographicPosition( const std::vector< std::string > & A ,
113 std::string_view Q );
114
136[[nodiscard]] inline std::size_t
137prefixEnd( const std::vector< std::string > & A , std::string_view P );
138
152[[nodiscard]] inline std::pair< std::size_t , std::size_t >
153prefixRange( const std::vector< std::string > & A , std::string_view P );
154
157[[nodiscard]] inline std::size_t commonPrefixLength( std::string_view a ,
158 std::string_view b );
159
160namespace detail
161{
162
164[[nodiscard]] inline bool precedes( std::string_view s , std::string_view P ,
165 bool upper );
166
169[[nodiscard]] inline std::uint64_t prefixKey( std::string_view s );
170
171} // end( namespace detail )
172
173/*--------------------------------------------------------------------------*/
174/*-------------------------- CLASS BinnedSearch ----------------------------*/
175/*--------------------------------------------------------------------------*/
176
179{
180 public:
201 explicit BinnedSearch( std::vector< std::uint64_t > X );
202
221 [[nodiscard]] std::size_t position( std::uint64_t y ) const;
222
225 [[nodiscard]] bool contains( std::uint64_t y ) const;
226
228 [[nodiscard]] std::size_t bins( void ) const;
229
231 [[nodiscard]] std::uint64_t width( void ) const;
232
236 [[nodiscard]] std::pair< std::size_t , std::size_t >
237 bin( std::size_t j ) const;
238
241 [[nodiscard]] std::optional< std::size_t > binOf( std::uint64_t y ) const;
242
244 [[nodiscard]] std::size_t largestBin( void ) const;
245
247 [[nodiscard]] std::size_t size( void ) const;
248
250 [[nodiscard]] const std::vector< std::uint64_t > & items( void ) const;
251
252 private:
253 std::vector< std::uint64_t > f_X;
254 std::uint64_t f_b = 1;
255 std::vector< std::size_t > f_start;
256 };
257
258/*--------------------------------------------------------------------------*/
259/*----------------------- CLASS BinnedStringSearch -------------------------*/
260/*--------------------------------------------------------------------------*/
261
264{
265 public:
269 explicit BinnedStringSearch( std::vector< std::string > A );
270
291 [[nodiscard]] std::size_t lexicographicPosition( std::string_view Q ) const;
292
314 [[nodiscard]] std::size_t prefixEnd( std::string_view P ) const;
315
318 [[nodiscard]] std::pair< std::size_t , std::size_t >
319 prefixRange( std::string_view P ) const;
320
322 [[nodiscard]] const BinnedSearch & keys( void ) const;
323
324 private:
327 [[nodiscard]] std::size_t rank( std::string_view P , bool upper ,
328 std::uint64_t k ) const;
329
330 std::vector< std::string > f_A;
331 BinnedSearch f_K;
332 };
333
334/*--------------------------------------------------------------------------*/
335
336} // end( namespace ADS )
337
338/*--------------------------------------------------------------------------*/
339/*-------------------------- IMPLEMENTATION --------------------------------*/
340/*--------------------------------------------------------------------------*/
341
342#include "ADS/string/impl/PrefixSearch.tpp"
343
344/*--------------------------------------------------------------------------*/
345
346#endif /* PrefixSearch.h included */
347
348/*--------------------------------------------------------------------------*/
349/*------------------------- End File PrefixSearch.h ------------------------*/
350/*--------------------------------------------------------------------------*/
The binary search of section 1.2 in a sorted vector : is compared with the middle item of the part ...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
the search by interpolation of a sorted array of integers by bins
Definition PrefixSearch.h:179
bool contains(std::uint64_t y) const
whether is an item, i.e., at the place that position( y ) gives
const std::vector< std::uint64_t > & items(void) const
the sorted array
std::size_t size(void) const
the number of items
std::uint64_t width(void) const
the width of a bin
std::pair< std::size_t, std::size_t > bin(std::size_t j) const
the places of in the bin
std::size_t largestBin(void) const
the number of items of the largest bin
BinnedSearch(std::vector< std::uint64_t > X)
the bins of , in
std::optional< std::size_t > binOf(std::uint64_t y) const
the bin of , or none if is out of
std::size_t bins(void) const
the number of bins
std::size_t position(std::uint64_t y) const
the number of items smaller than
std::size_t lexicographicPosition(std::string_view Q) const
the number of strings smaller than
std::pair< std::size_t, std::size_t > prefixRange(std::string_view P) const
the range of the strings prefixed by , by lexicographicPosition() and prefixEnd()
std::size_t prefixEnd(std::string_view P) const
the number of strings smaller than
const BinnedSearch & keys(void) const
the BinnedSearch of the integers of the strings
BinnedStringSearch(std::vector< std::string > A)
the bins of the integers of the first 8 characters of , a BinnedSearch on them
std::size_t commonPrefixLength(std::string_view a, std::string_view b)
the length of the longest common prefix of and , by comparing them from their start
std::size_t prefixEnd(const std::vector< std::string > &A, std::string_view P)
the number of strings of the sorted smaller than , i.e., smaller than or prefixed by
std::size_t lexicographicPosition(const std::vector< std::string > &A, std::string_view Q)
the number of strings of the sorted smaller than
std::pair< std::size_t, std::size_t > prefixRange(const std::vector< std::string > &A, std::string_view P)
the range of the strings of the sorted prefixed by
Definition ADT.h:96