2 #include "sparsematrix.hpp"
3 #include "listvector.hpp"
6 /***********************************************************************/
8 SparseMatrix::SparseMatrix() : numNodes(0), minsIndex(0), smallDist(1e6){ m = MothurOut::getInstance(); }
10 /***********************************************************************/
12 int SparseMatrix::getNNodes(){
16 /***********************************************************************/
18 float SparseMatrix::getSmallDist(){
22 /***********************************************************************/
24 MatData SparseMatrix::rmCell(MatData data){
26 if(data->vectorMap != NULL ){
27 *(data->vectorMap) = NULL;
28 data->vectorMap = NULL;
30 data = matrix.erase(data);
33 // seems like i should be updating smallDist here, but the only time we remove cells is when
34 // clustering and the clustering algorithm updates smallDist
37 m->errorOut(e, "SparseMatrix", "rmCell");
42 /***********************************************************************/
44 void SparseMatrix::addCell(PCell value){
46 matrix.push_back(value);
48 if(value.dist < smallDist){
49 smallDist = value.dist;
53 m->errorOut(e, "SparseMatrix", "addCell");
58 /***********************************************************************/
60 void SparseMatrix::clear(){
69 m->errorOut(e, "SparseMatrix", "clear");
74 /***********************************************************************/
76 MatData SparseMatrix::begin(){
77 return matrix.begin();
80 /***********************************************************************/
82 MatData SparseMatrix::end(){
86 /***********************************************************************/
88 void SparseMatrix::print(){
92 cout << endl << "Index\tRow\tColumn\tDistance" << endl;
94 for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
95 cout << index << '\t' << currentCell->row << '\t' << currentCell->column << '\t' << currentCell->dist << endl;
100 m->errorOut(e, "SparseMatrix", "print");
105 /***********************************************************************/
107 void SparseMatrix::print(ListVector* list){
111 m->mothurOutEndLine(); m->mothurOut("Index\tRow\tColumn\tDistance"); m->mothurOutEndLine();
113 for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
114 m->mothurOut(toString(index) + "\t" + toString(list->get(currentCell->row)) + "\t" + toString(list->get(currentCell->column)) + "\t" + toString(currentCell->dist)); m->mothurOutEndLine();
118 catch(exception& e) {
119 m->errorOut(e, "SparseMatrix", "print");
124 /***********************************************************************/
126 PCell* SparseMatrix::getSmallestCell(){
128 // this is where I check to see if the next small distance has the correct distance
129 // if it doesn't then I remove the offending Cell -> should also be able to check for
130 // invalid iterator / pointer -- right???
132 while(!mins.empty() && mins.back() == NULL){
136 // if the mins vector is empty go here...
140 smallDist = begin()->dist; //set the first candidate small distance
142 for(MatData currentCell=begin();currentCell!=end();currentCell++){
144 float dist = currentCell->dist;
146 if(dist < smallDist){ //found a new smallest distance
149 mins.push_back(&*currentCell); //this is the address of the data in the list being pointed to by the MatData iterator
151 else if(dist == smallDist){ //if a subsequent distance is the same as mins distance add the new iterator to the mins vector
152 mins.push_back(&*currentCell); //this is the address of the data in the list being pointed to by the MatData iterator
156 random_shuffle(mins.begin(), mins.end()); //randomize the order of the iterators in the mins vector
158 for(int i=0;i<mins.size();i++){
159 mins[i]->vectorMap = &mins[i]; //assign vectorMap to the address for the container
164 smallCell = mins.back(); //make the smallestCell the last element of the vector
166 mins.pop_back(); //remove the last element from the vector
170 catch(exception& e) {
171 m->errorOut(e, "SparseMatrix", "getSmallestCell");
176 /***********************************************************************/