/
Isam.cpp
115 lines (100 loc) · 2.21 KB
/
Isam.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
//
// Isam.cpp
// Sparse Matrix
//
// Created by Ian Murphy on 10/22/15.
// Copyright © 2015 Ian Murphy. All rights reserved.
//
#include "Isam.hpp"
#include <iostream>
extern std::ofstream fout;
Isam::Isam()
{
for(int i = 0; i< MAX_COL_HEIGHT; ++i)
{
m_col[i] = new Index;
}
}
void Isam::Insert(Person * newPerson)
{
bool done = false;
int col = 0;
node * nn = m_col[0]->CreateNode(newPerson);
while(!done)
{
if(m_col[col +1]->GetSize() == 0 || _CASECMP(m_col[col+1]->GetFirst(), nn->_data->GetLast()) > 0)
{
m_col[col]->Insert(nn);
if(m_col[col]->GetSize() > MAX_ROW_SIZE)
{
nn = m_col[col]->GetLast();
ShiftDown(col);
col++;
}
else
done = true;
}
else
{
col++;
}
}
}
bool Isam::Delete(coords loc)
{
m_col[loc.col]->Delete(loc.row);
if(m_col[loc.col]->GetSize() == 0 )
{
ShiftUp(loc.col+1);
}
return true;
}
coords Isam::Search(char * query)
{
int col = 0;
coords f;
bool done = false;
while(!done)
{
if(m_col[col +1]->GetFirst()[0] == '\0' || _CASECMP(m_col[col+1]->GetFirst(), query) > 0)
{
f.row = m_col[col]->Search(query);
done = true;
}
else
{
col++;
}
}
f.col = col;
return f;
}
void Isam::ShiftDown(int offset)
{
Index * hold = m_col[MAX_COL_HEIGHT - 1];
if(m_col[offset+1]->GetSize() == MAX_ROW_SIZE)
{
for(int i = 0; i< MAX_COL_HEIGHT - (offset+2); ++i)
{
m_col[MAX_COL_HEIGHT - (i +1)] = m_col[MAX_COL_HEIGHT-(i+2)];
}
m_col[offset+1] = hold;
}
}
void Isam::ShiftUp(int offset)
{
delete m_col[offset - 1];
for(int i = offset; i< MAX_COL_HEIGHT -1; ++i)
{
m_col[i-1] = m_col[i];
}
}
void Isam::Display(int col1, int col2)
{
fout<<"\nDisplaying row "<<col1<<" through row "<<col2<<"."<<std::endl;
for(int i = col1; i<col2+1; ++i)
{
fout<<"-------ROW "<<i<<"-------"<<std::endl;
m_col[i]->Display();
}
}