Skip to content

Commit

Permalink
Merge pull request #3 from gstonge/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Guillaume St-Onge committed Dec 12, 2018
2 parents e8656a7 + c5d578a commit e1254b1
Show file tree
Hide file tree
Showing 18 changed files with 3,763 additions and 273 deletions.
4 changes: 2 additions & 2 deletions examples.ipynb

Large diffs are not rendered by default.

126 changes: 54 additions & 72 deletions src/BinaryTree.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
/**
* \file BinaryTree.cpp
* \brief Methods for the class BinaryTree
* \author Guillaume St-Onge
* \version 1.0
* \date 03/02/2018
*/

#include <BinaryTree.hpp>
/*
* MIT License
*
* Copyright (c) 2018 Guillaume St-Onge
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "BinaryTree.hpp"
#include <iostream>
#include <set>

Expand All @@ -16,24 +32,17 @@ namespace net
{//start of namespace net


/*---------------------------
* Constructor
*---------------------------*/

/**
* \brief Default constructor of the class BinaryTree
*/
BinaryTree::BinaryTree() :
current_node_(nullptr), root_(nullptr), leaves_vector_(), leaves_index_map_()
//Default constructor of the class BinaryTree
BinaryTree::BinaryTree() :
current_node_(nullptr), root_(nullptr), leaves_vector_(),
leaves_index_map_()
{
}

/**
* \brief Constructor of the class BinaryTree
* \param[in] N number of leaves
*/
BinaryTree::BinaryTree(unsigned int n_leaves) :
current_node_(nullptr), root_(nullptr), leaves_vector_(), leaves_index_map_()
//Constructor of the class BinaryTree with specified leaves number
BinaryTree::BinaryTree(unsigned int n_leaves) :
current_node_(nullptr), root_(nullptr), leaves_vector_(),
leaves_index_map_()
{
if (n_leaves < 1)
{
Expand All @@ -44,46 +53,39 @@ BinaryTree::BinaryTree(unsigned int n_leaves) :
BinaryTreeNode* root = new BinaryTreeNode;
root_ = root;
current_node_ = root; //assign current node to the root
unsigned int n_nodes = 2*n_leaves - 1; //number of nodes for
unsigned int n_nodes = 2*n_leaves - 1; //number of nodes for
//a full binary tree
root->child_left = branch(root, 1, n_nodes);
root->child_right = branch(root, 2, n_nodes);
}
}

/**
* \brief Copy constructor of the class BinaryTree
* \param[in] tree Binary tree to copy its behavior
*/
//Copy constructor of the class BinaryTree
BinaryTree::BinaryTree(const BinaryTree& tree) :
current_node_(nullptr), root_(nullptr), leaves_vector_(), leaves_index_map_()
current_node_(nullptr), root_(nullptr), leaves_vector_(),
leaves_index_map_()
{
//Construct a new tree with n_leaves
unsigned int n_leaves = tree.leaves_vector_.size();
BinaryTreeNode* root = new BinaryTreeNode;
root_ = root;
current_node_ = root; //assign current node to the root
unsigned int n_nodes = 2*n_leaves - 1; //number of nodes for
unsigned int n_nodes = 2*n_leaves - 1; //number of nodes for
//a full binary tree
root->child_left = branch(root, 1, n_nodes);
root->child_right = branch(root, 2, n_nodes);

//Give the same values to the leaves
LeafIndex leaf_index = 0;
for (auto iter = tree.leaves_vector_.begin();
iter < tree.leaves_vector_.end(); ++iter)
for (auto iter = tree.leaves_vector_.begin();
iter != tree.leaves_vector_.end(); ++iter)
{
update_value(leaf_index, (*iter)->value);
leaf_index += 1;
}
}

/*---------------------------
* Assignement
*---------------------------*/
/**
* \brief Overload the assignement operator
*/

//Overload the assignement operator
BinaryTree& BinaryTree::operator=(const BinaryTree& tree)
{
//destroy the current tree
Expand All @@ -94,40 +96,30 @@ BinaryTree& BinaryTree::operator=(const BinaryTree& tree)
BinaryTreeNode* root = new BinaryTreeNode;
root_ = root;
current_node_ = root; //assign current node to the root
unsigned int n_nodes = 2*n_leaves - 1; //number of nodes for
unsigned int n_nodes = 2*n_leaves - 1; //number of nodes for
//a full binary tree
root->child_left = branch(root, 1, n_nodes);
root->child_right = branch(root, 2, n_nodes);

//Give the same values to the leaves
LeafIndex leaf_index = 0;
for (auto iter = tree.leaves_vector_.begin();
iter < tree.leaves_vector_.end(); ++iter)
for (auto iter = tree.leaves_vector_.begin();
iter != tree.leaves_vector_.end(); ++iter)
{
update_value(leaf_index, (*iter)->value);
leaf_index += 1;
}

return *this;
}

/*---------------------------
* Destructor
*---------------------------*/
/**
* \brief Destructor of the class
*/
//Destructor of the class
BinaryTree::~BinaryTree()
{
destroy_tree(root_);
}

/*---------------------------
* Accessors
*---------------------------*/
/**
* \brief Get the leaf index associated to the cumulative fraction r
* \param[in] r real value in [0,1)
*/
//Get the leaf index associated to the cumulative fraction r
LeafIndex BinaryTree::get_leaf_index(double r)
{
double cumul = 0;
Expand All @@ -149,17 +141,8 @@ LeafIndex BinaryTree::get_leaf_index(double r)
return chosen_leaf;
}

/*---------------------------
* Mutators
*---------------------------*/

/**
* \brief Recursive method to construct a full binary tree
* \param[in] parent parent node
* \param[in] node_index index associated to a new node created
* \param[in] n_nodes total number of nodes in the full tree
*/
BinaryTreeNode* BinaryTree::branch(BinaryTreeNode* parent, int node_index,
//Recursive method to construct a full binary tree
BinaryTreeNode* BinaryTree::branch(BinaryTreeNode* parent, int node_index,
int n_nodes)
{

Expand All @@ -186,10 +169,7 @@ BinaryTreeNode* BinaryTree::branch(BinaryTreeNode* parent, int node_index,
return node;
}

/**
* \brief Recursively destroy the subtree associated to the node
* \param[in] node Node of the tree
*/
//Recursively destroy the subtree associated to the node
void BinaryTree::destroy_tree(BinaryTreeNode* node)
{
if(node != nullptr)
Expand All @@ -200,6 +180,7 @@ void BinaryTree::destroy_tree(BinaryTreeNode* node)
}
}

//update value for the leaf and parents
void BinaryTree::update_value(LeafIndex leaf_index, double variation)
{
current_node_ = leaves_vector_[leaf_index];
Expand All @@ -211,6 +192,7 @@ void BinaryTree::update_value(LeafIndex leaf_index, double variation)
}
}

//update value for current leaf
void BinaryTree::update_value(double variation)
{
if (is_leaf())
Expand All @@ -228,4 +210,4 @@ void BinaryTree::update_value(double variation)
}
}

}//end of namespace net
}//end of namespace net
42 changes: 27 additions & 15 deletions src/BinaryTree.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
/**
* \file BinaryTree.hpp
* \brief Header file for class BinaryTree
* \author Guillaume St-Onge
* \version 1.0
* \date 27/01/2018
*/
/*
* MIT License
*
* Copyright (c) 2018 Guillaume St-Onge
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef BINARYTREE_HPP
#define BINARYTREE_HPP
Expand All @@ -30,11 +46,7 @@ struct BinaryTreeNode
BinaryTreeNode* child_right;
};

/**
* \class BinaryTree BinaryTree.hpp
* \brief Binary tree implementation where each node possess a value equal
* to the sum of the value of its children.
*/
//Binary tree to udpate dynamically a cumulative distribution
class BinaryTree
{
public:
Expand All @@ -53,7 +65,7 @@ class BinaryTree
bool is_root()
{return current_node_->parent == nullptr;}
bool is_leaf()
{return current_node_->child_left == nullptr and
{return current_node_->child_left == nullptr and
current_node_->child_right == nullptr ;}
double get_value() const
{return current_node_->value;}
Expand Down Expand Up @@ -86,7 +98,7 @@ class BinaryTree
std::unordered_map<BinaryTreeNode*,LeafIndex> leaves_index_map_;

//To be called by constructors and assignement operator
BinaryTreeNode* branch(BinaryTreeNode* parent, int node_index,
BinaryTreeNode* branch(BinaryTreeNode* parent, int node_index,
int n_nodes);

//to be called by destructor
Expand All @@ -97,4 +109,4 @@ class BinaryTree

}//end of namespace net

#endif /* BINARYTREE_HPP */
#endif /* BINARYTREE_HPP */
19 changes: 10 additions & 9 deletions src/HashPropensity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ namespace net
* Constructor
*---------------------------*/

HashPropensity::HashPropensity(double propensity_min, double propensity_max) :
propensity_min_(propensity_min), propensity_max_(propensity_max),
power_of_two_(false)
HashPropensity::HashPropensity(double propensity_min, double propensity_max,
double base) :
propensity_min_(propensity_min), propensity_max_(propensity_max),
power_of_base_(false), base_(base)
{
if (floor(log2(propensity_max/propensity_min)) ==
ceil(log2(propensity_max/propensity_min)) and
if (floor(log2(propensity_max/propensity_min)/log2(base_)) ==
ceil(log2(propensity_max/propensity_min)/log2(base_)) and
propensity_max != propensity_min)
{
power_of_two_ = true;
power_of_base_ = true;
}
}

Expand All @@ -38,8 +39,8 @@ HashPropensity::HashPropensity(double propensity_min, double propensity_max) :

size_t HashPropensity::operator()(double propensity) const
{
size_t index = floor(log2(propensity/propensity_min_));
if (power_of_two_ and propensity == propensity_max_)
size_t index = floor(log2(propensity/propensity_min_)/log2(base_));
if (power_of_base_ and propensity == propensity_max_)
{
index -=1 ;
}
Expand All @@ -50,4 +51,4 @@ size_t HashPropensity::operator()(double propensity) const



}//end of namespace net
}//end of namespace net
7 changes: 4 additions & 3 deletions src/HashPropensity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HashPropensity
{
public:
//Constructor
HashPropensity(double propensity_min, double propensity_max);
HashPropensity(double propensity_min, double propensity_max, double base_ = 2);

//Call operator definition
std::size_t operator()(double propensity) const;
Expand All @@ -33,9 +33,10 @@ class HashPropensity
//Members
double propensity_min_;
double propensity_max_;
bool power_of_two_;
double base_;
bool power_of_base_;
};

}//end of namespace net

#endif /* HASHPROPENSITY_HPP_ */
#endif /* HASHPROPENSITY_HPP_ */
Loading

0 comments on commit e1254b1

Please sign in to comment.