updated pa4
git-svn-id: svn://anubis/gvsu@299 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
2e3b5abd0f
commit
750c1e2cf3
@ -10,3 +10,39 @@ ElementSet::ElementSet(char initial)
|
||||
{
|
||||
mySet.insert(initial);
|
||||
}
|
||||
|
||||
ElementSet ElementSet::Union(const ElementSet & other)
|
||||
{
|
||||
ElementSet es;
|
||||
for (set<char>::iterator it = mySet.begin(); it != mySet.end(); it++)
|
||||
es.mySet.insert(*it);
|
||||
for (set<char>::iterator it = other.mySet.begin(); it != other.mySet.end(); it++)
|
||||
es.mySet.insert(*it);
|
||||
return es;
|
||||
}
|
||||
|
||||
ElementSet ElementSet::Intersection(const ElementSet & other)
|
||||
{
|
||||
ElementSet es;
|
||||
for (set<char>::iterator it = mySet.begin(); it != mySet.end(); it++)
|
||||
{
|
||||
set<char>::iterator find_it = other.mySet.find(*it);
|
||||
if (find_it != other.mySet.end())
|
||||
es.mySet.insert(*it);
|
||||
}
|
||||
return es;
|
||||
}
|
||||
|
||||
ostream & operator<<(std::ostream & out, const ElementSet & e)
|
||||
{
|
||||
int size = e.mySet.size();
|
||||
if (size > 1)
|
||||
out << '{';
|
||||
for (set<char>::iterator it = e.mySet.begin(); it != e.mySet.end(); it++)
|
||||
{
|
||||
out << *it;
|
||||
}
|
||||
if (size > 1)
|
||||
out << '}';
|
||||
return out;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#define ELEMENTSET_H ELEMENTSET_H
|
||||
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
|
||||
class ElementSet
|
||||
{
|
||||
@ -12,6 +13,11 @@ private:
|
||||
public:
|
||||
ElementSet();
|
||||
ElementSet(char initial);
|
||||
int size() { return mySet.size(); }
|
||||
ElementSet Union(const ElementSet & other);
|
||||
ElementSet Intersection(const ElementSet & other);
|
||||
|
||||
friend std::ostream & operator<<(std::ostream & out, const ElementSet & e);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -9,7 +9,13 @@ Sequence::Sequence()
|
||||
myMutationCount = 0;
|
||||
}
|
||||
|
||||
Sequence::Sequence(char * initializer)
|
||||
Sequence::Sequence(int size)
|
||||
{
|
||||
myMutationCount = 0;
|
||||
myElements = vector<ElementSet>(size);
|
||||
}
|
||||
|
||||
Sequence::Sequence(const char * initializer)
|
||||
{
|
||||
myMutationCount = 0;
|
||||
int len = strlen(initializer);
|
||||
@ -19,3 +25,31 @@ Sequence::Sequence(char * initializer)
|
||||
myElements[i] = ElementSet(initializer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Sequence Sequence::parent(const Sequence & other)
|
||||
{
|
||||
int size = myElements.size();
|
||||
Sequence s(size);
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
ElementSet intersection = myElements[i].Intersection(other.myElements[i]);
|
||||
if (intersection.size() == 0)
|
||||
{
|
||||
s.myElements[i] = myElements[i].Union(other.myElements[i]);
|
||||
s.myMutationCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
s.myElements[i] = intersection;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & out, const Sequence & s)
|
||||
{
|
||||
int sz = s.myElements.size();
|
||||
for (int i = 0; i < sz; i++)
|
||||
out << s.myElements[i];
|
||||
return out;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#define SEQUENCE_H SEQUENCE_H
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "ElementSet.h"
|
||||
|
||||
class Sequence
|
||||
@ -13,7 +14,11 @@ private:
|
||||
|
||||
public:
|
||||
Sequence();
|
||||
Sequence(char * initializer);
|
||||
Sequence(int size);
|
||||
Sequence(const char * initializer);
|
||||
Sequence parent(const Sequence & other);
|
||||
|
||||
friend std::ostream & operator<<(std::ostream & out, const Sequence & s);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,18 +1,99 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/time.h>
|
||||
#include <mpi.h>
|
||||
#include <stdlib.h> /* exit() */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include "Sequence.h"
|
||||
using namespace std;
|
||||
|
||||
void usage()
|
||||
{
|
||||
cerr << "Usage: maximum-parsimony <sequence-file>" << endl;
|
||||
exit(42);
|
||||
}
|
||||
|
||||
string getsequence(int fd)
|
||||
{
|
||||
string s;
|
||||
char chr;
|
||||
bool saw_name = false;
|
||||
for (;;)
|
||||
{
|
||||
int bytes_read = read(fd, &chr, 1);
|
||||
if (bytes_read < 1)
|
||||
{
|
||||
if (s == "")
|
||||
break;
|
||||
else
|
||||
{
|
||||
cerr << "Unexpected EOF in the middle of a line!" << endl;
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
else if (chr == '\n')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (chr == ' ' || chr == '\t')
|
||||
{
|
||||
saw_name = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (saw_name)
|
||||
s += chr;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int my_rank;
|
||||
int world_size; /* the number of processes */
|
||||
char * filename = NULL;
|
||||
|
||||
MPI_Init(&argc, &argv);
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
|
||||
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
|
||||
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
filename = argv[i];
|
||||
}
|
||||
|
||||
if (filename == NULL)
|
||||
usage();
|
||||
|
||||
int fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
cerr << "Couldn't open '" << filename << "'!" << endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
vector<Sequence *> sequences;
|
||||
for (;;)
|
||||
{
|
||||
string seqstr = getsequence(fd);
|
||||
if (seqstr == "")
|
||||
break;
|
||||
Sequence * seq = new Sequence(seqstr.c_str());
|
||||
sequences.push_back(seq);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
cout << "Found " << sequences.size() << " sequences:" << endl;
|
||||
for (int i = 0, sz = sequences.size(); i < sz; i++)
|
||||
{
|
||||
cout << *sequences[i] << endl;
|
||||
}
|
||||
|
||||
MPI_Finalize();
|
||||
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user