-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.cpp
105 lines (86 loc) · 1.79 KB
/
address.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
/*
* address.cpp
* PhoenixSim
*
* Created by Johnnie Chan on 7/6/11.
* Copyright 2011 Johnnie Chan. All rights reserved.
*
*/
#include "address.h"
#include <assert.h>
using namespace std;
using namespace PhoenixSim;
Address::Address()
{
}
Address::~Address()
{
}
int Address::GetAddress(int addressPos) const
{
return addressComponents[addressPos];
}
void Address::SetAddress(int addressPos, int addressValue)
{
assert(addressPos < addressComponents.size());
addressComponents[addressPos] = addressValue;
}
bool Address::operator ==(const Address & rhs) const
{
if(addressComponents.size() != rhs.addressComponents.size())
{
return false;
}
for(int i = 0; i < addressComponents.size(); i++)
{
if(addressComponents[i] != rhs.addressComponents[i])
{
return false;
}
}
return true;
}
bool Address::operator <(const Address & rhs) const
{
for(int i = 0 ; i < min(this->GetAddressLength(), rhs.GetAddressLength()); i++)
{
if(this->GetAddress(i) != rhs.GetAddress(i))
{
return this->GetAddress(i) < rhs.GetAddress(i);
}
}
return this->GetAddressLength() < rhs.GetAddressLength();
}
bool Address::operator !=(const Address & rhs) const
{
return !((*this) == rhs);
}
Address& Address::operator =(const Address& rhs)
{
addressComponents.resize(rhs.addressComponents.size());
for(int i = 0; i < addressComponents.size(); i++)
{
addressComponents[i] = rhs.addressComponents[i];
}
return *this;
}
void Address::SetAddressLength(int length)
{
addressComponents.resize(length);
}
std::ostream& PhoenixSim::operator <<(std::ostream& os, Address const &addr)
{
for(int i = 0; i < addr.addressComponents.size(); i++)
{
if(i != 0)
{
os << ".";
}
os << addr.GetAddress(i);
}
return os;
}
int Address::GetAddressLength() const
{
return addressComponents.size();
}