-
Notifications
You must be signed in to change notification settings - Fork 1
/
LongestCommonPrefix.cpp
102 lines (90 loc) · 2.56 KB
/
LongestCommonPrefix.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
/*
* LongestCommonPrefix.cpp
*
* This is the header file for the LongestCommonPrefix object. The Longest
* CommonPrefix object holds all information pertaining to the longest common
* prefix shared by two or more suffixes. The main attributes are:
*
* prefix - the prefix shared by the suffixes
* suffixes - a collection of Suffix objects that share the prefix
* length - the length of the prefix
* sortIndex - the index of the first suffix in the main sorted suffixes
* collection on the LongestCommonPrefixUtil object
*
* Typical use:
* 1. LongestCommonPrefix(aPrefix, aSortIndex) - instantiate object
* 2. addSuffix(aSuffix) - add a suffix that shares the prefix (repeat
* until all suffixes that share the prefix have
* been added
* 3. toString() - returns a XML result string representing the object
*
* Created on: 1-11-13
* Author: tomkolar
*/
#include "LongestCommonPrefix.h"
#include <sstream>
#include <vector>
#include <set>
using namespace std;
// Constuctors
// ==============================================
LongestCommonPrefix::LongestCommonPrefix() {
}
LongestCommonPrefix::LongestCommonPrefix(string aPrefix, int aSortIndex) {
prefix = aPrefix;
length = aPrefix.length();
sortIndex = aSortIndex;
}
// Destructor
// =============================================
LongestCommonPrefix::~LongestCommonPrefix() {
}
// Public Methods
// =============================================
// addSuffix(Suffix*& aSuffix)
// Purpose:
// Adds aSuffix to the suffixes collection
// Preconditions:
// Postconditions:
// aSuffixe added to suffixes vector
void LongestCommonPrefix::addSuffix(Suffix*& aSuffix) {
suffixes.push_back(aSuffix);
}
// string toString()
// Purpose:
// Returns a formatted XML element representing the prefix and
// the suffixes it contains
//
// format:
// <result type='DNA Sequence'> >
// <<suffix1.toString(length)>>
// <<suffix2.toString(length)>>
// ...
// <<suffixN.toString(length)>>
// <<prefix>>
// </result>
// Preconditions:
// Object has been fully populated
string LongestCommonPrefix::toString() {
stringstream ss;
ss << " <result type='DNA Sequence'>\n";
for (Suffix* suffix : suffixes) {
ss << suffix->toString(length);
}
ss << " ";
ss << prefix;
ss << "\n";
ss << " </result>\n";
return ss.str();
}
// Public Accessors
// =============================================
string& LongestCommonPrefix::getPrefix() {
return prefix;
}
int& LongestCommonPrefix::getSortIndex() {
return sortIndex;
}
int& LongestCommonPrefix::getLength() {
return length;
}