-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.cpp
115 lines (105 loc) · 3.98 KB
/
iterator.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <iterator>
class Photo {
public:
bool favorite;
Photo(bool isFavorite) : favorite(isFavorite) {}
};
class Album {
private:
//std::vector<Photo> photos;
const std::vector<Photo> &photos;
std::vector<size_t> favoritePhotoIndices;
std::vector<size_t> nonFavoritePhotoIndices;
size_t currentNonFavoriteIndex;
size_t currentFavoriteIndex;
//bool favoritePhotosRemaining;
//std::vector<Photo>::const_iterator favoritePhotoIterator;
public:
std::vector<Photo>::const_iterator sentinelIterator;
//Album(const std::vector<Photo>& photos) : photos(photos), favoritePhotosRemaining(true) {
Album(const std::vector<Photo>& photos) : photos(photos) {
for (size_t i = 0; i < photos.size(); ++i) {
if (!photos[i].favorite) {
nonFavoritePhotoIndices.push_back(i);
} else {
favoritePhotoIndices.push_back(i);
}
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(favoritePhotoIndices.begin(), favoritePhotoIndices.end(), g);
std::shuffle(nonFavoritePhotoIndices.begin(), nonFavoritePhotoIndices.end(), g);
currentFavoriteIndex = 0;
currentNonFavoriteIndex = 0;
//favoritePhotoIterator = photos.begin();
sentinelIterator = photos.end();
}
// Function to get the next photo iterator, favoring favorites
//std::vector<Photo>::const_iterator getNextPhoto() {
//if (favoritePhotosRemaining) {
// Look for the next favorite photo starting from the last favorite photo
//while(favoritePhotoIterator != sentinelIterator) {
//if (favoritePhotoIterator->favorite) {
//favoritePhotoIterator++;
//return favoritePhotoIterator - 1;
//} else {
//favoritePhotoIterator++;
//}
//}
//favoritePhotosRemaining = false;
//favoritePhotoIterator = photos.begin();
//}
//if (currentNonFavoriteIndex < nonFavoritePhotoIndices.size()) {
//size_t index = nonFavoritePhotoIndices[currentNonFavoriteIndex];
//++currentNonFavoriteIndex;
//return photos.begin() + index;
//}
//return sentinelIterator;
//}
std::vector<Photo>::const_iterator getNextPhoto() {
if (currentFavoriteIndex < favoritePhotoIndices.size()) {
size_t index = favoritePhotoIndices[currentFavoriteIndex];
++currentFavoriteIndex;
return photos.begin() + index;
}
if (currentNonFavoriteIndex < nonFavoritePhotoIndices.size()) {
size_t index = nonFavoritePhotoIndices[currentNonFavoriteIndex];
++currentNonFavoriteIndex;
return photos.begin() + index;
}
return sentinelIterator;
}
};
int main() {
std::vector<Photo> photos;
photos.push_back(Photo(true)); // Example: favorite photo
photos.push_back(Photo(false)); // Example: non-favorite photo
photos.push_back(Photo(true)); // Example: favorite photo
photos.push_back(Photo(false)); // Example: non-favorite photo
Album myAlbum(photos);
std::vector<Photo>::const_iterator photoIterator;
//while (true) {
//photoIterator = myAlbum.getNextPhoto();
//if (photoIterator == myAlbum.sentinelIterator) {
// break;
//} else {
//if (photoIterator->favorite) {
//std::cout << "Favorite photo" << std::endl;
//} else {
//std::cout << "Non favorite photo" << std::endl;
//}
//}
//}
while((photoIterator = myAlbum.getNextPhoto()) != myAlbum.sentinelIterator) {
if (photoIterator->favorite) {
std::cout << "Favorite photo" << std::endl;
} else {
std::cout << "Non favorite photo" << std::endl;
}
}
return 0;
}