-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.cpp
More file actions
110 lines (100 loc) · 2.66 KB
/
Copy pathprimes.cpp
File metadata and controls
110 lines (100 loc) · 2.66 KB
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
#include "primes.h"
Primes::Primes()
{
this->EratosfenesAlgorithm(100);
}
Primes::Primes(uint32_t max)
{
if (max != 0)
this->EratosfenesAlgorithm(max);
}
Primes::Primes(uint32_t quantity, ContainerType type):typeFlag(UNLIMITED)
{
if (quantity == 0)
return;
this->containerData = new uint32_t[quantity];
for (size_t i = 0 ,j = 1;i < quantity;j++)
if(isPrime(j))
this->containerData[i++] = j;
this->length = quantity;
}
Primes::Primes(const Primes& other):
length(other.length),
typeFlag(other.typeFlag),
containerData(new uint32_t[other.length])
{
for (size_t i = 0; i < other.length ; i++)
this->containerData[i] = other.containerData[i];
}
Primes::Primes(Primes&& other)
:containerData(other.containerData),
length(other.length),
typeFlag(other.typeFlag)
{
other.containerData = nullptr;
other.length = 0;
other.typeFlag = false;
}
void Primes::operator=(const Primes& other)
{
if (&other == this)
return ;
delete [] this->containerData;
this->length = other.length;
this->typeFlag = other.typeFlag;
this->containerData = new uint32_t[this->length];
for (size_t i = 0; i < this->length ; i++)
this->containerData[i] = other.containerData[i];
}
void Primes::operator=(Primes&& other)
{
delete [] this->containerData;
this->containerData = other.containerData;
this->length = other.length;
this->typeFlag = other.typeFlag;
other.length = 0;
other.typeFlag = false;
other.containerData = nullptr;
}
uint32_t Primes::operator[](uint32_t index)
{
if (index >= this->length)
throw std::out_of_range("Out of range");
return this->containerData[index];
}
Primes::~Primes()
{
delete [] this->containerData;
this->containerData = nullptr;
}
Primes::Iterator::Iterator(uint32_t* pointer)
{
this->pointer = pointer;
}
inline Primes::Iterator& Primes::Iterator::operator++()
{
this->pointer++;
return *this;
}
inline uint32_t Primes::Iterator::operator*()
{
return *this->pointer;
}
inline bool Primes::Iterator::operator==(const Iterator& other)
{
return this->pointer == other.pointer;
}
inline bool Primes::Iterator::operator!=(const Iterator& other)
{
return !(this->pointer == other.pointer);
}
Primes::Iterator Primes::begin()
{
return Iterator(this->containerData);
}
Primes::Iterator Primes::end()
{
if (this->typeFlag == UNLIMITED)
return Iterator(nullptr);
return Iterator(this->containerData + this->length);
}