[llvm-commits] CVS: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/README cell.hh constants.hh coordinate.hh obstacle.hh ocean.cpp ocean.hh predator.hh prey.hh random.hh

Chris Lattner lattner at cs.uiuc.edu
Mon Oct 4 13:01:30 PDT 2004



Changes in directory llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean:

README added (r1.1)
cell.hh added (r1.1)
constants.hh added (r1.1)
coordinate.hh added (r1.1)
obstacle.hh added (r1.1)
ocean.cpp added (r1.1)
ocean.hh added (r1.1)
predator.hh added (r1.1)
prey.hh added (r1.1)
random.hh added (r1.1)
---
Log message:

Initial checkin of all of the source


---
Diffs of the changes:  (+446 -0)

Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/README
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/README:1.1
*** /dev/null	Mon Oct  4 15:01:24 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/README	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,2 ----
+ Listings 8.1 to 8.14.  "The C++ Workbook" by Wiener and Pinson,
+ Addison-Wesley, 1990.


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/cell.hh
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/cell.hh:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/cell.hh	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,36 ----
+ #ifndef CellDef
+ #define CellDef
+ 
+ class Cell {
+ friend class Ocean;
+ 
+ protected:
+   Coordinate *offset;
+   char image;
+ 
+   Cell *getCellAt(Coordinate aCoord);
+   void assignCellAt(Coordinate aCoord, Cell *aCell);
+   Cell *getNeighborWithImage(char anImage);
+   Coordinate getEmptyNeighborCoord(void);
+   Coordinate getPreyNeighborCoord(void);
+   Cell *north(void);
+   Cell *south(void);
+   Cell *east(void);
+   Cell *west(void);
+   virtual Cell *reproduce(Coordinate anOffset);
+ 
+ public:
+   Cell(Coordinate &aCoord) {
+     offset = new Coordinate(aCoord);
+     image = DefaultImage;
+   }
+   Cell(void) {}
+   virtual ~Cell(void) {delete offset;}
+   Coordinate &getOffset(void) {return *offset;}
+   void setOffset(Coordinate &anOffset) {offset = new Coordinate(anOffset);}
+   char getImage(void) { return image;}
+   void display(void);
+   virtual void process(void) {}
+ };
+ 
+ #endif


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/constants.hh
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/constants.hh:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/constants.hh	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,21 ----
+ #ifndef Constants
+ #define Constants
+ 
+ #define MaxRows 25
+ #define MaxCols 70
+ 
+ #define DefaultNumObstacles 75
+ #define DefaultNumPredators 20
+ #define DefaultNumPrey 150
+ 
+ #define DefaultNumIterations 1000
+ 
+ #define DefaultImage '-'
+ #define DefaultPreyImage 'f'
+ #define DefaultPredImage 'S'
+ #define ObstacleImage '#'
+ 
+ #define TimeToFeed 6
+ #define TimeToReproduce 6
+ 
+ #endif


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/coordinate.hh
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/coordinate.hh:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/coordinate.hh	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,33 ----
+ #ifndef CoordinateDef
+ #define CoordinateDef
+ 
+ class Coordinate {
+ private:
+   unsigned x;
+   unsigned y;
+ 
+ public:
+   Coordinate(unsigned anX, unsigned aY) : x(anX), y(aY) {}
+   Coordinate(Coordinate &aCoord) {
+     x = aCoord.x;
+     y = aCoord.y;
+   }
+   Coordinate(void) {}
+   ~Coordinate(void) {}
+   unsigned getX(void) {return x;}
+   unsigned getY(void) {return y;}
+   void setX(unsigned anX) {x = anX;}
+   void setY(unsigned aY) {y = aY;}
+   void operator = (Coordinate &aCoord) {
+     x = aCoord.x;
+     y = aCoord.y;
+   }
+   int operator == (Coordinate &c) {
+     return (x == c.x && y == c.y);
+   }
+   int operator != (Coordinate &c) {
+     return (x != c.x && y != c.y);
+   }
+ };
+ 
+ #endif


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/obstacle.hh
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/obstacle.hh:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/obstacle.hh	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,12 ----
+ #ifndef ObstacleDef
+ #define ObstacleDef
+ 
+ class Obstacle : public Cell {
+ public:
+   Obstacle(Coordinate &aCoord) : Cell(aCoord) {
+     image = ObstacleImage;
+   }
+   virtual ~Obstacle(void) {Cell::~Cell();}
+ };
+ 
+ #endif


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/ocean.cpp
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/ocean.cpp:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/ocean.cpp	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,236 ----
+ #include "constants.hh"
+ #include "random.hh"
+ #include "coordinate.hh"
+ #include "ocean.hh"
+ #include "cell.hh"
+ #include "prey.hh"
+ #include "predator.hh"
+ #include "obstacle.hh"
+ 
+ //cell.cc
+ Cell *Cell::getCellAt(Coordinate aCoord) {
+   return cells[aCoord.getY()][aCoord.getX()];
+ }
+ 
+ void Cell::assignCellAt(Coordinate aCoord, Cell *aCell) {
+   cells[aCoord.getY()][aCoord.getX()] = aCell;
+ }
+ 
+ Cell *Cell::getNeighborWithImage(char anImage) {
+   Cell *neighbors[4];
+   unsigned count = 0;
+   if (north()->getImage() == anImage) neighbors[count++] = north();
+   if (south()->getImage() == anImage) neighbors[count++] = south();
+   if (east()->getImage() == anImage) neighbors[count++] = east();
+   if (west()->getImage() == anImage) neighbors[count++] = west();
+   if (!count) return this;
+   else
+     return neighbors[Ocean1->random->nextIntBetween(0,count-1)];
+ }
+ 
+ Coordinate Cell::getEmptyNeighborCoord(void) {
+   return getNeighborWithImage(DefaultImage)->getOffset();
+ }
+ 
+ Coordinate Cell::getPreyNeighborCoord(void) {
+   return getNeighborWithImage(DefaultPreyImage)->getOffset();
+ }
+ 
+ Cell *Cell::north(void) {
+   unsigned yvalue;
+   yvalue = (offset->getY() > 0) ? (offset->getY()-1) : (Ocean1->numRows-1);
+   return cells[yvalue][offset->getX()];
+ }
+ 
+ Cell *Cell::south(void) {
+   unsigned yvalue;
+   yvalue = (offset->getY()+1) % Ocean1->numRows;
+   return cells[yvalue][offset->getX()];
+ }
+ 
+ Cell *Cell::east(void) {
+   unsigned xvalue;
+   xvalue = (offset->getX()+1) % Ocean1->numCols;
+   return cells[offset->getY()][xvalue];
+ }
+ 
+ Cell *Cell::west(void) {
+   unsigned xvalue;
+   xvalue = (offset->getX() > 0) ? (offset->getX()-1) : (Ocean1->numCols-1);
+   return cells[offset->getY()][xvalue];
+ }
+ 
+ Cell *Cell::reproduce(Coordinate anOffset) {
+   Cell *temp = new Cell(anOffset);
+   return temp;
+ }
+ 
+ void Cell::display(void) {}
+ 
+ //prey.cc
+ void Prey::moveFrom(Coordinate from, Coordinate to) {
+   Cell *toCell;
+   --timeToReproduce;
+   if (to != from) {
+     toCell = getCellAt(to);
+     delete toCell;
+     setOffset(to);
+     assignCellAt(to, this);
+     if (timeToReproduce <= 0) {
+       timeToReproduce = TimeToReproduce;
+       assignCellAt(from, reproduce(from));
+     }
+     else
+       assignCellAt(from, new Cell(from));
+   }
+ }
+ 
+ Cell *Prey::reproduce(Coordinate anOffset) {
+   Prey *temp = new Prey(anOffset);
+   Ocean1->setNumPrey(Ocean1->getNumPrey()+1);
+   return (Cell *) temp;
+ }
+ 
+ //predator.cc
+ void Predator::process(void) {
+   Coordinate toCoord;
+   if (--timeToFeed <= 0) {
+     assignCellAt(*offset, new Cell(*offset));
+     Ocean1->setNumPredators(Ocean1->getNumPredators()-1);
+     delete this;
+   }
+   else {
+     toCoord = getPreyNeighborCoord();
+     if (toCoord != *offset) {
+       Ocean1->setNumPrey(Ocean1->getNumPrey()-1);
+       timeToFeed = TimeToFeed;
+       moveFrom(*offset, toCoord);
+     }
+     else
+       Prey::process();
+   }
+ }
+ 
+ Cell *Predator::reproduce(Coordinate anOffset) {
+   Predator *temp = new Predator(anOffset);
+   Ocean1->setNumPredators(Ocean1->getNumPredators()+1);
+   return (Cell *) temp;
+ }
+ 
+ //random.cc
+ #define MAX 32767
+ 
+ float Random::randReal(void) {
+   return 0.0;
+ }
+ 
+ unsigned Random::nextIntBetween(int low, int high) {
+   return 0;
+ }
+ 
+ //ocean.cc
+ void Ocean::initialize(void) {
+   random = new Random();
+   random->initialize();
+   numRows = MaxRows;
+   numCols = MaxCols;
+ 
+   numObstacles = DefaultNumObstacles;
+   numPredators = DefaultNumPredators;
+   numPrey = DefaultNumPrey;
+ 
+   initCells();
+ }
+ 
+ void Ocean::initCells(void) {
+   addEmptyCells();
+   if (numPredators == (size - numObstacles))
+     numPredators = size - numObstacles;
+   if (numPrey == (size - numObstacles - numPredators))
+     numPrey = size - numObstacles - numPredators;
+   addObstacles();
+   addPredators();
+   addPrey();
+   displayStats(-1);
+   displayBorder();
+   Ocean1 = this;
+ }
+ 
+ void Ocean::addEmptyCells(void) {
+   for (unsigned row = 0; row < numRows; row++)
+     for (unsigned col = 0; col < numCols; col++)
+       cells[row][col] = new Cell(Coordinate(col,row));
+ }
+ 
+ void Ocean::addObstacles(void) {
+   Coordinate empty;
+   for (unsigned count = 0; count < numObstacles; count++) {
+     empty = getEmptyCellCoord();
+     cells[empty.getY()][empty.getX()] = new Obstacle(empty);
+   }
+ }
+ 
+ void Ocean::addPredators(void) {
+   Coordinate empty;
+   for (unsigned count = 0; count < numPredators; count++) {
+     empty = getEmptyCellCoord();
+     cells[empty.getY()][empty.getX()] = new Predator(empty);
+   }
+ }
+ 
+ void Ocean::addPrey(void) {
+   Coordinate empty;
+   for (unsigned count = 0; count < numPredators; count++) {
+     empty = getEmptyCellCoord();
+     cells[empty.getY()][empty.getX()] = new Prey(empty);
+   }
+ }
+ 
+ Coordinate Ocean::getEmptyCellCoord(void) {
+   unsigned x, y;
+   Coordinate empty;
+   do {
+     x = random->nextIntBetween(0,numCols-1);
+     y = random->nextIntBetween(0,numRows-1);
+   }
+   while (cells[y][x]->getImage() != DefaultImage);
+   empty = cells[y][x]->getOffset();
+   delete cells[y][x];
+   return empty;
+ }
+ 
+ void Ocean::displayBorder(void) {
+   for (unsigned col = 0; col < numCols; col++);
+ }
+ 
+ void Ocean::displayCells(void) {
+   for (unsigned row = 0; row < numRows; row++)
+     for (unsigned col = 0; col < numCols; col++)
+       cells[row][col]->display();
+ }
+ 
+ void Ocean::displayStats(int iteration) {
+   // lots of cout stuff
+   displayBorder();
+ }
+ 
+ void Ocean::run(void) {
+   unsigned numIterations = DefaultNumIterations; // instead of cin
+   if (numIterations > 1000) numIterations = DefaultNumIterations;
+   for (unsigned iteration = 0; iteration < numIterations; iteration++) {
+     if (numPredators > 0 && numPrey > 0) {
+       for (unsigned row = 0; row < numRows; row++)
+ 	for (unsigned col = 0; col < numCols; col++)
+ 	  cells[row][col]->process();
+       displayStats(iteration);
+       displayCells();
+       displayBorder();
+     }
+   }
+ }
+ 
+ main() {
+   Ocean *myOcean = new Ocean;
+   myOcean->initialize();
+   myOcean->run();
+ }


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/ocean.hh
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/ocean.hh:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/ocean.hh	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,41 ----
+ #ifndef OceanDef
+ #define OceanDef
+ 
+ class Cell;
+ Cell *cells[MaxRows][MaxCols];
+ 
+ class Ocean {
+ friend class Cell;
+ private:
+   unsigned numRows;
+   unsigned numCols;
+   unsigned size;
+   unsigned numPrey;
+   unsigned numPredators;
+   unsigned numObstacles;
+   Random *random;
+ 
+   void initCells(void);
+   void addEmptyCells(void);
+   void addObstacles(void);
+   void addPredators(void);
+   void addPrey(void);
+   Coordinate getEmptyCellCoord(void);
+   void displayBorder(void);
+   void displayCells(void);
+   void displayStats(int iteration);
+ 
+ public:
+   unsigned getNumPrey(void) {return numPrey;}
+   unsigned getNumPredators(void) {return numPredators;}
+   void setNumPrey(unsigned aNumber) {numPrey = aNumber;}
+   void setNumPredators(unsigned aNumber) {numPredators = aNumber;}
+   void initialize(void);
+   void run(void);
+   Ocean(void) {}
+   ~Ocean(void) {delete random;}
+ };
+ 
+ Ocean *Ocean1;
+ 
+ #endif


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/predator.hh
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/predator.hh:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/predator.hh	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,20 ----
+ #ifndef PredatorDef
+ #define PredatorDef
+ 
+ class Predator : public Prey {
+ private:
+   virtual Cell *reproduce(Coordinate anOffset);
+ 
+ protected:
+   unsigned timeToFeed;
+ 
+ public:
+   Predator(Coordinate aCoord) : Prey(aCoord) {
+     timeToFeed = TimeToFeed;
+     image = DefaultPredImage;
+   }
+   virtual ~Predator(void) {Prey::~Prey();}
+   virtual void process(void);
+ };
+ 
+ #endif


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/prey.hh
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/prey.hh:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/prey.hh	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,25 ----
+ #ifndef PreyDef
+ #define PreyDef
+ 
+ class Prey : public Cell {
+ protected:
+   int timeToReproduce;
+ 
+   void moveFrom(Coordinate from, Coordinate to);
+   Cell *reproduce(Coordinate anOffset);
+ 
+ public:
+   Prey(Coordinate &aCoord) : Cell(aCoord) {
+     timeToReproduce = TimeToReproduce;
+     image = DefaultPreyImage;
+   }
+   ~Prey(void) {Cell::~Cell();}
+   void process(void) {
+     Coordinate toCoord;
+ 
+     toCoord = getEmptyNeighborCoord();
+     moveFrom(*offset,toCoord);
+   }
+ };
+ 
+ #endif


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/random.hh
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/random.hh:1.1
*** /dev/null	Mon Oct  4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/ocean/random.hh	Mon Oct  4 15:01:14 2004
***************
*** 0 ****
--- 1,20 ----
+ #ifndef RandomDef
+ #define RandomDef
+ 
+ class Random {
+ private:
+   int seed1, seed2;
+ public:
+   void initialize(void) {
+     seed1 = 3797;
+     seed2 = 2117;
+   }
+   void init(int s1, int s2) {
+     seed1 = s1;
+     seed2 = s2;
+   }
+   float randReal(void);
+   unsigned nextIntBetween(int low, int high);
+ };
+ 
+ #endif






More information about the llvm-commits mailing list