Sane C++ Libraries
C++ Platform Abstraction Libraries
VectorSet.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Containers/Vector.h"
5
6namespace SC
7{
8template <typename Value, typename Container>
9struct VectorSet;
10} // namespace SC
11
14
18template <typename Value, typename Container = SC::Vector<Value>>
20{
21 Container items;
22
24 auto size() const { return items.size(); }
25
26 [[nodiscard]] Value* begin() { return items.begin(); }
27 [[nodiscard]] const Value* begin() const { return items.begin(); }
28 [[nodiscard]] Value* end() { return items.end(); }
29 [[nodiscard]] const Value* end() const { return items.end(); }
30
32 template <typename ComparableToValue>
33 [[nodiscard]] bool contains(const ComparableToValue& value)
34 {
35 return items.contains(value);
36 }
37
39 [[nodiscard]] bool insert(const Value& value)
40 {
41 if (items.contains(value))
42 {
43 return true;
44 }
45 return items.push_back(value);
46 }
47
49 template <typename ComparableToValue>
50 [[nodiscard]] bool remove(const ComparableToValue& value)
51 {
52 return items.remove(value);
53 }
54};
A set built on an unsorted Vector, ensuring no item duplication.
Definition: VectorSet.h:20
auto size() const
Return size of the set.
Definition: VectorSet.h:24
bool contains(const ComparableToValue &value)
Check if the given Value exists in the VectorSet.
Definition: VectorSet.h:33
bool insert(const Value &value)
Inserts a value in the VectorSet (if it doesn't already exists)
Definition: VectorSet.h:39
bool remove(const ComparableToValue &value)
Removes a value from the VectorSet (if it exists)
Definition: VectorSet.h:50