https://github.com/styczynski/keyed-queue
C++17 STL-friendly queue with map-like keys support
https://github.com/styczynski/keyed-queue
cpp-library cpp17 datastructures queue
Last synced: 5 days ago
JSON representation
C++17 STL-friendly queue with map-like keys support
- Host: GitHub
- URL: https://github.com/styczynski/keyed-queue
- Owner: styczynski
- Created: 2018-01-08T12:37:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-08T14:06:22.000Z (over 8 years ago)
- Last Synced: 2025-03-05T16:16:20.792Z (about 1 year ago)
- Topics: cpp-library, cpp17, datastructures, queue
- Language: C++
- Homepage:
- Size: 53.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Keyed queue
[@styczynski](https://github.com/styczynski) - code of keyed_queue
[@kowaalczyk](https://github.com/kowaalczyk) - increadibly useful handful of tests
## About
This library was written on the *Faculty of Mathematics, Informathics and Mechanics, Warsaw Univeristy*
as a part of **JNP1** (en. Languages and Tools for Programming) subject during **2017/2018** term.
Class `keyed_queue` implements (**copy-on-write**) queue with map-like keys support.
It provides **strong exception quarantee** for all the methods.
The guarantee does not support throwing destructors of key or value.
## Interface
**keyed_queue** class implements the following methods:
- **void push(K const &k, V const &v)**
Inserts value v to the end of the queue assigning the key k.
- **void pop()**
Removes first element from the queue. If the queue is empty then `lookup_error` is thrown.
- **void pop(K const &)**
Removes the first element from the queue that has the given key value. If no element with such key exists in the queue then `lookup_error` is thrown.
- **void move_to_back(K const &k)**
Moves the elements with the given key to the end of the queue preserving their relative order. If no element with the provided key exists in the queue then `lookup_error` is thrown.
- **std::pair front()**
Returns key, value pair for element at the queue front. If the queue is empty then `lookup_error` is thrown. This method allows value modification by reference. Any writing operation performed on the queue may invalidate captured references. Value is unmodificable in `const` version of the method.
- **std::pair back()**
Returns key, value pair for element at the queue back. If the queue is empty then `lookup_error` is thrown. This method allows value modification by reference. Any writing operation performed on the queue may invalidate captured references. Value is unmodificable in `const` version of the method.
- **std::pair front(K const&)**
Returns key, value pair for the first element on the queue with the given key. If the queue is empty or does not contain any element with the given key then `lookup_error` is thrown. This method allows value modification by reference. Any writing operation performed on the queue may invalidate captured references. Value is unmodificable in `const` version of the method.
- **std::pair last(K const&)**
Returns key, value pair for the last element on the queue with the given key. If the queue is empty or does not contain any element with the given key then `lookup_error` is thrown. This method allows value modification by reference. Any writing operation performed on the queue may invalidate captured references. Value is unmodificable in `const` version of the method.
- **size_t size()**
Returns the number of elements on the queue
- **bool empty()**
Returns true if the queue is empty
- **void clear()**
Clears the queue
- **size_t count(K const &)**
Counts elements with the given key
- **keyed_queue::k_iterator k_begin()**
Returns iterator to the first element in the queue. Can be used to iterate queue in STL-like style.
- **keyed_queue::k_iterator k_end()**
Returns past-the-end iterator to the queue. Can be used to iterate queue in STL-like style.
# Building
The examples require the **GCC 7.2.0** compiler to be built.
Other versions may work but they do not have to.
Library itself is portable but the makefile is not.
* Use **make all** to build all examples
* Use **make run-basic-usage** and **make run-** to run the compiled examples
* Use **make clean** to clean the build
# Example usage
```c++
#include "keyed_queue.h"
#include
#include
#include
int main() {
keyed_queue q1;
q1.push(1, 1);
std::cout << q1.to_string() << "\n";
keyed_queue q2 = q1;
std::cout << q2.to_string() << "\n";
q1.push(2, 2);
std::cout << q1.to_string() << "\n";
q2.push(3, 3);
std::cout << q2.to_string() << "\n";
q2.move_to_back(1);
std::cout << q2.to_string() << "\n";
return 0;
}
```