Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/cpp/templates.txt

1.1 KiB, 1 run

created by r1870400018:11956, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#include <vector>
2#include <string>
3#include <stdexcept>
4#include <algorithm>
5
6template<typename T>
7class Stack {
8public:
9 void push(const T& value) {
10 data_.push_back(value);
11 }
12
13 void push(T&& value) {
14 data_.push_back(std::move(value));
15 }
16
17 T pop() {
18 if (data_.empty()) {
19 throw std::runtime_error("pop from empty stack");
20 }
21 T top = std::move(data_.back());
22 data_.pop_back();
23 return top;
24 }
25
26 const T& top() const {
27 if (data_.empty()) {
28 throw std::runtime_error("top on empty stack");
29 }
30 return data_.back();
31 }
32
33 bool empty() const noexcept { return data_.empty(); }
34 size_t size() const noexcept { return data_.size(); }
35
36private:
37 std::vector<T> data_;
38};
39
40template<typename Container, typename Pred>
41auto count_if(const Container& c, Pred pred) -> size_t {
42 size_t count = 0;
43 for (const auto& item : c) {
44 if (pred(item)) {
45 ++count;
46 }
47 }
48 return count;
49}
50
51template<typename T>
52constexpr T clamp(T val, T lo, T hi) noexcept {
53 return val < lo ? lo : (val > hi ? hi : val);
54}