Disjoint Set ADT Implementation in C++ - c++

I have problem in implementing a disjoint set ADT in C++ due to the fact that our teacher only explained the union and find operations. I fully understand the concepts of union and find but I am still confused about how to implement them.
Could someone please give me an idea of the implementation and also explain what the interface of this data structure should look like?

You have way too many requirements, we're not here to do your homework for you.
Have a look at http://en.wikipedia.org/wiki/Disjoint-set_data_structure

#include <iostream>
template<typename T>
class Disjoint_sets
{
public:
int FIND(int pos);
bool in_same_set(T data_element_1, T data_element_2);
void UNION_IF_EQUIVALENT(T data_element_1, T data_element_2);
void UNION(T data_element_1, T data_element_2);
Disjoint_sets(bool (*is_equivalent)(T, T));
Disjoint_sets();
Disjoint_sets(T* data_arr, bool (*is_equivalent)(T, T),int size);
void insert(T data_element);
bool is_root(int pos_number);
int get_pos(T data_element);
void partition();
void print_partition();
private:
T* data;
int* parent_pos;
int* number_of_children;
int size;
bool (*isequivalent)(T D1, T D2);
};
template<typename T>
Disjoint_sets<T>::Disjoint_sets()
{
data = NULL;
parent_pos = NULL;
number_of_children = NULL;
size = 0;
isequivalent = NULL;
}
template<typename T>
Disjoint_sets<T>::Disjoint_sets(bool (*is_equivalent)(T, T))
{
isequivalent = is_equivalent;
data = NULL;
parent_pos = NULL;
number_of_children = NULL;
size = 0;
}
template<typename T>
Disjoint_sets<T>::Disjoint_sets(T* data_arr, bool (*is_equivalent)(T, T), int size)
{
data = new T[size];
parent_pos = new int[size];
number_of_children = new int[size];
this->size = size;
isequivalent = is_equivalent;
for (int i = 0; i < size; i++)
{
data[i] = data_arr[i];
parent_pos[i] = -1;
number_of_children[i] = 0;
}
}
template<typename T>
bool Disjoint_sets<T>::is_root(int pos)
{
if (pos<0 && pos>size - 1)
{
std::cout << "Error, invalid pos supplied to is_root\n";
return false;
}
if (parent_pos[pos] == -1)
{
return true;
}
else
{
return false;
}
}
template <typename T>
int Disjoint_sets<T>::FIND(int pos)
{
while (!is_root(pos))
{
pos = parent_pos[pos];
}
return pos;
}
template<typename T>
bool Disjoint_sets<T>::in_same_set(T data_element_1, T data_element_2)
{
return FIND(get_pos(data_element_1)) == FIND(get_pos(data_element_2));
}
template<typename T>
int Disjoint_sets<T>::get_pos(T data_element)
{
for (int i = 0; i < size; i++)
{
if (data[i] == data_element)
{
return i;
}
}
std::cout << "Could not find element\n";
return -1;
}
template <typename T>
void Disjoint_sets<T>::UNION(T data_element_1, T data_element_2)
{
int data_parent_1_pos = FIND(get_pos(data_element_1));
int data_parent_2_pos = FIND(get_pos(data_element_2));
if ( data_parent_1_pos==data_parent_2_pos )
{
return;
}
if (number_of_children[data_parent_1_pos] >= number_of_children[data_parent_2_pos])
{
parent_pos[data_parent_2_pos] = data_parent_1_pos;
}
else
{
parent_pos[data_parent_1_pos] = data_parent_2_pos;
}
}
template <typename T>
void Disjoint_sets<T>::UNION_IF_EQUIVALENT(T data_element_1, T data_element_2)
{
if (FIND(get_pos(data_element_1)) == FIND(get_pos(data_element_2)))
{
return;
}
if (isequivalent(data_element_1, data_element_2))
{
UNION(data_element_1, data_element_2);
}
}
template<typename T>
void Disjoint_sets<T>::partition()
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
UNION_IF_EQUIVALENT(data[i], data[j]);
}
}
}
template <typename T>
void Disjoint_sets<T>::print_partition()
{
for (int i = 0; i < size; i++)
{
if (is_root(i))
{
for (int j = 0; j < size; j++)
{
if (FIND(j) == i)
{
std::cout << data[j] << " ";
}
}
}
std::cout << "\n";
}
}
template <typename T>
bool lol(int a, int b)
{
return a * a == b * b;
}
int main()
{
int arr[6] = { -1,1,2,3,-3,4 };
Disjoint_sets<int> d(arr,lol<int>, 6);
d.partition();
d.print_partition();
}

Related

Editor with dynamically allocated matrix in template

I've got two classes (structs) - Editor and Matrix. The matrix itself is working fine. But if I want to use Matrix via Editor, it's not working. I believe there is some problem with memory because I can't deallocate and even use matrix inside the Editor struct. I've tried to allocate the matrix dynamically, but then I couldn't use the addRow method.
Not really good with c++, but have to use those classes, can't use std::vector and stuff like that.
Editor definition:
struct Editor
{
private:
Matrix<std::string> data;
public:
Editor();
Editor(Matrix<std::string> oldData);
~Editor();
void addRow(std::string* row);
int getNumberOfRows();
int getNumberOfColumns();
};
Editor implementation:
using namespace std;
Editor::Editor()
{
}
Editor::Editor(Matrix<string> oldData)
{
data = oldData;
}
Editor::~Editor()
{
}
void Editor::addRow(std::string* row)
{
data.addRow(row);
}
int Editor::getNumberOfRows()
{
return this->data.getNumberOfRows();
}
int Editor::getNumberOfColumns()
{
return this->data.getNumberOfColumns();
}
Matrix definition:
template<typename T>
struct Matrix
{
private:
T** data;
int numberOfRows;
int numberOfColumns;
public:
Matrix(int numberOfRows, int numberOfColumns);
Matrix(const Matrix<T>& m);
~Matrix();
int getNumberOfRows();
int getNumberOfColumns();
T getItem(int indexRow, int indexColumn) const;
void addRow(T* newRow);
T** getData();
};
Matrix implementation:
template<typename T>
inline Matrix<T>::Matrix(int numberOfRows, int numberOfColumns)
{
this->numberOfColumns = numberOfColumns;
this->numberOfRows = numberOfRows;
this->data = new T * [numberOfRows];
for (int i = 0; i < this->numberOfRows; i++)
{
this->data[i] = new T[numberOfColumns];
}
}
template<typename T>
inline Matrix<T>::Matrix(const Matrix<T>& m)
{
numberOfRows = m.numberOfRows;
numberOfColumns = m.numberOfColumns;
if (m.data)
{
data = new T * [numberOfRows];
for (int i = 0; i < numberOfRows; i++)
data[i] = new T[numberOfColumns];
for (int i = 0; i < numberOfRows; i++)
{
for (int j = 0; j < numberOfColumns; j++)
{
data[i][j] = m.data[i][j];
}
}
}
}
template<typename T>
inline Matrix<T>::~Matrix()
{
for (int i = 0; i < this->numberOfRows; i++)
{
delete[] data[i];
}
delete[] data;
}
template<typename T>
inline int Matrix<T>::getNumberOfRows()
{
return this->numberOfRows;
}
template<typename T>
inline int Matrix<T>::getNumberOfColumns()
{
return this->numberOfColumns;
}
template<typename T>
inline T** Matrix<T>::getData()
{
return this->data;
}
template<typename T>
inline T Matrix<T>::getItem(int indexRow, int indexColumn) const
{
if (indexRow < 0 || indexRow > numberOfRows || indexColumn < 0 || indexColumn > numberOfColumns)
{
throw std::exception("not valid at least one of those indexes");
}
return this->data[indexRow][indexColumn];
}
template<typename T>
inline void Matrix<T>::addRow(T* newRow)
{
if (newRow == nullptr)
{
throw std::exception("not valid row");
}
T** newData = new T * [numberOfRows + 1];
for (int i = 0; i < numberOfRows + 1; i++)
{
newData[i] = new T[numberOfColumns];
for (int j = 0; j < numberOfColumns; j++)
{
if (i != this->numberOfRows)
{
newData[i][j] = data[i][j];
}
else {
newData[i][j] = newRow[j];
}
}
}
for (int i = 0; i < this->numberOfRows; i++)
{
delete[] this->data[i];
}
delete[] this->data;
this->data = newData;
this->numberOfRows++;
}
Main:
using namespace std;
int main()
{
Matrix<string> mt{1,1};
string* st = new string[3];
st[0] = "aa";
st[1] = "bb";
st[2] = "cc";
mt.addRow(st); // works
Editor ed{ mt };
ed.addRow(st); // doesn't work
return 0;
}
It crashes in xstring* on this:
_CONSTEXPR20_CONTAINER void _Copy_assign(const basic_string& _Right, false_type) {
_Pocca(_Getal(), _Right._Getal());
assign(_Right._Mypair._Myval2._Myptr(), _Right._Mypair._Myval2._Mysize);
}
with code:
Exception: Read access violation.
_Right – 0xDDDDDDDD.
EDIT:
Did cut most of the stuff from the project to just show the minimum. Anyway, the answer with semi-shallow copy was the right. Altho I thought
struct = struct;
calls for copy constructor. But it seems that it doesn't. I had to add operator= to somehow make it work properly.

Refactoring Ideas for generic objects buffer in c++

I am trying to come up with a generic buffer (or heap container - not sure how to call it) for generic objects. So I could have a large number of them in contiguous memory.
The header I did works, but I was wondering if you guys could help me understand what am I doing wrong if anything, or any gotchas/bugs I am missing here. Or any other re-factoring I could do to make it better.
buffer.h
#ifndef _ENGINE_BUFFER
#define _ENGINE_BUFFER
#include <cstdlib>
#include <cstdio>
#include <typeinfo>
#include <cstring>
// Defining buffer struct:
template <typename T>
struct Buffer {
unsigned int _size;
unsigned int _number_of_objects;
unsigned int _head;
unsigned int _next;
unsigned int * _backlog;
int _backlog_head;
T* _buffer_address;
};
// Creating buffer:
template <typename T>
Buffer<T>* createBuffer(int size) {
Buffer<T>* _buffer = (Buffer<Sprite>*) calloc(1, sizeof(Buffer<T>));
if (_buffer) {
*(_buffer) = Buffer<T>();
_buffer->_size = size;
_buffer->_number_of_objects = 0;
_buffer->_head = 0;
_buffer->_next = 0;
_buffer->_backlog = (unsigned int*) calloc(size, sizeof(int));
_buffer->_backlog_head = -1;
_buffer->_buffer_address = (T*) calloc(size, sizeof(T));
if(_buffer->_buffer_address) {
return _buffer;
}
}
return (Buffer<T>*) nullptr;
}
// Adding object to buffer:
template <typename T>
int addObjectToBuffer(Buffer<T>* _buffer, const T &_obj) {
int _head = 0;
if (_buffer->_backlog_head > -1) {
_buffer->_buffer_address[_buffer->_backlog[_buffer->_backlog_head]] = _obj;
_head = _buffer->_backlog_head;
_buffer->_backlog_head--;
_buffer->_number_of_objects++;
} else {
if ( (int) (_buffer->_number_of_objects + 1) >= _buffer->_size){
T* _new_address = (T*) calloc((_buffer->_number_of_objects + 1) * 2, sizeof(T));
memcpy(_new_address, _buffer->_buffer_address, _buffer->_number_of_objects * sizeof(T));
free(_buffer->_buffer_address);
_buffer->_buffer_address = _new_address;
_buffer->_size = (_buffer->_number_of_objects + 1) * 2;
for (int i = _buffer->_head; i < _buffer->_size; i++){
_buffer->_buffer_address[i] = 0;
}
}
_buffer->_buffer_address[_buffer->_head] = _obj;
_buffer->_number_of_objects++;
_head = _buffer->_head;
_buffer->_head++;
}
return _head;
}
// Getting number of objects in buffer:
template <typename T>
unsigned int bufferGetSize(Buffer<T>* _buffer) {
return (int) _buffer->_number_of_objects;
}
// Getting next object in buffer:
template <typename T>
T* bufferGetNext(Buffer<T>* _buffer) {
if (_buffer->_backlog_head > -1){
for (int i = 0; i <= _buffer->_backlog_head; i++ ) {
if (_buffer->_backlog[i] == _buffer->_next) _buffer->_next++;;
}
}
unsigned int _next = _buffer->_next;
if (_next < _buffer->_head) {
_buffer->_next++;
return &_buffer->_buffer_address[_next];
} else {
_buffer->_next = 0;
bufferGetNext(_buffer);
}
}
// Reset iterator head:
template <typename T>
void bufferResetHead(Buffer<T>* _buffer){
_buffer->_next = 0;
}
// Deleting object from buffer:
template <typename T>
void deleteObjectFromBuffer(Buffer<T>* _buffer, unsigned int _obj_index) {
if (_obj_index >= 0 && _obj_index <= _buffer->_head) {
bool _obj_exists = false;
for (int i = _buffer->_backlog_head; i >= 0; i-- ) {
if (_buffer->_backlog[i] == _obj_index){
_obj_exists = true;
fprintf(stderr, "\n[ Warning: object_deleted_twice | buffer: %#010x | object: %#010x | index: \"%d\" ]\n",
_buffer, &_buffer->_buffer_address[_obj_index], _obj_index);
}
}
if (!_obj_exists) {
_buffer->_backlog_head++;
_buffer->_backlog[_buffer->_backlog_head] = _obj_index;
_buffer->_number_of_objects--;
}
} else {
fprintf(stderr, "\n[ Warning: index_out_of_range | buffer: %#010x | index: \"%d\" ]\n",
_buffer, _obj_index);
}
}
// Deleting buffer:
template <typename T>
void deleteBuffer(Buffer<T>* _buffer) {
free(_buffer->_buffer_address);
free(_buffer);
}
// Print buffer memory:
template <typename T>
void printBufferMemory(Buffer<T>* _buffer, unsigned int _number_of_columns, bool _print_address) {
int _column_count = 0;
printf("\n");
printf("[ Number of objects in buffer: %#02d | Actual buffer size: %#02d ]\n", _buffer->_number_of_objects, _buffer->_size);
for (int i = 1; i < 11 * _number_of_columns; i++) {
printf("=");
}
printf("\n");
for (int i = 0; i < _buffer->_size; i++) {
if (_column_count > _number_of_columns - 1) {
printf("\n");
_column_count = 0;
}
if (_print_address) {
printf("%#010x ", &_buffer->_buffer_address[i]);
} else {
printf("%#010d ", *(&_buffer->_buffer_address[i]));
}
_column_count++;
}
printf("\n");
for (int i = 1; i < 11 * _number_of_columns; i++) {
printf("=");
}
printf("\n");
}
#endif // _ENGINE_BUFFER
sprite.h
#ifndef _SPRITE
#define _SPRITE
struct Sprite {
int value = 0;
Sprite(int n){
value = n;
}
};
#endif // _SPRITE
main.cpp
#include <cstdio>
#include <cstdlib>
#include "sprite.h"
#include "buffer.h"
using namespace std;
int main()
{
int buffer_size = 512;
int object_number = 512;
Buffer<Sprite>* engine_buffer = createBuffer<Sprite>(buffer_size);
for (int i = 0; i < object_number; i++) {
addObjectToBuffer(engine_buffer, Sprite(i + 100));
}
for (int i = 0; i < bufferGetSize(engine_buffer); i++) {
printf("Value of Sprite %d is: %d\n", i + 1, bufferGetNext(engine_buffer)->value);
}
printBufferMemory(engine_buffer, 10, false);
deleteBuffer(engine_buffer);
return(0);
}

C++ Undefined reference to List<int>::GetCount() const [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 9 years ago.
I am writing a simple data structure library, while I met some problems.
I wrote three files. collections.h is the header file, collections.cpp is to implement the methods declared in header file, main.cpp is for test. But compile error occurs:
Undefined reference to List::GetCount() const;
Undefined reference to List::IsEmpty() const;
Undefined reference to List::Add(int);
...// And so on.
I provide my code below, where is the problem?
collections.h:
#ifndef COLLECTIONS_H
#define COLLECTIONS_H
#include <windows.h>
#include <stdexcept>
template<typename T>
class ISequenceList
{
protected:
ISequenceList() { }
public:
virtual int GetCount() const = 0;
virtual bool IsEmpty() const = 0;
virtual void Add(T item) = 0;
virtual void AddRange(const T *items, int length) = 0;
virtual T &ElementAt(int index);
virtual bool InsertAt(T item, int index);
virtual bool Remove(T item) = 0;
virtual bool RemoveAt(int index) = 0;
virtual bool Contains(T item) = 0;
};
template<typename T>
class List : public ISequenceList<T>
{
private:
int _count;
int _capacity;
T *_array;
void ExpandCapacity();
public:
List(int capacity = 100)
{
if (capacity <= 0)
std::__throw_invalid_argument("The capcity can't be 0 or below.");
this->_count = 0;
this->_capacity = capacity;
this->_array = (T*)malloc(_capacity* sizeof(T));
}
List(const List &other)
{
if (this == other)
return;
this->_count = other->_count;
this->_capacity = other->_capacity;
free(_array);
this->_array = other->_array;
}
List &operator=(const List &other)
{
this = other;
}
~List()
{
if (_array)
free(_array);
}
int GetCount() const;
bool IsEmpty() const;
T &ElementAt(int index);
void Add(T item);
void AddRange(const T *items, int length);
bool InsertAt(T item, int index);
bool Remove(T item);
bool RemoveAt(int index);
bool Contains(T item);
};
#endif
collections.cpp:
#include "collections.h"
template<typename T>
void List<T>::ExpandCapacity()
{
T *temp = this->_array;
this->_array = (T*)malloc((this->_capacity << 1) * sizeof(T));
memcpy(this->_array, temp, this->_capacity * sizeof(T));
this->_capacity = this->_capacity << 1;
free(temp);
}
template<typename T>
int List<T>::GetCount() const
{
return this->_count;
}
template<typename T>
bool List<T>::IsEmpty() const
{
return this->_count == 0;
}
template<typename T>
void List<T>::Add(T item)
{
this->_array[_count] = item;
_count++;
if (_count == _capacity)
this->ExpandCapacity();
}
template<typename T>
void List<T>::AddRange(const T *items, int length)
{
if (length <= 0)
std::__throw_invalid_argument("The length can't be 0 or below.");
if (!items)
std::__throw_invalid_argument("The items can't be null");
int totalLength = this->_count + length;
if (totalLength >= this->_capacity)
{
T *temp = this->_array;
this->_array = (T*)malloc((totalLength << 1) * sizeof(T));
memcpy(_array, temp, this->_capacity);
free(temp);
}
this->_array += this->_capacity;
memcpy(_array, items, length * sizeof(T));
this->_capacity = totalLength << 1;
this->_count += length;
}
template<typename T>
T &List<T>::ElementAt(int index)
{
if (index < 0 || index >= _count )
std::__throw_invalid_argument("The index is out of bound.");
return _array[index];
}
template<typename T>
bool List<T>::InsertAt(T item, int index)
{
if (index < 0 || index > _count)
return false;
if (index == _count)
{
this->Add(item);
return true;
}
for (int i = _count; i > index; i--)
{
_array[i] = _array[i - 1];
}
_array[index] = item;
_count++;
if (_count == _capacity)
this->ExpandCapacity();
return true;
}
template<typename T>
bool List<T>::Remove(T item)
{
for (int i = 0; i < _count; i++)
{
if (_array[i] == item)
{
for (int j = i; j < _count; j++)
{
_array[j] = _array[j + 1];
}
_count--;
return true;
}
}
return false;
}
template<typename T>
bool List<T>::RemoveAt(int index)
{
if (index < 0 || index >= _count)
return false;
for (int j = index; j < _count; j++)
{
_array[j] = _array[j + 1];
}
_count--;
return true;
}
template<typename T>
bool List<T>::Contains(T item)
{
for (int i = 0; i < _count; i++)
{
if (_array[i] == item)
return true;
}
return false;
}
main.cpp:
#include "collections.h"
#include <iostream>
int main()
{
List<int> *seqList = new List<int>();
seqList->Add(5);
int arr[100] = {0};
seqList->AddRange(arr, 50);
seqList->ElementAt(5) = 111;
seqList->InsertAt(100, 15);
seqList->Remove(50);
seqList->ElementAt(44) = 44;
seqList->RemoveAt(44);
if (seqList->Contains(111))
std::cout << "Yes" << std::endl;
for (int i = 0; i < seqList->GetCount(); i++)
{
std::cout << seqList->ElementAt(i) << "\t";
}
return 0;
}
I have defined all the methods in List, but why can't the complier recognize? Where is the problem? Thanks for anyone who help me..
Note: my ide is Code::Blocks
Implementation of the template functions must be in a header; it can't be in a separate source file. The compiler needs to see it at the point where the template is used and its arguments become known.

c++ using vector of employee pointers

I have created my vector class template and I have already done my employee classes of hourly and I salaried. I want to use the vector of employee pointers instead of the array of employee pointers, I'm trying to do that but when I run it just breaks while I have no error listed.
Also, I have used the at function like( payroll.at(i)->writeFile(out); ) to get access to an element, but I don't know what's wrong.
Any suggestion?
Thanks
here is my code:
myvector class template:
#include <iostream>
#include <string>
#include <cassert>
#include <algorithm>
const int CAPACITY = 4;
template <class T>
class MyVector {
public:
MyVector();
MyVector( int size);
MyVector( int size, const T & initial);
MyVector(const MyVector<T> & v);
~MyVector();
int capacity() const;
int size() const;
void push_back(const T & value);
//T & operator[](unsigned int index);
MyVector<T> & operator=(const MyVector<T> &);
void clear();
T at(int i);
friend ostream& operator<<(ostream &out, const MyVector<T>& );
private:
int applied;
int my_size;
int my_capacity;
T * buffer;
T * daArray;
};
template<class T>
MyVector<T>::MyVector()
{
my_capacity = 0;
my_size = 0;
buffer = 0;
applied = 0;
}
template<class T>
MyVector<T>::MyVector(const MyVector<T> & v)
{
my_size = v.my_size;
my_capacity = v.my_capacity;
buffer = new T[my_size];
for ( int i = 0; i < my_size; i++)
buffer[i] = v.buffer[i];
}
template<class T>
MyVector<T>::MyVector(int size)
{
my_capacity = size;
my_size = size;
buffer = new T[size];
}
template<class T>
MyVector<T>::MyVector( int size, const T & initial)
{
my_size = size;
my_capacity = size;
buffer = new T [size];
for (unsigned int i = 0; i < size; i++)
buffer[i] = initial;
//T();
}
template<class T>
MyVector<T> & MyVector<T>::operator = (const MyVector<T> & v)
{
delete[ ] buffer;
my_size = v.my_size;
my_capacity = v.my_capacity;
buffer = new T [my_size];
for (int i = 0; i < my_size; i++)
buffer[i] = v.buffer[i];
return *this;
}
template<class T>
void MyVector<T>::push_back(const T & i)
{
if (my_capacity == 0)
{
my_capacity = 1;
my_size = 1;
applied= 0;
buffer = new T[1];
buffer[0] = i;
}
else
{
if (applied+1 == my_capacity)
{
int newCapacity = my_capacity * CAPACITY;
daArray = new T[newCapacity];
for (int i = 0; i < my_size; i++)
{
daArray[i] = buffer[i];
}
my_capacity = newCapacity;
delete buffer;
my_size++;
applied++;
buffer[applied] = i;
}
else
{
if (my_size == applied + 1)
my_size++;
applied++;
buffer[applied] = i;
}
}
}
template<class T>
int MyVector<T>::size()const//
{
return my_size;
}
template<class T>
int MyVector<T>::capacity()const
{
return my_capacity;
}
template<class T>
MyVector<T>::~MyVector()
{
delete[ ] buffer;
}
template <class T>
void MyVector<T>::clear()
{
my_capacity = 0;
my_size = 0;
buffer = 0;
}
template <class T>
T MyVector<T>::at(int i)
{
if (i < 0 || i > my_size -1)
{
string error = "Index is undefined";
throw error;
}
return buffer[i];
}
template <class T>
ostream& operator<<(ostream &out, const MyVector<T>& v)
{
for (unsigned i = 0; i < v.size(); i++)
{
out << v[i] << " ";
}
return out;
}
main
int main() {
MyVector< employee*> payroll;
payroll.push_back(new Hourly ("H. Potter", "Privet Drive", "201-9090", 40, 12.00));
payroll.push_back(new Salaried ( "A. Dumbledore", "Hogewarts", "803-1230", 1200));
ofstream out;
out.open(file);
if (out.fail()) {
cout<<" could not open the file"<<endl;
system("PAUSE");
}
for (int i = 0; i < SIZE; i++) {
payroll.at(i)->writeFile(out);
}
out.close( );
}
You have a bug in your push_back method. You need something like this
if (applied+1 == my_capacity)
{
int newCapacity = my_capacity * CAPACITY;
daArray = new T[newCapacity];
for (int i = 0; i < my_size; i++)
{
daArray[i] = buffer[i];
}
my_capacity = newCapacity;
delete buffer;
buffer = daArray; // new line here
my_size++;
applied++;
buffer[applied] = i;
}
See where I've put the comment // new line here

How do I interface a merge_sort algorithm to Vector and a user defined dynamic_array? - to_do

I want to take this merge_sort I wrote and put it into a class. Because their is some coupling of the variables, particularly - left, right_begin, right...I want to make these class members. The variables that are local to each function I will leave as local function variables.
Also this function will be used for another class I have called dynamic_array which is a bit more efficient than vector. However I want to make it available to the STL vector as well.
How do I do this. Do I use inheritance? Do I make the class a composite class of my dynamic_array...If I do this it will not be available to vector though.
The Question:
How do I make my merge_sort class interface with the STL container Vector and to my own container dynamic_array.
This is for an interview preparation.
Thank you. Dynamic Array code below(remove old merge_sort cold)
Merge Sort Code - to be converted to a class
#include <iostream>
using namespace std;
const int size=8;
int scratch[size],array[size]={6,5,3,1,8,7,2,4};
void print_array(int arr[]){ for (int i = 0; i < size; i++) cout << arr[i] << endl;}
void flip_if_unordered(int &x, int &y)
{
if(array[x]>array[y])
{
int tmp = array[x];
array[x] = array[y];
array[y] = tmp;
}
}
void merge_algo(int &left, int &right_begin, int &right)
{
int iter,iter_left=left,iter_right=right_begin;
for(iter=left;iter<=right;++iter)
{
if( (iter_right>right) || ((iter_left < right_begin) && (array[iter_left]<=array[iter_right])))
{
scratch[iter]=array[iter_left];
++iter_left;
}
else
{
scratch[iter]=array[iter_right];
++iter_right;
}
}
for(iter=left;iter<=right;++iter){array[iter]=scratch[iter];}
}
void merge_recurse(int left,int right)
{
int left_end=(left+((right-left)/2));
int right_begin=left_end+1;
if(((left+1)==right)){flip_if_unordered(left,right);return;}
else if ((left==right)){return;}
else
{
merge_recurse(left,left_end);
merge_recurse(right_begin,right);
merge_algo(left,right_begin,right);
}
}
int main()
{
merge_recurse(0,(size-1));
print_array(array);
return 0;
}
Dynamic Array
#include "c_arclib.cpp"
using namespace std;
template <class T> class dynamic_array
{
private:
T* array;
T* scratch;
void merge_recurse(int left, int right)
{
if(right == left + 1)
{
return;
}
else
{
int i = 0;
int length = right-left;
int midpoint_distance = length/2;
int l = left, r = left + midpoint_distance;
merge_recurse(left, left + midpoint_distance);
merge_recurse(left + midpoint_distance, right);
for(i = 0; i < length; i++)
{
if((l < (left + midpoint_distance)) && (r == right || array[l] > array[r]))
{
scratch[i] = array[l];
l++;
}
else
{
scratch[i] = array[r];
r++;
}
}
for(i = left; i < right; i++)
{
array[i] = scratch[i - left];
}
}
}
void quick_recurse(int left, int right)
{
int l = left, r = right, tmp;
int pivot = array[(left + right) / 2];
while (l <= r)
{
while (array[l] < pivot)l++;
while (array[r] > pivot)r--;
if (l <= r)
{
tmp = array[l];
array[l] = array[r];
array[r] = tmp;
l++;
r--;
}
}
if (left < r)quick_recurse(left, r);
if (l < right)quick_recurse(l, right);
}
public:
int size;
void rorder();
void order();
void randorder();
dynamic_array(int sizein)
{
size=sizein;
array=new T[size]();
}
void print_operator(std::ostream &os = cout) const
{
for (int i = 0; i < size; i++) os << array[i] << endl;
}
int merge_sort()
{
scratch = new T[size]();
if(scratch != NULL)
{
merge_recurse(0, size);return 1;
}
else{return 0;}
}
void quick_sort()
{
quick_recurse(0,size);
}
};
template <class T> void dynamic_array<T>::randorder()
{
srand(time(NULL));
int *ap;
for(ap=array;ap!=array+size;++ap){*ap=rand();}
}
template <class T> void dynamic_array<T>::order()
{
int *ap,i=0;
for(ap=array;ap!=array+size;++ap)
{
*ap=i;
++i;
}
}
template <class T> void dynamic_array<T>::rorder()
{
int *ap,i=size;
for(ap=array;ap!=array+size;++ap)
{
*ap=i;
--i;
}
}
template<class T> ostream& operator<<(ostream& stream, dynamic_array<T> const& data)
{
data.print_operator(stream);
return stream;
}
int main()
{
dynamic_array<int> d1(10);
d1.order();
cout << d1;
/*
clock_t time_start=clock();
d1.merge_sort();
clock_t time_end=clock();
double result = (double)(time_end - time_start) / CLOCKS_PER_SEC;
cout << result;
cout << d1;
*/
}
Just make it a template. You need 2 template parameter, the type of the elements, and the type of the container.
Tipp: if you want to use the class "like a function" define the operator ()
template<class _Item, class _Container> MergeSort
{
...
operator () (_Container & C);
...
}
now you can use C[] and to access elemets for any class that defines operator [], and you can use the size() function with any class that has it. You only have to add them to your dynamic_array class, and you can use MergeSort<int, vector<int> > with vectors, and MergeSort<int, dynamic_array<int> > with dynamic arrays.