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

70 lines
1.8 KiB
C++

//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <filesystem>
#include <string>
#include <algorithm>
#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);
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);
template <class T>
struct FirstGreater {
bool operator () (const T& left, const T& right) {
return std::get<0>(left) > std::get<0>(right);
}
};
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;
};