Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Set.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
8#include "Stroika/Foundation/Containers/Set.h"
10
11// Not generally included, but you can include these if you want to select a particular backend implementation
13#include "Stroika/Foundation/Containers/Concrete/Set_stdset.h"
14#include "Stroika/Foundation/Containers/SortedSet.h"
15
17
18#include "Set.h"
19
20using namespace std;
21
22using namespace Stroika::Foundation;
25
26namespace {
27 void SimplestSetTest_ ()
28 {
29 /*
30 * A Set<T> is an un-ordered container where each item of type T is present at most one time.
31 * Think of it like std::set<T>, except that the Stroika set may be implemented with a variety of
32 * different data structures, and there is no need to define operator< (or less<) comparison function
33 * (except for certain backend data structure representations).
34 */
35 {
36 Set<int> s;
37 s.Add (3);
38 s += 3;
39 for ([[maybe_unused]] int i : s) {
40 Assert (i == 3);
41 }
42 Assert (s.size () == 1);
43 }
44 {
45 vector<int> c{3, 4, 5};
46 Set<int> s9{1, 2, 3};
47 // Construct a set with an arbitrary comparison routine (in this case comparing mod 11)
48 Set<int> s10{Common::DeclareEqualsComparer ([] (int l, int r) { return (l % 11) == (r % 11); }), c};
49 Assert (s10.Contains (3) and s10.Contains (3 + 11) and not s10.Contains (6));
50 DbgTrace ("s10={}"_f, s10);
51 }
52 {
53 Set<int> s{1, 2, 3}; // use the default Set<> representation - the best for type 'int'
54 s = Concrete::Set_LinkedList<int>{s}; // Force using a linked list to represent the set
55 // other set operations work the same, either way
56 if (s.Contains (5)) {
57 Assert (false);
58 }
59 }
60 }
61 void SetWithExplicitComparer_ ()
62 {
63 {
65 SortedSet<String> tmp{String::LessComparer{Characters::CompareOptions::eCaseInsensitive}, {"a", "b", "A"}};
66 Assert (tmp.size () == 2);
67 Assert (tmp.Contains ("A"));
68 Assert (tmp.Contains ("B"));
69 }
70 }
71}
72
73void Samples::Containers::Set::RunDemo ()
74{
75 SimplestSetTest_ ();
76}
#define DbgTrace
Definition Trace.h:309
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Set_LinkedList<T> is an LinkedList-based concrete implementation of the Set<T> container pattern.
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
nonvirtual void Add(ArgByValueType< value_type > item)
Definition Set.inl:138
nonvirtual size_t size() const
Returns the number of items contained.
Definition Iterable.inl:302
Create a format-string (see std::wformat_string or Stroika FormatString, or python 'f' strings.
constexpr Common::ComparisonRelationDeclaration< ComparisonRelationType::eEquals, remove_cvref_t< FUNCTOR > > DeclareEqualsComparer(FUNCTOR &&f)
DeclareEqualsComparer () marks a FUNCTOR (lambda or not) as being a FUNCTOR which compares for equali...
Definition Compare.inl:31
STL namespace.
very similar to ThreeWayComparer but returns true if less
Definition String.h:1865