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
20template <typename Value, typename Container = SC::Vector<Value>>
22{
23 Container items;
24
26 auto size() const { return items.size(); }
27
28 [[nodiscard]] Value* begin() { return items.begin(); }
29 [[nodiscard]] const Value* begin() const { return items.begin(); }
30 [[nodiscard]] Value* end() { return items.end(); }
31 [[nodiscard]] const Value* end() const { return items.end(); }
32
34 template <typename ComparableToValue>
35 [[nodiscard]] bool contains(const ComparableToValue& value)
36 {
37 return items.contains(value);
38 }
39
41 [[nodiscard]] bool insert(const Value& value)
42 {
43 if (items.contains(value))
44 {
45 return true;
46 }
47 return items.push_back(value);
48 }
49
51 template <typename ComparableToValue>
52 [[nodiscard]] bool remove(const ComparableToValue& value)
53 {
54 return items.remove(value);
55 }
56};
A set built on an unsorted Vector, ensuring no item duplication.
Definition: VectorSet.h:22
auto size() const
Return size of the set.
Definition: VectorSet.h:26
bool contains(const ComparableToValue &value)
Check if the given Value exists in the VectorSet.
Definition: VectorSet.h:35
bool insert(const Value &value)
Inserts a value in the VectorSet (if it doesn't already exists)
Definition: VectorSet.h:41
bool remove(const ComparableToValue &value)
Removes a value from the VectorSet (if it exists)
Definition: VectorSet.h:52