2 #include "sparsematrix.hpp"
3 #include "listvector.hpp"
5 typedef list<PCell>::iterator MatData;
7 /***********************************************************************/
9 SparseMatrix::SparseMatrix() : numNodes(0), minsIndex(0), smallDist(1e6){}
11 /***********************************************************************/
13 int SparseMatrix::getNNodes(){
17 /***********************************************************************/
19 float SparseMatrix::getSmallDist(){
23 /***********************************************************************/
25 void SparseMatrix::rmCell(MatData data){
27 if(data->vectorMap != NULL ){
28 *(data->vectorMap) = NULL;
29 data->vectorMap = NULL;
34 // seems like i should be updating smallDist here, but the only time we remove cells is when
35 // clustering and the clustering algorithm updates smallDist
38 errorOut(e, "SparseMatrix", "rmCell");
43 /***********************************************************************/
45 void SparseMatrix::addCell(PCell value){
47 matrix.push_back(value);
49 if(value.dist < smallDist){
50 smallDist = value.dist;
54 errorOut(e, "SparseMatrix", "addCell");
59 /***********************************************************************/
61 void SparseMatrix::clear(){
70 errorOut(e, "SparseMatrix", "clear");
75 /***********************************************************************/
77 MatData SparseMatrix::begin(){
78 return matrix.begin();
81 /***********************************************************************/
83 MatData SparseMatrix::end(){
87 /***********************************************************************/
89 void SparseMatrix::print(){
94 mothurOut("Index\tRow\tColumn\tDistance");
97 for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
98 mothurOut(toString(index) + "\t" + toString(currentCell->row) + "\t" + toString(currentCell->column) + "\t" + toString(currentCell->dist)); mothurOutEndLine();
102 catch(exception& e) {
103 errorOut(e, "SparseMatrix", "print");
108 /***********************************************************************/
110 void SparseMatrix::print(ListVector* list){
115 mothurOut("Index\tRow\tColumn\tDistance");
119 for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
120 mothurOut(toString(index) + "\t" + toString(list->get(currentCell->row)) + "\t" + toString(list->get(currentCell->column)) + "\t" + toString(currentCell->dist)); mothurOutEndLine();
124 catch(exception& e) {
125 errorOut(e, "SparseMatrix", "print");
130 /***********************************************************************/
132 PCell* SparseMatrix::getSmallestCell(){
134 // this is where I check to see if the next small distance has the correct distance
135 // if it doesn't then I remove the offending Cell -> should also be able to check for
136 // invalid iterator / pointer -- right???
138 while(!mins.empty() && mins.back() == NULL){
142 // if the mins vector is empty go here...
146 smallDist = begin()->dist; //set the first candidate small distance
148 for(MatData currentCell=begin();currentCell!=end();currentCell++){
150 float dist = currentCell->dist;
152 if(dist < smallDist){ //found a new smallest distance
155 mins.push_back(&*currentCell); //this is the address of the data in the list being pointed to by the MatData iterator
157 else if(dist == smallDist){ //if a subsequent distance is the same as mins distance add the new iterator to the mins vector
158 mins.push_back(&*currentCell); //this is the address of the data in the list being pointed to by the MatData iterator
162 random_shuffle(mins.begin(), mins.end()); //randomize the order of the iterators in the mins vector
164 for(int i=0;i<mins.size();i++){
165 mins[i]->vectorMap = &mins[i]; //assign vectorMap to the address for the container
170 smallCell = mins.back(); //make the smallestCell the last element of the vector
172 mins.pop_back(); //remove the last element from the vector
176 catch(exception& e) {
177 errorOut(e, "SparseMatrix", "getSmallestCell");
182 /***********************************************************************/