-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.cpp
109 lines (95 loc) · 2.99 KB
/
Storage.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
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cassert>
#include <tuple>
#include <assert.h>
#include "Grid.h"
#include "Storage.h"
using namespace std;
Storage::Storage(): unitlist_r(9,vector<string>(9)),unitlist_c(9,vector<string>(9)),unitlist_b(9,vector<string>(9))
{ //cout << "A Storage instance has been constructed!" << endl;
digits = "123456789";
rows = "ABCDEFGHI";
cols = digits;
rows_threes = {"ABC","DEF","GHI"};
cols_threes = {"123","456","789"};
squares = cross(rows,cols);
//creating unitlist
for(int i = 0; i < 9; ++i){
for(int j = 0; j < 9; ++j){
unitlist_r[i][j] = cross(rows.substr(i,1),cols)[j];
unitlist_c[i][j] = cross(rows,cols.substr(i,1))[j];
}}
for(int i = 0; i < 3; ++i){
for(int j = 0; j < 3; ++j){
for(int k = 0; k < 9; ++k){
unitlist_b[3*i+j][k] = cross(rows_threes[i],cols_threes[j])[k];
}}}
unitlist.insert(unitlist.end(),unitlist_r.begin(),unitlist_r.end());
unitlist.insert(unitlist.end(),unitlist_c.begin(),unitlist_c.end());
unitlist.insert(unitlist.end(),unitlist_b.begin(),unitlist_b.end());
//checking unitlist
if(false){
cout << "checking unitlist: " << endl;
for(int i = 0; i < 27; ++i){
for(int j = 0; j < 9; ++j){
cout<< "ith row " << i << " contains: "<< unitlist[i][j] << endl;}}}
//creating units object
for(int i = 0; i < 81; ++i){
for(int j = 0; j < 27; ++j){
if(find(unitlist[j].begin(),unitlist[j].end(),squares[i]) != unitlist[j].end()){
vector<string> temp;
temp = unitlist[j];
units[squares[i]].push_back(temp);}}}
//checking units
if(false){
cout << "checking units: " << endl;
for(int i = 0; i < 3; ++i){
for(int j = 0; j < 9; ++j){
cout<< i<< "-th row contains: " << units["C2"][i][j] << endl;}}}
//defining peers
for(int i = 0 ; i < 81; ++i){
set<string> temp = {};
for(int j = 0; j < 3; ++j){
set<string> insert_set (units[squares[i]][j].begin(),units[squares[i]][j].end());
temp.insert(insert_set.begin(), insert_set.end());
}
temp.erase(squares[i]);
peers[squares[i]] = temp;
}
//checking peers
if(false){
cout << "checking peers: " << endl;
for(int i = 0; i < 81; ++i){
cout<< squares[i]<< " has peers: " << endl;
for (string const& peer : peers[squares[i]]){
cout << peer << endl;}
}}
test();
}
vector<string> Storage::cross(string A, string B){
vector<string> AB(A.length()*B.length());
for(int i=0; i< A.length(); i++){
for(int j = 0; j < B.length(); j++){
AB.at(j+i*B.length()) = A.substr(i,1)+B.substr(j,1);
}}
return AB;}
void Storage::test(){
assert(squares.size() == 81);
assert(unitlist.size() == 27);
for(int i = 0; i < squares.size(); ++i){
assert((units[squares[i]]).size()==3);
assert((units[squares[i]][0]).size()==9);
assert((units[squares[i]][1]).size()==9);
assert((units[squares[i]][2]).size()==9);
}
for (int i = 0; i< squares.size(); ++i){
assert((peers[squares[i]]).size() == 20);
}
}
Storage::~Storage() {
//cout << "A Storage instance has been destroyed!"<<endl;
}