-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority.cpp
136 lines (113 loc) · 3.97 KB
/
priority.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* File: priority.cpp
* Description: Implements a priority ordering structure specially made for determining the
* order of neighbours visited in PA2's filler::fill function.
* Note that this does NOT serve the same purpose as the stack/queue
* ordering structure which you have also been asked to implement.
*
* Ignoring resize costs, any single insertion or removal operation
* should cost at most O(n), where n is the number of PixelPoints
* stored in the collection.
*
* Date: 2022-02-10 02:04
*
*/
#include "priority.h"
#include <assert.h>
using namespace cs221util;
using namespace std;
/*
* Default constructor
* Initializes refcolor to the default color according to the HSLAPixel implementation.
*/
PriorityNeighbours::PriorityNeighbours() {
// complete your implementation below
refcolor = HSLAPixel();
}
/*
* Parameterized constructor
* Initializes refcolor to the supplied value.
*/
PriorityNeighbours::PriorityNeighbours(HSLAPixel ref) {
// complete your implementation below
refcolor = ref;
}
/*
* Inserts an item into the collection
* PARAM: p - item to be inserted
* POST: the collection contains p, along with all previously existing items.
*/
void PriorityNeighbours::Insert(PixelPoint p) {
// complete your implementation below
points.push_back(p);
}
/*
* Removes and returns an item from the collection satisfying the priority condition
* PRE: the collection is not empty (but it would be good to check anyway!).
* POST: the item satisfying the priority condition is removed from the collection.
* RETURN: the item satisfying the priority condition
*
* Priority condition:
* The PixelPoint in the collection whose color is the minimum distance away
* from the reference color is the priority item to be returned.
*
* In case of multiple items with the same priority value (i.e. minimal distance
* away from the reference color), the one with the minimal y-coordinate will be
* selected.
* In case of multiple items with the same priority value and the same y-coordinate,
* the one with the minimal x-coordinate will be selected.
*
* ***ATTENTION*** The dist() function in HSLAPixel.h will be very helpful!
*
* Combined with Insert(), think about the time complexity of maintaining the
* priority order and/or accessing the priority element in this specific application!
*/
PixelPoint PriorityNeighbours::Remove() {
// complete your implementation below
int temp = 0;
if (!IsEmpty()) {
for (int i = 1; i < points.size(); i++) {
// if priority is greater than temp
if (points[i].color.dist(refcolor) < points[temp].color.dist(refcolor)) {
temp = i;
} else if (points[i].color.dist(refcolor) == points[temp].color.dist(refcolor)) {
// if y-coord is smaller
if (points[i].y < points[temp].y) {
temp = i;
// if x-coord is smaller
} else if (points[i].y == points[temp].y) {
if (points[i].x < points[temp].x) {
temp = i;
}
}
}
}
}
PixelPoint temp_point = points[temp];
points.erase(points.begin() + temp);
return temp_point; // REPLACE THIS STUB
}
/*
* Checks if the collection is empty
* RETURN: true, if the collection is empty
* false, otherwise
*/
bool PriorityNeighbours::IsEmpty() const {
// complete your implementation below
return points.empty(); // REPLACE THIS STUB
}
/*
* Returns the value of the reference color
*/
HSLAPixel PriorityNeighbours::GetReferenceColor() const {
// complete your implementation below
return refcolor; // REPLACE THIS STUB
}
/*
* Sets the reference color attribute
* POST: refcolor is set to the supplied value
*/
void PriorityNeighbours::SetReferenceColor(HSLAPixel ref) {
// complete your implementation below
refcolor = ref;
}