Program needs to be written in cpp not java. C++ program help. filling in codes
ID: 3793340 • Letter: P
Question
Program needs to be written in cpp not java.
C++ program help. filling in codes for two functions.
//header file to finish code
#ifndef PERSISTENT_VECTOR_H
#define PERSISTENT_VECTOR_H
#include
#include
#include
#include
template
class persistent_vector {
private:
class node {
public:
virtual ~node() {}
virtual void print_details() = 0;
};
class internal_node : public node {
public:
private:
};
class leaf_node : public node {
public:
private:
};
public:
//constructor
persistent_vector(const std::vector& v) {
}
//copy constructor
persistent_vector(const persistent_vector& other) {
}
//copy assignment operator
const persistent_vector& operator=(const persistent_vector& other) {
return *this;
}
const T& operator[](size_t index) const {
return ;
}
void print_details() const {
std::cout << "persistent_vector of " << m_size << " elements with height " << m_height << std::endl;
if(m_size) {
m_head->print_details();
}
}
private:
std::shared_ptr m_head;
size_t m_size;
size_t m_height;
};
#endif // defined PERSISTENT_VECTOR_H
//end of driver file
Begin the implementation of a binary C++ persistent vector like that described here: http://hypirion.com/musings/understanding-persistent-vector-pt-1 and http://hypirion.com/musings/understanding-persistent-vector-pt-2.
1) a constructor that takes a constant reference to an std::vector and results in a new persistent vector with the same contents. You should copy the contents of the vector into the leaves of the trie.
2) The trie should be composed of std::shared_pointers so that when the persistent vector is destructed, the elements will be automatically destructed (so long as no other copies have been made)
3) A copy constructor that takes a constant reference to another persistent_vector and produces a copy that shares the other vector's trie.
4) An assignment operator that takes a constant reference to another persistent_vector and produces a copy that shares the other vector's trie.
5) a bracket operator that returns a constant reference to the indexed element.
Your submission should be a single C++ header file that provides the implementation of the persistent vector. Use the attached header file as a starting point.
//code of test-1.cpp
#include
#include
#include "persistent_vector.h"
int main() {
std::vector v { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, };
persistent_vector pv(v);
pv.print_details();
std::cout << "element at index 3 is " << pv[3] << std::endl;
return 0;
}
//end of test-1.cpp
When finished, running test-1.cpp should yield something like:
persistent_vector of 11 elements with height 3
node 0x7ff331d004a0 : 0x7ff331d00400 0x7ff331d00430
node 0x7ff331d00400 : 0x7ff331d002d0 0x7ff331d00330
node 0x7ff331d002d0 : 0x7ff331d00030 0x7ff331d00090
leaf 0x7ff331d00030 : 0 1
leaf 0x7ff331d00090 : 2 3
node 0x7ff331d00330 : 0x7ff331d00100 0x7ff331d000e0
leaf 0x7ff331d00100 : 4 5
leaf 0x7ff331d000e0 : 6 7
node 0x7ff331d00430 : 0x7ff331d00140 0x0
node 0x7ff331d00140 : 0x7ff331d001b0 0x7ff331d00280
leaf 0x7ff331d001b0 : 8 9
leaf 0x7ff331d00280 : 10
element at index 3 is 3
Where the hexadecimal numbers can be any unique identifier so long as it is possible to reconstruct the tree from the statements. For my implementation, I just printed the value of the pointer to each node as the unique identifier.
P.S.
I've attached a viewer that consumes output that looks like the above and generates a picture of the tree. It requires that you have java and graphviz (http://www.graphviz.org) installed. I have only tested it against Java 1.8, but I don't know that it is required.
P.P.S
Here's how to use it
java -jar vector-viewer.jar
Explanation / Answer
#ifndef PERSISTENT_VECTOR_H
#define PERSISTENT_VECTOR_H
#include
#include
#include
#include
template
class persistent_vector {
private:
class node {
public:
virtual ~node() {}
virtual void print_details() = 0;
};
class internal_node : public node {
public:
private:
};
class leaf_node : public node {
public:
private:
};
public:
//constructor
persistent_vector(const std::vector& v) {
}
//copy constructor
persistent_vector(const persistent_vector& other) {
}
//copy assignment operator
const persistent_vector& operator=(const persistent_vector& other) {
return *this;
}
const T& operator[](size_t index) const {
return ;
}
void print_details() const {
std::cout << "persistent_vector of " << m_size << " elements with height " << m_height << std::endl;
if(m_size) {
m_head->print_details();
}
}
private:
std::shared_ptr m_head;
size_t m_size;
size_t m_height;
};
std::vector v { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, };
persistent_vector pv(v);
pv.print_details();
std::cout << "element at index 3 is " << pv[3] << std::endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.