Uniot Core
0.8.1
Loading...
Searching...
No Matches
Map.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <Common.h>
22
23#include "IterableQueue.h"
24
25namespace uniot {
26
40template <typename T_Key, typename T_Value>
41class Map : public IterableQueue<Pair<T_Key, T_Value>> {
42 public:
47
59 bool put(const T_Key& key, const T_Value& value) {
60 if (exist(key)) {
61 return false;
62 }
64 return true;
65 }
66
78 const T_Value& get(const T_Key& key, const T_Value& defaultValue = {}) const {
82 if (item.first == key) {
83 return item.second;
84 }
86 }
87 return defaultValue;
88 }
89
100 bool exist(const T_Key& key) const {
104 if (item.first == key) {
105 return true;
106 }
108 }
109 return false;
110 }
111
122 bool remove(const T_Key& key) {
125 const auto& item = IterableQueue<MapItem>::current();
126 if (item.first == key) {
128 }
130 }
131 return false;
132 }
133};
134
135
136} // namespace uniot
void push(const T &value)
Adds an element to the end of the queue.
Definition ClearQueue.h:207
Definition IterableQueue.h:36
const T & next() const
Move to the next element and return the current element.
Definition IterableQueue.h:104
void begin() const
Initialize the iterator to the beginning of the queue.
Definition IterableQueue.h:94
bool isEnd() const
Check if the iterator has reached the end of the queue.
Definition IterableQueue.h:99
const T & current() const
Access the current element without moving the iterator.
Definition IterableQueue.h:111
bool deleteCurrent()
Remove the current element from the queue.
Definition IterableQueue.h:116
Definition Map.h:41
bool put(const T_Key &key, const T_Value &value)
Inserts a key-value pair into the map.
Definition Map.h:59
Pair< T_Key, T_Value > MapItem
Type alias for a key-value pair in the map.
Definition Map.h:46
bool exist(const T_Key &key) const
Checks if a key exists in the map.
Definition Map.h:100
const T_Value & get(const T_Key &key, const T_Value &defaultValue={}) const
Retrieves the value associated with a key.
Definition Map.h:78
bool remove(const T_Key &key)
Removes a key-value pair from the map.
Definition Map.h:122
auto MakePair(Args &&...args) -> decltype(std::make_pair(std::forward< Args >(args)...))
Creates a pair instance, alias for std::make_pair.
Definition Common.h:184
std::pair< T_First, T_Second > Pair
Type alias for std::pair with cleaner syntax.
Definition Common.h:169
Contains all classes and functions related to the Uniot Core.