Skip to content
Snippets Groups Projects
Commit dac25339 authored by Marcus M. Darden's avatar Marcus M. Darden
Browse files

Initial commit.

parents
No related branches found
No related tags found
No related merge requests found
#ifndef BINARY_H
#define BINARY_H
#include "Eecs281PQ.h"
//A specialized version of the 'priority_queue' ADT implemented as a binary priority_queue.
template<typename TYPE, typename COMP_FUNCTOR = std::less<TYPE>>
class BinaryPQ : public Eecs281PQ<TYPE, COMP_FUNCTOR> {
public:
typedef unsigned size_type;
//Description: Construct a priority_queue out of an iterator range with an optional
// comparison functor.
//Runtime: O(n) where n is number of elements in range.
template<typename InputIterator>
BinaryPQ(InputIterator start, InputIterator end, COMP_FUNCTOR comp = COMP_FUNCTOR());
//Description: Construct an empty priority_queue with an optional comparison functor.
//Runtime: O(1)
BinaryPQ(COMP_FUNCTOR comp = COMP_FUNCTOR());
//Description: Add a new element to the priority_queue.
//Runtime: O(log(n))
virtual void push(const TYPE& val);
//Description: Remove the most extreme (defined by 'compare') element from
// the priority_queue.
//Note: We will not run tests on your code that would require it to pop an
//element when the priority_queue is empty. Though you are welcome to if you are
//familiar with them, you do not need to use exceptions in this project.
//Runtime: O(log(n))
virtual void pop();
//Description: Return the most extreme (defined by 'compare') element of
// the priority_queue.
//Runtime: O(1)
virtual const TYPE& top() const;
//Description: Get the number of elements in the priority_queue.
//Runtime: O(1)
virtual size_type size() const {
//Fill this in - might be very simple depending on implementation
return 0;
}
//Description: Return true if the priority_queue is empty.
//Runtime: O(1)
virtual bool empty() const {
// Fill this in - might be very simple depending on implementation
return true;
}
private:
//Note: This vector *must* be used your priority_queue implementation.
std::vector<TYPE> data;
private:
//***Add any additional member functions or data you require here.
};
template<typename TYPE, typename COMP_FUNCTOR>
template<typename InputIterator>
BinaryPQ<TYPE, COMP_FUNCTOR>::BinaryPQ(
InputIterator,
InputIterator,
COMP_FUNCTOR
) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
BinaryPQ<TYPE, COMP_FUNCTOR>::BinaryPQ(COMP_FUNCTOR) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
void BinaryPQ<TYPE, COMP_FUNCTOR>::push(const TYPE&) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
void BinaryPQ<TYPE, COMP_FUNCTOR>::pop() {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
const TYPE& BinaryPQ<TYPE, COMP_FUNCTOR>::top() const {
//Your code.
//These lines present only so that this provided file compiles.
static TYPE removeTheseLines;
return removeTheseLines;
}
#endif //BINARY_H
#ifndef EECS281_PQ_H
#define EECS281_PQ_H
#include <functional>
#include <iterator>
#include <vector>
//A simple interface that implements a generic priority_queue.
//Runtime specifications assume constant time comparison and copying.
template<typename TYPE, typename COMP_FUNCTOR = std::less<TYPE>>
class Eecs281PQ {
public:
typedef unsigned size_type;
virtual ~Eecs281PQ() {}
//Description: Add a new element to the priority_queue.
virtual void push(const TYPE& val) = 0;
//Description: Remove the most extreme (defined by 'compare') element from
// the priority_queue.
//Note: We will not run tests on your code that would require it to pop an
//element when the priority_queue is empty. Though you are welcome to if you are
//familiar with them, you do not need to use exceptions in this project.
virtual void pop() = 0;
//Description: Return the most extreme (defined by 'compare') element of
// the priority_queue.
virtual const TYPE& top() const = 0;
//Description: Get the number of elements in the priority_queue.
virtual size_type size() const = 0;
//Description: Return true if the priority_queue is empty.
virtual bool empty() const = 0;
protected:
//Note: These data members *must* be used in all of your priority_queue
// implementations.
//You can use this in the derived classes with:
//this->compare(Thing1, Thing2)
//With the default compare function (std::less), this will
//tell you if Thing1 is less than Thing2.
COMP_FUNCTOR compare;
};
#endif
#ifndef JARJAR_PQ_H
#define JARJAR_PQ_H
#include "Eecs281PQ.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <vector>
//A bad priority queue that linearly searches for the maximum element every
//time it is requested. You do not need to implement this; it is already done
//for you.
template<typename TYPE, typename COMP_FUNCTOR = std::less<TYPE>>
class JarJarPQ : public Eecs281PQ<TYPE, COMP_FUNCTOR> {
public:
typedef unsigned size_type;
template<typename InputIterator>
JarJarPQ(InputIterator start, InputIterator end, COMP_FUNCTOR comp = COMP_FUNCTOR())
: data(start, end)
{
this->compare = comp;
}
JarJarPQ(COMP_FUNCTOR comp = COMP_FUNCTOR()) {
this->compare = comp;
}
virtual void push(const TYPE& val) {
data.push_back(val);
}
virtual void pop() {
assert(!data.empty());
typename std::vector<TYPE>::iterator it = std::max_element(data.begin(), data.end(), this->compare);
std::iter_swap(it, std::prev(data.end()));
data.pop_back();
}
virtual const TYPE& top() const {
return *std::max_element(data.begin(), data.end(), this->compare);
}
virtual size_type size() const {
return data.size();
}
virtual bool empty() const {
return data.empty();
}
private:
std::vector<TYPE> data;
};
#endif
Makefile 0 → 100644
## EECS 281 Advanced Makefile
# How to use this Makefile...
###################
###################
## ##
## $ make help ##
## ##
###################
###################
# IMPORTANT NOTES:
# 1. Set EXECUTABLE to the command name given in the project specification.
# 2. To enable automatic creation of unit test rules, your program logic
# (where main() is) should be in a file named project*.cpp or specified
# in the PROJECTFILE variable.
# 3. Files you want to include in your final submission cannot match the
# test*.cpp pattern.
# Version 4 - 2015-05-03, Marcus M. Darden (mmdarden@umich.edu)
# * Updated build rules for tests
# Version 3.0.1 - 2015-01-22, Waleed Khan (wkhan@umich.edu)
# * Added '$(EXECUTABLE): $(OBJECTS)' target. Now you can compile with
# 'make executable', and re-linking isn't done unnecessarily.
# Version 3 - 2015-01-16, Marcus M. Darden (mmdarden@umich.edu)
# * Add help rule and message
# * All customization locations are cleary marked.
# Version 2 - 2014-11-02, Marcus M. Darden (mmdarden@umich.edu)
# * Move customization section to the bottom of the file
# * Add support for submit without test cases, to prevent submission
# deduction while testing, when code fails to compile
# usage: make partialsubmit <- includes no test case files
# make fullsubmit <- includes all test case files
# * Add automatic creation of test targets for test driver files
# usage: (add cpp files to the project folder with a test prefix)
# make alltests <- builds all test*.cpp
# make test_insert <- builds testinsert from test_insert.cpp
# make test2 <- builds testinsert from test2.cpp
# * Add documentation and changelog
# Version 1 - 2014-09-21, David Snider (sniderdj@umich.edu)
# Vertion 0 - ????-??-??, Matt Diffenderfer (mjdiffy@umich.edu)
# enables c++14 on CAEN
PATH := /usr/um/gcc-5.1.0/bin:$(PATH)
LD_LIBRARY_PATH := /usr/um/gcc-5.1.0/lib64
LD_RUN_PATH := /usr/um/gcc-5.1.0/lib64
# Change EXECUTABLE to match the command name given in the project spec.
EXECUTABLE = galaxy
# designate which compiler to use
CXX = g++
# list of test drivers (with main()) for development
TESTSOURCES = $(wildcard test*.cpp)
# names of test executables
TESTS = $(TESTSOURCES:%.cpp=%)
# list of sources used in project
SOURCES = $(wildcard *.cpp)
SOURCES := $(filter-out $(TESTSOURCES), $(SOURCES))
# list of objects used in project
OBJECTS = $(SOURCES:%.cpp=%.o)
# TODO
# If main() is in a file named project*.cpp, use the following line
PROJECTFILE = $(wildcard project*.cpp)
# TODO
# If main() is in another file delete the line above, edit and uncomment below
#PROJECTFILE = mymainfile.cpp
# name of the tar ball created for submission
PARTIAL_SUBMITFILE = partialsubmit.tar.gz
FULL_SUBMITFILE = fullsubmit.tar.gz
#Default Flags
CXXFLAGS = -std=c++14 -Wall -Werror -Wextra -pedantic
# make release - will compile "all" with $(CXXFLAGS) and the -O3 flag
# also defines NDEBUG so that asserts will not check
release: CXXFLAGS += -O3 -DNDEBUG
release: all
# make debug - will compile "all" with $(CXXFLAGS) and the -g flag
# also defines DEBUG so that "#ifdef DEBUG /*...*/ #endif" works
debug: CXXFLAGS += -g3 -DDEBUG
debug: clean all
# make profile - will compile "all" with $(CXXFLAGS) and the -pg flag
profile: CXXFLAGS += -pg
profile: clean all
# highest target; sews together all objects into executable
all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
ifeq ($(EXECUTABLE), executable)
@echo Edit EXECUTABLE variable in Makefile.
@echo Using default a.out.
$(CXX) $(CXXFLAGS) $(OBJECTS)
else
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(EXECUTABLE)
endif
# Automatically generate any build rules for test*.cpp files
define make_tests
SRCS = $$(filter-out $$(PROJECTFILE), $$(SOURCES))
OBJS = $$(SRCS:%.cpp=%.o)
HDRS = $$(wildcard *.h)
$(1): CXXFLAGS += -g3 -DDEBUG
$(1): $$(OBJS) $$(HDRS) $(1).cpp
ifeq ($$(PROJECTFILE),)
@echo Edit PROJECTFILE variable to .cpp file with main\(\)
@exit 1
endif
$$(CXX) $$(CXXFLAGS) $$(OBJS) $(1).cpp -o $(1)
endef
$(foreach test, $(TESTS), $(eval $(call make_tests, $(test))))
alltests: clean $(TESTS)
# rule for creating objects
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $*.cpp
# make clean - remove .o files, executables, tarball
clean:
rm -f $(OBJECTS) $(EXECUTABLE) $(TESTS) $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE)
# make partialsubmit.tar.gz - cleans, runs dos2unix, creates tarball omitting test cases
PARTIAL_SUBMITFILES=$(filter-out $(TESTSOURCES), $(wildcard Makefile *.h *.cpp))
$(PARTIAL_SUBMITFILE): $(PARTIAL_SUBMITFILES)
rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE)
-dos2unix $(PARTIAL_SUBMITFILES)
tar -vczf $(PARTIAL_SUBMITFILE) $(PARTIAL_SUBMITFILES)
@echo !!! WARNING: No test cases included. Use 'make fullsubmit' to include test cases. !!!
# make fullsubmit.tar.gz - cleans, runs dos2unix, creates tarball including test cases
FULL_SUBMITFILES=$(filter-out $(TESTSOURCES), $(wildcard Makefile *.h *.cpp test*.txt))
$(FULL_SUBMITFILE): $(FULL_SUBMITFILES)
rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE)
-dos2unix $(FULL_SUBMITFILES)
tar -vczf $(FULL_SUBMITFILE) $(FULL_SUBMITFILES)
@echo !!! Final submission prepared, test cases included... READY FOR GRADING !!!
# shortcut for make submit tarballs
partialsubmit: $(PARTIAL_SUBMITFILE)
fullsubmit: $(FULL_SUBMITFILE)
define MAKEFILE_HELP
EECS281 Advanced Makefile Help
* This Makefile uses advanced techniques, for more information:
$$ man make
* General usage
1. Follow directions at each "TODO" in this file.
a. Set EXECUTABLE equal to the name given in the project specification.
b. Set PROJECTFILE equal to the name of the source file with main()
c. Add any dependency rules specific to your files.
2. Build, test, submit... repeat as necessary.
* Preparing submissions
A) To build 'partialsubmit.tar.gz', a tarball without tests used to find
buggy solutions in the autograder. This is useful for faster autograder
runs during development and free submissions if the project does not
build.
$$ make partialsubmit
B) Build 'fullsubmit.tar.gz' a tarball complete with autograder test cases.
ALWAYS USE THIS FOR FINAL GRADING! It is also useful when trying to
find buggy solutions in the autograder.
$$ make fullsubmit
* Unit testing support
A) Source files for unit testing should be named test*.cpp. Examples
include test_input.cpp or test3.cpp.
B) Automatic build rules are generated to support the following:
$$ make test_input
$$ make test3
$$ make alltests (this builds all test drivers)
C) If test drivers need special dependencies, they must be added manually.
D) IMPORTANT: NO SOURCE FILES THAT BEGIN WITH test WILL BE ADDED TO ANY
SUBMISSION TARBALLS.
endef
export MAKEFILE_HELP
help:
@echo "$$MAKEFILE_HELP"
#######################
# TODO (begin) #
#######################
# individual dependencies for objects
# Examples:
# "Add a header file dependency"
# project2.o: project2.cpp project2.h
#
# "Add multiple headers and a separate class"
# HEADERS = some.h special.h header.h files.h
# myclass.o: myclass.cpp myclass.h $(HEADERS)
# project5.o: project5.cpp myclass.o $(HEADERS)
#
# ADD YOUR OWN DEPENDENCIES HERE
# tests
class.o: class.cpp class.h
project0.o: project0.cpp class.h
######################
# TODO (end) #
######################
# these targets do not create any files
.PHONY: all release debug profile clean alltests partialsubmit fullsubmit help
# disable built-in rules
.SUFFIXES:
P2.cpp 0 → 100644
#include "P2.h"
void P2::PR_init(std::stringstream& ss,
int seed,
int num_generals,
int num_planets,
int num_deployments,
int arrival_rate)
{
P2::MersenneTwister mt;
mt.init_genrand(seed);
int max_force_sensitivity = 100;
int max_quantity = 50;
long double timestamp = 0;
for(int i = 0; i < num_deployments; ++i) {
unsigned long time_increase = (mt.genrand_int32() % arrival_rate + 1);
timestamp += (1.0l / time_increase);
unsigned long planet_num = mt.genrand_int32() % num_planets;
unsigned long general_num = mt.genrand_int32() % num_generals;
std::string force_side = mt.genrand_int32() % 2 ? "SITH" : "JEDI";
int quantity = mt.genrand_int32() % max_quantity + 1;
int force_sensitivity = mt.genrand_int32() % max_force_sensitivity + 1;
ss << int(timestamp)
<< " " << force_side
<< " G" << general_num
<< " P" << planet_num
<< " F" << force_sensitivity
<< " #" << quantity
<< "\n";
}
}
/**
* C++ Mersenne Twister wrapper class written by
* Jason R. Blevins <jrblevin@sdf.lonestar.org> on July 24, 2006.
* Based on the original MT19937 C code by
* Takuji Nishimura and Makoto Matsumoto.
*/
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any feedback is very welcome.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
*/
/**
* Constructor
*/
P2::MersenneTwister::MersenneTwister(void):
mt_(new unsigned long[N]), mti_(N+1),
init_key_(NULL), key_length_(0), s_(0),
seeded_by_array_(false), seeded_by_int_(false)
{
init_genrand(0);
}
/**
* Destructor
*/
P2::MersenneTwister::~MersenneTwister(void)
{
assert(mt_ != NULL);
delete[] mt_;
mt_ = NULL;
}
/**
* Initializes the Mersenne Twister with a seed.
*
* \param s seed
*/
void P2::MersenneTwister::init_genrand(unsigned long s)
{
mt_[0]= s & 0xffffffffUL;
for (mti_=1; mti_<N; mti_++) {
mt_[mti_] =
(1812433253UL * (mt_[mti_-1] ^ (mt_[mti_-1] >> 30)) + mti_);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt_[]. */
/* 2002/01/09 modified by Makoto Matsumoto */
mt_[mti_] &= 0xffffffffUL;
/* for >32 bit machines */
}
// Store the seed
s_ = s;
seeded_by_array_ = false;
seeded_by_int_ = true;
}
/**
* Generates a random number on [0,0xffffffff]-interval
*
* \return random number on [0, 0xffffffff]
*/
unsigned long P2::MersenneTwister::genrand_int32(void)
{
unsigned long y;
static unsigned long mag01[2]={0x0UL, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti_ >= N) { /* generate N words at one time */
int kk;
if (mti_ == N+1) /* if init_genrand() has not been called, */
init_genrand(5489UL); /* a default initial seed is used */
for (kk=0;kk<N-M;kk++) {
y = (mt_[kk]&UPPER_MASK)|(mt_[kk+1]&LOWER_MASK);
mt_[kk] = mt_[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
for (;kk<N-1;kk++) {
y = (mt_[kk]&UPPER_MASK)|(mt_[kk+1]&LOWER_MASK);
mt_[kk] = mt_[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
y = (mt_[N-1]&UPPER_MASK)|(mt_[0]&LOWER_MASK);
mt_[N-1] = mt_[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
mti_ = 0;
}
y = mt_[mti_++];
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return y;
}
P2.h 0 → 100644
#ifndef P2_H
#define P2_H
#include <cassert>
#include <sstream>
#include <string>
#include <vector>
class P2 {
public:
static void PR_init(std::stringstream& ss,
int seed,
int num_generals,
int num_planets,
int num_deployments,
int arrival_rate);
// Don't need to read further than this, unless you want to learn about
// Mersenne Twister implementation
private:
/**
* mt.h: Mersenne Twister header file
*
* Jason R. Blevins <jrblevin@sdf.lonestar.org>
* Durham, March 7, 2007
*/
/**
* Mersenne Twister.
*
* M. Matsumoto and T. Nishimura, "Mersenne Twister: A
* 623-dimensionally equidistributed uniform pseudorandom number
* generator", ACM Trans. on Modeling and Computer Simulation Vol. 8,
* No. 1, January pp.3-30 (1998).
*
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html.
*/
class MersenneTwister
{
public:
MersenneTwister(void);
~MersenneTwister(void);
void print(void);
void init_genrand(unsigned long s);
unsigned long genrand_int32(void);
private:
static const int N = 624;
static const int M = 397;
// constant vector a
static const unsigned long MATRIX_A = 0x9908b0dfUL;
// most significant w-r bits
static const unsigned long UPPER_MASK = 0x80000000UL;
// least significant r bits
static const unsigned long LOWER_MASK = 0x7fffffffUL;
unsigned long* mt_; // the state vector
int mti_; // mti == N+1 means mt not initialized
unsigned long* init_key_; // Storage for the seed vector
int key_length_; // Seed vector length
unsigned long s_; // Seed integer
bool seeded_by_array_; // Seeded by an array
bool seeded_by_int_; // Seeded by an integer
};
};
#endif
#ifndef PAIRING_PQ_H
#define PAIRING_PQ_H
#include "Eecs281PQ.h"
//A specialized version of the 'priority_queue' ADT implemented as a pairing priority_queue.
template<typename TYPE, typename COMP_FUNCTOR = std::less<TYPE>>
class PairingPQ : public Eecs281PQ<TYPE, COMP_FUNCTOR> {
public:
typedef unsigned size_type;
//Description: Construct a priority_queue out of an iterator range with an optional
// comparison functor.
//Runtime: O(n) where n is number of elements in range.
template<typename InputIterator>
PairingPQ(InputIterator start, InputIterator end, COMP_FUNCTOR comp = COMP_FUNCTOR());
//Description: Construct an empty priority_queue with an optional comparison functor.
//Runtime: O(1)
PairingPQ(COMP_FUNCTOR comp = COMP_FUNCTOR());
//Description: Copy constructor.
//Runtime: O(n)
PairingPQ(const PairingPQ& other);
//Description: Copy assignment operator.
//Runtime: O(n)
PairingPQ& operator=(const PairingPQ& rhs);
//Description: Destructor
//Runtime: O(n)
~PairingPQ();
//Description: Add a new element to the priority_queue.
//Runtime: Amortized O(1)
virtual void push(const TYPE& val);
//Description: Remove the most extreme (defined by 'compare') element from
// the priority_queue.
//Note: We will not run tests on your code that would require it to pop an
//element when the priority_queue is empty. Though you are welcome to if you are
//familiar with them, you do not need to use exceptions in this project.
//Runtime: Amortized O(log(n))
virtual void pop();
//Description: Return the most extreme (defined by 'compare') element of
// the priority_queue.
//Runtime: O(1)
virtual const TYPE& top() const;
//Description: Get the number of elements in the priority_queue.
//Runtime: O(1)
virtual size_type size() const {
//Fill this in
return 0;
}
//Description: Return true if the priority_queue is empty.
//Runtime: O(1)
virtual bool empty() const {
//Fill this in
return true;
}
class Node {
public:
Node(const TYPE & val)
: elt(val), sibling(nullptr), child(nullptr)
{}
public:
//Description: Allows access to the element at that Node's position.
//Runtime: O(1) - this has been provided for you.
const TYPE& operator*() const { return elt; }
const Node* sibling_ptr() const { return sibling; }
const Node* child_ptr() const { return child; }
//The following line allows you to access any private data members of this
//Node class from within the pairing_priority_queue class. (ie: myNode.elt is a legal
//statement in PairingPQ's add_node() function).
friend PairingPQ;
private:
TYPE elt;
Node* sibling;
Node* child;
};
const Node* root_ptr() const { return root; }
private:
Node* root;
//***Add any additional member functions or data you require here.
//***We recommend creating a 'meld' function (see the spec).
};
template<typename TYPE, typename COMP_FUNCTOR>
template<typename InputIterator>
PairingPQ<TYPE, COMP_FUNCTOR>::PairingPQ(
InputIterator,
InputIterator,
COMP_FUNCTOR
) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
PairingPQ<TYPE, COMP_FUNCTOR>::PairingPQ(COMP_FUNCTOR) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
PairingPQ<TYPE, COMP_FUNCTOR>::PairingPQ(const PairingPQ&) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
PairingPQ<TYPE, COMP_FUNCTOR>&
PairingPQ<TYPE, COMP_FUNCTOR>::operator=(const PairingPQ&) {
//Your code.
return *this;
}
template<typename TYPE, typename COMP_FUNCTOR>
PairingPQ<TYPE, COMP_FUNCTOR>::~PairingPQ() {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
void PairingPQ<TYPE, COMP_FUNCTOR>::pop() {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
const TYPE& PairingPQ<TYPE, COMP_FUNCTOR>::top() const {
//Your code.
//These lines present only so that this provided file compiles.
static TYPE removeTheseLines;
return removeTheseLines;
}
template<typename TYPE, typename COMP_FUNCTOR>
void PairingPQ<TYPE, COMP_FUNCTOR>::push(const TYPE&) {
//Your code.
}
#endif //PAIRING_H
File added
File added
File added
#ifndef SORTED_PQ_H
#define SORTED_PQ_H
#include "Eecs281PQ.h"
//A specialized version of the 'priority_queue' ADT that is implemented with an
//underlying sorted array-based container.
//Note: The most extreme element should be found at the end of the
//'data' container, such that traversing the iterators yields the elements in
//sorted order.
template<typename TYPE, typename COMP_FUNCTOR = std::less<TYPE>>
class SortedPQ : public Eecs281PQ<TYPE, COMP_FUNCTOR> {
public:
typedef unsigned size_type;
//Description: Construct a priority_queue out of an iterator range with an optional
// comparison functor.
//Runtime: O(n log n) where n is number of elements in range.
template<typename InputIterator>
SortedPQ(InputIterator start, InputIterator end, COMP_FUNCTOR comp = COMP_FUNCTOR());
//Description: Construct an empty priority_queue with an optional comparison functor.
//Runtime: O(1)
SortedPQ(COMP_FUNCTOR comp = COMP_FUNCTOR());
//Description: Add a new element to the priority_queue.
//Runtime: O(n)
virtual void push(const TYPE& val);
//Description: Remove the most extreme (defined by 'compare') element from
// the priority_queue.
//Note: We will not run tests on your code that would require it to pop an
//element when the priority_queue is empty. Though you are welcome to if you are
//familiar with them, you do not need to use exceptions in this project.
//Runtime: Amortized O(1)
virtual void pop();
//Description: Return the most extreme (defined by 'compare') element of
// the priority_queue.
//Runtime: O(1)
virtual const TYPE& top() const;
//Description: Get the number of elements in the priority_queue. This has been
// implemented for you.
//Runtime: O(1)
virtual size_type size() const { return data.size(); }
//Description: Return true if the priority_queue is empty. This has been implemented
// for you.
//Runtime: O(1)
virtual bool empty() const { return data.empty(); }
private:
//Note: This vector *must* be used by your priority_queue implementation.
std::vector<TYPE> data;
private:
//***Add any additional member functions or data you require here.
};
template<typename TYPE, typename COMP_FUNCTOR>
template<typename InputIterator>
SortedPQ<TYPE, COMP_FUNCTOR>::SortedPQ(
InputIterator,
InputIterator,
COMP_FUNCTOR
) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
SortedPQ<TYPE, COMP_FUNCTOR>::SortedPQ(COMP_FUNCTOR) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
void SortedPQ<TYPE, COMP_FUNCTOR>::push(const TYPE&) {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
void SortedPQ<TYPE, COMP_FUNCTOR>::pop() {
//Your code.
}
template<typename TYPE, typename COMP_FUNCTOR>
const TYPE& SortedPQ<TYPE, COMP_FUNCTOR>::top() const {
//Your code.
//These lines present only so that this provided file compiles.
static TYPE removeTheseLines;
return removeTheseLines;
}
#endif //SORTED_H
/*
* Compile this test against your .h files to make sure they compile. Note how
* the eecs281 priority queues can be constructed with the different types. We
* suggest adding to this file or creating your own test cases to test your
* priority queue implementations.
*
* These tests are /not/ a complete test of your priority queues!
*/
#include "BinaryPQ.h"
#include "Eecs281PQ.h"
#include "JarJarPQ.h"
#include "PairingPQ.h"
#include "SortedPQ.h"
#include <cassert>
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
void testPriorityQueue(Eecs281PQ<int>& pq, const string& pqType);
void testPairing(vector<int>&);
int main()
{
JarJarPQ<int> jarJarPq;
SortedPQ<int> sortedPq;
BinaryPQ<int> binaryPq;
// Jar-Jar should pass already.
testPriorityQueue(jarJarPq, "Jar-Jar");
testPriorityQueue(sortedPq, "Sorted");
testPriorityQueue(binaryPq, "Binary");
vector<int> vec;
vec.push_back(0);
vec.push_back(1);
testPairing(vec);
}
void testPriorityQueue(Eecs281PQ<int>& pq, const string& pqType)
{
cout << "Testing priority queue: " << pqType << endl;
pq.push(3);
pq.push(4);
assert(pq.size() == 2);
assert(pq.top() == 4);
pq.pop();
assert(pq.size() == 1);
assert(pq.top() == 3);
assert(!pq.empty());
pq.pop();
assert(pq.size() == 0);
assert(pq.empty());
}
void testPairing(vector<int> & vec)
{
Eecs281PQ<int> * pq1 = new PairingPQ<int>(vec.begin(), vec.end());
Eecs281PQ<int> * pq2 = new PairingPQ<int>(*((PairingPQ<int> *)pq1));
// This line is different just to show two different ways to declare a
// pairing heap: as an Eecs281PQ and as a PairingPQ. Yay for inheritance!
PairingPQ<int> * pq3 = new PairingPQ<int>();
*pq3 = *((PairingPQ<int> *)pq2);
pq1->push(3);
pq2->pop();
pq1->size();
pq1->empty();
assert(pq1->top() == 3);
delete pq1;
delete pq2;
delete pq3;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment