AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
labels.h
1#ifndef MERGE_AND_SHRINK_LABELS_H
2#define MERGE_AND_SHRINK_LABELS_H
3
4#include <memory>
5#include <vector>
6
7namespace merge_and_shrink {
8/*
9 Iterator class for Labels.
10
11 The iterator provides the *index* into label_costs of Labels, which is the
12 ID of the label.
13
14 Implementation note: to avoid keeping a reference to label_costs, this class
15 stores the current position (curr_pos) in addition to the iterator (it) which
16 are always incremented in parallel.
17*/
18class LabelsConstIterator {
19 const std::vector<int>::const_iterator end_it;
20 std::vector<int>::const_iterator it;
21 std::size_t current_pos;
22
23 void advance_to_next_valid_index();
24
25public:
26 LabelsConstIterator(
27 const std::vector<int>& label_costs,
28 std::vector<int>::const_iterator it);
29 LabelsConstIterator& operator++();
30
31 int operator*() const { return static_cast<int>(current_pos); }
32
33 bool operator==(const LabelsConstIterator& rhs) const
34 {
35 return it == rhs.it;
36 }
37
38 bool operator!=(const LabelsConstIterator& rhs) const
39 {
40 return it != rhs.it;
41 }
42};
43
44/*
45 This class serves both as a container class to handle the set of all
46 labels and to perform label reduction on this set.
47
48 Labels are identified via integers indexing label_costs, which stores their
49 costs. When using label reductions, labels that become inactive are set to
50 -1 in label_costs.
51*/
52class Labels {
53 std::vector<int> label_costs;
54 int max_num_labels; // The maximum number of labels that can be created.
55 int num_active_labels; // The current number of active (non-reduced) labels.
56public:
57 Labels(std::vector<int>&& label_costs, int max_num_labels);
58 void reduce_labels(const std::vector<int>& old_labels);
59 int get_label_cost(int label) const;
60 void dump_labels() const;
61
62 // The summed number of both inactive and active labels.
63 int get_num_total_labels() const { return label_costs.size(); }
64
65 int get_max_num_labels() const { return max_num_labels; }
66
67 int get_num_active_labels() const { return num_active_labels; }
68
69 LabelsConstIterator begin() const
70 {
71 return LabelsConstIterator(label_costs, label_costs.begin());
72 }
73
74 LabelsConstIterator end() const
75 {
76 return LabelsConstIterator(label_costs, label_costs.end());
77 }
78};
79} // namespace merge_and_shrink
80
81#endif