aboutsummaryrefslogtreecommitdiff
path: root/include/utility/container.h
blob: 489f7fd410d5fb801240d3b53fd525aead4ad600 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2022 Sadie Powell <sadie@witchery.services>
 *
 * This file is part of InspIRCd.  InspIRCd is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#pragma once


namespace insp
{
	/** Checks whether an element with the given value is in a container.
	 * @param container The ontainer to find the element in.
	 * @param value The value of the element to look for.
	 * @return True if the element was found in the container; otherwise, false.
	 */
	template <typename Container, typename Value = typename Container::value_type>
	inline bool contains(const Container& container, const Value& value)
	{
		return std::find(container.begin(), container.end(), value) != container.end();
	}

	template <typename Container, typename Key = typename Container::key_type, typename Value = typename Container::mapped_type>
	inline std::remove_pointer_t<Value>* find_value(const Container& container, const Key& key)
	{
		auto it = container.find(key);
		if (it == container.end())
			return nullptr;

		return insp::to_ptr(it->second);
	}

	/** Combines a sequence of elements into a string.
	 * @param sequence A sequence of elements to combine.
	 * @param separator The value to separate the elements with. Defaults to ' ' (space).
	 */
	template<typename Collection, typename Separator = char>
	inline std::string join(const Collection& sequence, Separator separator = ' ')
	{
		std::string joined;
		if (sequence.empty())
			return joined;

		const std::string separatorstr = ConvToStr(separator);
		for (const auto& element : sequence)
			joined.append(ConvToStr(element)).append(separatorstr);

		joined.erase(joined.end() - separatorstr.length());
		joined.shrink_to_fit();
		return joined;
	}

	/** Compares two maps and returns a list of the differences.
	 * @param first The first map to compare.
	 * @param second The second map to compare.
	 * @param out The map to store the differences in.
	 */
	template<typename Key, typename Value, typename Compare, template<typename...> typename Map>
	void map_difference(const Map<Key, Value, Compare>& first, const Map<Key, Value, Compare>& second,
		Map<Key, std::pair<std::optional<Value>, std::optional<Value>>, Compare>& out)
	{
		auto fiter = first.cbegin();
		auto siter = second.cbegin();

		while (fiter != first.end())
		{
			if (siter == second.end())
			{
				// Store the remaining from the first.
				while (fiter != first.end())
				{
					out[fiter->first] = { fiter->second, std::nullopt };
					fiter++;
				}
				return;
			}

			if (fiter->first < siter->first)
			{
				// The key in first is not in second.
				out[fiter->first] = { fiter->second, std::nullopt };
				fiter++;
			}
			else if (siter->first < fiter->first)
			{
				// The key in second is not in first.
				out[siter->first] = { std::nullopt, siter->second };
				siter++;
			}
			else if (fiter->second != siter->second)
			{
				// The key exists in both but the value differs.
				out[fiter->first] = { fiter->second, siter->second };
				fiter++;
				siter++;
			}
			else
			{
				// The key/value is identical in both.
				fiter++;
				siter++;
			}
		}

		// Store the remaining from the second.
		while (siter != second.end())
		{
			out[siter->first] = { std::nullopt, siter->second };
			siter++;
		}
	}

	/** Erase a single element from a container by overwriting it with a copy of the last element,
	 * which is then removed. This, in contrast to rase(), does not result in all elements after
	 * the erased element being moved. All iterators, references and pointers to the erased element
	 * and the last element are invalidated
	 * @param container The container to remove the element from.
	 * @param it An iterator to the element to remove.
	 */
	template <typename Container>
	inline void swap_erase(Container& container, const typename Container::iterator& it)
	{
		*it = std::move(container.back());
		container.pop_back();
	}

	/** Find and erase a single element from a vector by overwriting it with a copy of the last
	 * element, which is then removed. This, in contrast to erase(), does not result in all elements
	 * after the erased element being moved. If the given value occurs multiple times, the one with
	 * the lowest index is removed. If the element is found then  all iterators, references and
	 * pointers to the erased element and the last element are invalidated.
	 * @param container The container to remove the element from.
	 * @param value The value to look for and remove.
	 * @return True if the element was found and removed; otherwise, false.
	 */
	template <typename Container>
	inline bool swap_erase(Container& container, const typename Container::value_type& value)
	{
		auto it = std::find(container.begin(), container.end(), value);
		if (it != container.end())
		{
			swap_erase(container, it);
			return true;
		}
		return false;
	}
}