-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortList.cpp
56 lines (49 loc) · 1.55 KB
/
ShortList.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
#include "ShortList.h"
#include <algorithm> // For std::find
#include <iostream>
ShortList::ShortList(std::mt19937& rng) : size(0), rng(rng) {
// Initialize containsArr and indexArr
std::fill(containsArr, containsArr + MAX_SIZE, false);
std::fill(indexArr, indexArr + MAX_SIZE, -1); // Invalid index
}
bool ShortList::insert(int num) {
if (size < MAX_SIZE && !contains(num)) {
arr[size] = num;
containsArr[num] = true;
indexArr[num] = size;
++size;
return true;
}
return false; // List is full or integer already exists
}
bool ShortList::remove(int num) {
if (contains(num)) {
int idx = indexArr[num];
arr[idx] = arr[--size]; // Replace the element with the last element and decrement size
containsArr[arr[idx]] = true; // Update containsArr for the moved element
containsArr[num] = false;
indexArr[arr[idx]] = idx; // Update indexArr for the moved element
indexArr[num] = -1; // Invalidate the index of the removed element
return true;
}
return false; // Element not found
}
bool ShortList::contains(int num) const {
return num >= 0 && num < MAX_SIZE && containsArr[num];
}
int ShortList::getRandomElement() const {
if (size > 0) {
return arr[rng() % size];
}
return -1; // No elements in the list
}
int ShortList::getSize() const {
return size;
}
void ShortList::print() const {
std::cout << "List: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}