forked from landerrosette/algs4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderedST.h
45 lines (28 loc) · 1.12 KB
/
OrderedST.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef ALGS4_ORDEREDST_H
#define ALGS4_ORDEREDST_H
#include "ST.h"
#include <list>
#include <optional>
template<typename Key, typename Value>
class OrderedST : public ST<Key, Value> {
public:
virtual std::optional<Key> min() const = 0;
virtual std::optional<Key> max() const = 0;
virtual std::optional<Key> floor(const Key &key) const = 0;
virtual std::optional<Key> ceiling(const Key &key) const = 0;
virtual int rank(const Key &key) const = 0;
virtual std::optional<Key> select(int k) const = 0;
virtual void removeMin() { this->remove(*min()); }
virtual void removeMax() { this->remove(*max()); }
using ST<Key, Value>::size;
int size(const Key &lo, const Key &hi) const;
std::list<Key> keys() const override { return keys(*min(), *max()); }
virtual std::list<Key> keys(const Key &lo, const Key &hi) const = 0;
};
template<typename Key, typename Value>
int OrderedST<Key, Value>::size(const Key &lo, const Key &hi) const {
if (hi < lo) return 0;
else if (this->contains(hi)) return rank(hi) - rank(lo) + 1;
else return rank(hi) - rank(lo);
}
#endif //ALGS4_ORDEREDST_H