Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReversalTree #46

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dsalglib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(SOURCE_FILES
include/kmpsearch.h
include/linklist.h
include/matrix.h
include/memory/unique_ptr.h
include/mergesort.h
include/queue.h
include/quicksort.h
Expand All @@ -31,6 +32,6 @@ set(SOURCE_FILES
main.cpp
sample.h
tests.h
timepass.h include/hashmap.cpp include/hashmap.h)
include/hashmap.cpp include/hashmap.h)

add_executable(dsalglib ${SOURCE_FILES})
add_executable(dsalglib ${SOURCE_FILES})
1 change: 1 addition & 0 deletions dsalglib/dsalglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "include/search.h"
#include "include/sort.h"
#include "include/queue.h"
#include "include/reversaltree.h"
#include "include/tree.h"
#include "include/heap.h"
#include "include/graph.h"
Expand Down
51 changes: 48 additions & 3 deletions dsalglib/include/alginc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,49 @@
* contains fundamental functions required
*/


#define STR_DETAIL(x) #x
#define STR(x) STR_DETAIL(x)

#if defined(DEBUG)
#define EXPECTS(cond) if (!(cond))\
throw ("Precondition failure at " __FILE__ ":"\
STR(__LINE__));
#else
#define EXPECTS(cond)
#endif

namespace dsa
{

template <class T>
struct remove_reference {
using type = T;
};
template <class T>
struct remove_reference<T&> {
using type = T;
};
template <class T>
struct remove_reference<T&&> {
using type = T;
};

// Analogue of std::move
template <class T>
typename remove_reference<T>::type&&
rvalue_cast(T&& t) noexcept
{
return static_cast<typename remove_reference<T>::type&&>(t);
}


template<typename T>
inline void swapit(T &x, T &y)
{
T t = x;
x = y;
y = t;
T t = rvalue_cast(x);
x = rvalue_cast(y);
y = rvalue_cast(t);
}

template<typename T>
Expand All @@ -37,5 +70,17 @@ namespace dsa
return min;
}

// Analogue of std::forward.
template<class T>
T&& forward(typename remove_reference<T>::type& t) noexcept
{
return static_cast<T&&>(t);
}
template<class T>
T&& forward(typename remove_reference<T>::type&& t) noexcept
{
return static_cast<T&&>(t);
}

}
#endif //DSALGLIB_ALGINC_H
70 changes: 70 additions & 0 deletions dsalglib/include/memory/unique_ptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef DSALGLIB_UNIQUE_PTR_H
#define DSALGLIB_UNIQUE_PTR_H

#include "../alginc.h"

namespace dsa {

template <class T>
class unique_ptr {
public:
unique_ptr(T* ptr = nullptr) : ptr_(ptr) {}

~unique_ptr() {
delete ptr_;
}

unique_ptr(const unique_ptr&) = delete;

unique_ptr(unique_ptr&& rhs) : ptr_(rhs.ptr_) {
rhs.ptr_ = nullptr;
}

unique_ptr& operator=(const unique_ptr&) = delete;

unique_ptr& operator=(unique_ptr&& rhs) {
// calling delete on nullptr is not an error
delete ptr_;
ptr_ = rhs.ptr_;
rhs.ptr_ = nullptr;
return *this;
}

T* get() {
return ptr_;
}

const T* get() const {
return ptr_;
}

T& operator*() {
return *ptr_;
}
const T& operator*() const {
return *ptr_;
}

T* operator->() {
return ptr_;
}
const T* operator->() const {
return ptr_;
}

operator bool() const {
return ptr_ != nullptr;
}

private:
T* ptr_;
};

template <class T, class... Args>
unique_ptr<T> make_unique(Args... args) {
return unique_ptr<T>(new T(rvalue_cast(args)...));
}

} // namespace dsa

#endif // DSALGLIB_UNIQUE_PTR_H
Loading