cppx-core
is_in.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 
3 #include <cppx-core/language/syntax/macro-use.hpp> // CPPX_USE_STD
4 #include <cppx-core/language/syntax/type-assemblers.hpp> // cppx::Raw_array_of_
6 #include <cppx-core/language/types/Truth.hpp> // cppx::Truth
7 
8 #include <algorithm> // std::(binary_search, find)
9 #include <bitset> // std::bitset
10 #include <initializer_list> // std::initializer_list
11 #include <iterator> // std::(begin, end)
12 #include <string_view> // std::string_view
13 
14 namespace cppx
15 {
17  basic_string_view, begin, binary_search, bitset, end, find, initializer_list
18  );
19 
20  template< class Char >
21  inline auto is_in( const basic_string_view<Char>& sv, const Char ch ) noexcept
22  -> Truth
23  { return 0 <= static_cast<Size>( sv.find( ch ) ); }
24 
25  template< class Item, class Value >
26  inline auto is_in( const initializer_list<Item>& items, const Value& v ) noexcept
27  -> Truth
28  {
29  for( auto& item : items )
30  {
31  if( item == v ) { return true; }
32  }
33  return false;
34  }
35 
36  template< size_t n >
37  inline auto is_in( const bitset<n>& bits, const int i ) noexcept
38  -> Truth
39  { return !!bits[i]; }
40 
41 
42  //-------------------------------------------------------- Arrays
43 
44  template< class It, class Arg >
45  auto is_in_span( const It begin, const It end, const Arg& v )
46  -> Truth
47  { return find( begin, end, v ) != end; }
48 
49  template< class Key, Size n, class Arg >
50  auto is_in( Raw_array_of_<n, const Key>& a, const Arg& v )
51  -> Truth
52  { return is_in_range( begin( a ), end( a ), v ); }
53 
54  template< class It, class Arg >
55  auto is_in_sorted_span( const It begin, const It end, const Arg& v )
56  -> Truth
57  { return binary_search( begin, end, v ); }
58 
59  template< class Key, Size n, class Arg >
60  auto is_in_sorted( Raw_array_of_<n, const Key>& a, const Arg& v )
61  -> Truth
62  { return is_in_sorted_range( begin( a ), end( a ), v ); }
63 
64 } // namespace cppx
A drop-in replacement for bool without implicit conversion from/to types other than bool.
Definition: Truth.hpp:34
auto is_in(const basic_string_view< Char > &sv, const Char ch) noexcept -> Truth
Definition: is_in.hpp:21
Truth is a drop-in replacement for bool without implicit conversion from/to types other than bool.
CPPX_USE_STD(basic_string, basic_string_view, bitset, char_traits, size)
Simple type builders Type_, P_, R_, Raw_array_ and Raw_array_of_.
auto is_in_sorted(Raw_array_of_< n, const Key > &a, const Arg &v) -> Truth
Definition: is_in.hpp:60
auto is_in_span(const It begin, const It end, const Arg &v) -> Truth
Definition: is_in.hpp:45
Item[n] Raw_array_of_
Creates a raw array type of a specified size.
Macros for generating more concise and clear using statements, primarily $use_cppx and $use_std,...
Signed Size and Index, plus unsigned equivalents Unsigned_size and Unsigned_index.
auto is_in_sorted_span(const It begin, const It end, const Arg &v) -> Truth
Definition: is_in.hpp:55