1
0
forked from blue/pica
pica/utils/helpers.h

70 lines
1.8 KiB
C
Raw Normal View History

2023-12-30 22:42:11 +00:00
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <filesystem>
#include <string>
2024-01-03 01:11:56 +00:00
#include <algorithm>
2023-12-11 23:29:55 +00:00
#define UNUSED(variable) (void)variable
void initPaths(const char* programPath);
const std::filesystem::path& sharedPath();
bool endsWith(const std::string& string, const std::string& query);
2023-12-11 23:29:55 +00:00
void ltrim(std::string& string);
void rtrim(std::string& string);
void trim(std::string& string);
std::string extract(std::string& string, std::string::size_type begin, std::string::size_type end);
2023-12-31 17:10:04 +00:00
template <class T>
struct FirstGreater {
2024-01-03 01:11:56 +00:00
bool operator () (const T& left, const T& right) {
2023-12-31 17:10:04 +00:00
return std::get<0>(left) > std::get<0>(right);
}
};
2024-01-03 01:11:56 +00:00
template <class Type, class Container, class Compare = std::less<Type>>
class PriorityQueue {
public:
explicit PriorityQueue(const Compare& compare = Compare()):
container(),
compare(compare)
{}
const Type& top () const {
return container.front();
}
bool empty () const {
return container.empty();
}
template<class... Args>
void emplace (Args&&... args) {
container.emplace_back(std::forward<Args>(args)...);
std::push_heap(container.begin(), container.end(), compare);
}
void push (const Type& element) {
container.push_back(element);
std::push_heap(container.begin(), container.end(), compare);
}
void push (Type&& element) {
container.push_back(std::move(element));
std::push_heap(container.begin(), container.end(), compare);
}
Type pop () {
std::pop_heap(container.begin(), container.end(), compare);
Type result = std::move(container.back());
container.pop_back();
return result;
}
private:
Container container;
Compare compare;
};