-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.cpp
218 lines (168 loc) · 4.69 KB
/
memory.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "memory.h"
/*
Author : Blaz Pocrnja
Student ID : 5035473
Lab #5
Purpose: Member functions to the class "Memory" which simulates computer memory. Stores a specified number of integers, with a default of 100.
*/
Memory::Memory(int s){
/*Constructor takes an int as an argument, allocates the specified amount of space and saves it into size */
m = new int[size = s];
}
Memory::Memory(){
/*Default constructor which takes no parameters, initlializes size to 100*/
m = new int[size = 100];
}
Memory::Memory(const Memory &other){
/*Copy constructor*/
copy(other);
}
Memory::~Memory(void){
/*Destructor: Ensures destruction of pointer*/
delete []m;
}
void Memory::BlockCopy(int source, int destination, int length){
/*Block Copy: One region of Memory is copied into another, copies from source, up to and including, source + length. If the block copied is too long to fit into the destination, then whatever is can fit is copied*/
int tmp[length];
for(int i = source; i <= (source+length); i++){
tmp[i - source] = m[i];
}
if((destination + length) > size){
for(int i = destination; i < size; i++){
m[i] = tmp[i - destination];
}
}
else{
for(int i = destination; i <= (destination+length); i++){
m[i] = tmp[i - destination];
}
}
}
void Memory::insert(int insertion, int location){
/*Stores an integer at any memory location, overwriting whatever was there before. If specified location is larger than last index of Memory, will insert at the end */
if (location > size-1){
printf("\n");
printf("Out of range insertion: Placing at end.");
printf("\n");
printf("\n");
m[size-1] = insertion;
}
else{
m[location] = insertion;
}
}
int Memory::getSize(void){
/*Returns member variable size*/
return size;
}
int Memory::retrieve(int location){
/*Returns element at specified location*/
return m[location];
}
void Memory::dump(int start, int end) const {
/*Prints values stored in Memory in rows of 10 from the closest to start, up to and including closest index of end*/
int i, j, row, index;
index = floor(start/10) * 10; //Sets starting index to the closest and smallest number divisible by 10 i.e 45's index starts at 40 and increments from there
row = ceil(end / 10); //If end value exceeds column length a new row is created
if(end >= size){
printf("\n");
printf("Caution: Index out Range. Will output junk.");
printf("\n \n");
}
printf(" ");
for(i = 0; i < 10; i++){
printf("%5d", i);
}
printf("\n");
for(i = floor(start/10); i <= row; i++){ //i starts at the tens position of the start i.e 45 is in row 4, so it begins there
printf("%05d : ", i);
for(j = 0; j < 10; j++){
printf("%05d" , m[index]);
index++;
}
printf("\n");
}
}
void Memory::dump() const {
/*prints all Memory values*/
dump(0, size - 1);
}
int& Memory::operator[](int n){
/*Returns a reference to an int, as long as the index is not out of bounds*/
if( n >= size ){
printf("\nIndex out of bounds.\n");
// return first element.
return m[0];
}
return m[n];
}
const int& Memory::operator[](int n) const{
/*Returns a reference to a const, as long as the index is not out of bounds*/
if( n >= size ){
printf("\nIndex out of bounds.\n");
// return first element.
return m[0];
}
return m[n];
}
bool Memory::operator==(const Memory& other){
/*compares two Memories to see if they are equal*/
bool equals = false;
if (size == other.size){
for(int i = 0; i < size; i++){
if(m[i] == other.m[i]){
equals = true;
}
else{
equals = false;
break;
}
}
}
return equals;
}
bool Memory::operator!=(const Memory& other){
/*calls == operator and returns the opposite*/
return !(*this == other);
}
Memory& Memory::operator=(const Memory& other){
/*Assigns one Memory's contents into another instance of Memory*/
if(this != &other){;
delete[] m;
copy(other);
}
return *this;
}
void Memory::copy(const Memory& other){
/*Copies contents of other into calling object */
m = new int[other.size];
size = other.size;
for(int i = 0; i < size; i++){
m[i] = other[i];
}
}
Memory operator+(const Memory &a,const Memory &b){
/*Adds two Memories together. If one is larger than the other it simply copies what's left*/
int i;
int newsize = max(a.size, b.size);
Memory additionmem(newsize);
for(i = 0; i < min(a.size, b.size); i++){
additionmem[i] = a[i] + b[i];
}
if(a.size > b.size){
for(i; i < newsize; i++){
additionmem[i] = a[i];
}
}
else if(a.size < b.size){
for(i; i < newsize; i++){
additionmem[i] = b[i];
}
}
return additionmem;
}
ostream& operator<<(ostream &s, const Memory& m){
/*Prints all of Memory*/
m.dump();
return s;
}