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

Const Code Review #269

Merged
merged 1 commit into from
Mar 23, 2023
Merged
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
Empty file modified .clang-format
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ If you are interested, please contact us at zigrazor@gmail.com or contribute to
| :heavy_check_mark: | Add Benchmark for all algorithms | Oct 5, 2022 |
| :heavy_check_mark: | Code Optimization | Oct 5, 2022 |
| :heavy_check_mark: | Release 0.4.0 | Oct 7, 2022 |
| :memo: | "Const" Code Review [#155](https://github.com/ZigRazor/CXXGraph/issues/155) | TBD |
| :heavy_check_mark: | "Const" Code Review [#155](https://github.com/ZigRazor/CXXGraph/issues/155) | Mar 23, 2023 |
| :memo: | Release 0.5.0 | TBD |
| :memo: | Test on Partition Algorithm [#264](https://github.com/ZigRazor/CXXGraph/issues/264) | TBD |
| :grey_exclamation: | Test on Partition Algorithm [#264](https://github.com/ZigRazor/CXXGraph/issues/264) | Mar 21, 2023 |
| :heavy_check_mark: | Bug Resolution [#263](https://github.com/ZigRazor/CXXGraph/issues/263) | Mar 21, 2023 |
| :memo: | General Performance Optimization [#262](https://github.com/ZigRazor/CXXGraph/issues/262) [#265](https://github.com/ZigRazor/CXXGraph/issues/265) | TBD |
| :memo: | Reduction of Code Issue of Static Analysis | TBD |
Expand Down
Empty file modified include/CXXGraph.hpp
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions include/Edge/DirectedEdge.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class DirectedEdge : public Edge<T> {
return UndirectedEdge<T>(Edge<T>::getId(), Edge<T>::getNodePair());
}

friend std::ostream &operator<<<>(std::ostream &os,
const DirectedEdge<T> &edge);
friend std::ostream &operator<< <>(std::ostream &os,
const DirectedEdge<T> &edge);
};

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions include/Edge/DirectedWeightedEdge.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class DirectedWeightedEdge : public DirectedEdge<T>, public Weighted {
Weighted::getWeight());
}

friend std::ostream &operator<<<>(std::ostream &os,
const DirectedWeightedEdge<T> &edge);
friend std::ostream &operator<< <>(std::ostream &os,
const DirectedWeightedEdge<T> &edge);
};

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion include/Edge/Edge.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Edge {
// operator UndirectedEdge<T>() const { return UndirectedEdge<T>(id,
// nodePair); }

friend std::ostream &operator<<<>(std::ostream &os, const Edge<T> &edge);
friend std::ostream &operator<< <>(std::ostream &os, const Edge<T> &edge);
};

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions include/Edge/UndirectedEdge.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class UndirectedEdge : public Edge<T> {
return DirectedEdge<T>(Edge<T>::getId(), Edge<T>::getNodePair());
}

friend std::ostream &operator<<<>(std::ostream &os,
const UndirectedEdge<T> &edge);
friend std::ostream &operator<< <>(std::ostream &os,
const UndirectedEdge<T> &edge);
};

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions include/Edge/UndirectedWeightedEdge.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class UndirectedWeightedEdge : public UndirectedEdge<T>, public Weighted {
Weighted::getWeight());
}

friend std::ostream &operator<<<>(std::ostream &os,
const UndirectedWeightedEdge<T> &edge);
friend std::ostream &operator<< <>(std::ostream &os,
const UndirectedWeightedEdge<T> &edge);
};

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion include/Edge/Weighted.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Weighted {

public:
Weighted();
Weighted(const double weight);
explicit Weighted(const double weight);
virtual ~Weighted() = default;
double getWeight() const;
void setWeight(const double weight);
Expand Down
32 changes: 17 additions & 15 deletions include/Graph/Graph.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Graph {
* @param edgeSet The Edge Set
*
*/
virtual void setEdgeSet(T_EdgeSet<T> &edgeSet);
virtual void setEdgeSet(const T_EdgeSet<T> &edgeSet);
/**
* \brief
* Function add an Edge to the Graph Edge Set
Expand All @@ -143,7 +143,7 @@ class Graph {
* @param edgeId The Edge Id to remove
*
*/
virtual void removeEdge(unsigned long long edgeId);
virtual void removeEdge(const unsigned long long edgeId);
/**
* \brief
* Function that return the Node Set of the Graph
Expand Down Expand Up @@ -545,14 +545,15 @@ class Graph {
* @return The partiton Map of the partitioned graph
*/
virtual PartitionMap<T> partitionGraph(
PARTITIONING::PartitionAlgorithm algorithm,
unsigned int numberOfPartitions, double param1 = 0.0, double param2 = 0.0,
double param3 = 0.0,
unsigned int numberOfthreads = std::thread::hardware_concurrency()) const;

friend std::ostream &operator<<<>(std::ostream &os, const Graph<T> &graph);
friend std::ostream &operator<<<>(std::ostream &os,
const AdjacencyMatrix<T> &adj);
const PARTITIONING::PartitionAlgorithm algorithm,
const unsigned int numberOfPartitions, const double param1 = 0.0,
const double param2 = 0.0, const double param3 = 0.0,
const unsigned int numberOfthreads =
std::thread::hardware_concurrency()) const;

friend std::ostream &operator<< <>(std::ostream &os, const Graph<T> &graph);
friend std::ostream &operator<< <>(std::ostream &os,
const AdjacencyMatrix<T> &adj);
};

template <typename T>
Expand All @@ -576,7 +577,7 @@ const T_EdgeSet<T> &Graph<T>::getEdgeSet() const {
}

template <typename T>
void Graph<T>::setEdgeSet(T_EdgeSet<T> &edgeSet) {
void Graph<T>::setEdgeSet(const T_EdgeSet<T> &edgeSet) {
this->edgeSet.clear();
for (const auto &edgeSetIt : edgeSet) {
/*
Expand Down Expand Up @@ -605,7 +606,7 @@ void Graph<T>::addEdge(const Edge<T> *edge) {
}

template <typename T>
void Graph<T>::removeEdge(unsigned long long edgeId) {
void Graph<T>::removeEdge(const unsigned long long edgeId) {
auto edgeOpt = Graph<T>::getEdge(edgeId);
if (edgeOpt.has_value()) {
/*
Expand Down Expand Up @@ -2657,9 +2658,10 @@ int Graph<T>::readFromFile(InputOutputFormat format,

template <typename T>
PartitionMap<T> Graph<T>::partitionGraph(
PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions,
double param1, double param2, double param3,
unsigned int numberOfThreads) const {
const PARTITIONING::PartitionAlgorithm algorithm,
const unsigned int numberOfPartitions, const double param1,
const double param2, const double param3,
const unsigned int numberOfThreads) const {
PartitionMap<T> partitionMap;
PARTITIONING::Globals globals(numberOfPartitions, algorithm, param1, param2,
param3, numberOfThreads);
Expand Down
192 changes: 89 additions & 103 deletions include/Node/Node.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,128 +9,114 @@
/*** Header-Only C++ Library for Graph ***/
/*** Representation and Algorithms ***/
/***********************************************************/
/*** Author: ZigRazor ***/
/*** Author: ZigRazor ***/
/*** E-Mail: zigrazor@gmail.com ***/
/***********************************************************/
/*** Collaboration: ----------- ***/
/***********************************************************/
/*** License: AGPL v3.0 ***/
/*** License: AGPL v3.0 ***/
/***********************************************************/


#ifndef __CXXGRAPH_NODE_H__
#define __CXXGRAPH_NODE_H__

#pragma once
#include <iostream>
#include <openssl/sha.h>
#include <iomanip>


namespace CXXGRAPH
{
template <typename T>
class Node;
template <typename T>
std::ostream &operator<<(std::ostream &os, const Node<T> &node);
template <typename T>
class Node
{
private:
std::size_t id = 0;
std::string userId = "";
T data;
void setId(std::string_view);

public:
Node(std::string_view, const T &data);
~Node() = default;
const std::size_t &getId() const;
const std::string &getUserId() const;
const T &getData() const;
//operator
bool operator==(const Node<T> &b) const;
bool operator<(const Node<T> &b) const;
friend std::ostream &operator<<<>(std::ostream &os, const Node<T> &node);
};

template <typename T>
Node<T>::Node(std::string_view id, const T &data)
{
this->userId = id;
// the userid is set as sha512 hash of the user provided id
setId(id);
this->data = data;
}
#include <iomanip>
#include <iostream>

template <typename T>
void Node<T>::setId(std::string_view inpId)
{
//const unsigned char* userId = reinterpret_cast<const unsigned char *>((*inpId).c_str() );
//unsigned char obuf[64];
//unsigned long long obuf[8];
//SHA512(userId, (*inpId).length(), reinterpret_cast<unsigned char*>(obuf));
/**
// Transform byte-array to string
std::stringstream shastr;
shastr << std::hex << std::setfill('0');
int i = 0;
//unsigned long can only store 8 bytes so we truncate the hash to 8 bytes
for (const auto &byte: obuf)
{
shastr << std::setw(2) << static_cast<int>(byte);
i++;
if (i==8) break;
}
auto idStr = shastr.str();
// convert hex string to unsigned long long
std::istringstream iss(idStr);
iss >> std::hex >> this->id;

**/
this->id = std::hash<std::string_view>{}(inpId);

}
namespace CXXGRAPH {
template <typename T>
class Node;
template <typename T>
std::ostream &operator<<(std::ostream &os, const Node<T> &node);
template <typename T>
class Node {
private:
std::size_t id = 0;
std::string userId = "";
T data;
void setId(const std::string &);

public:
Node(const std::string &, const T &data);
~Node() = default;
const std::size_t &getId() const;
const std::string &getUserId() const;
const T &getData() const;
// operator
bool operator==(const Node<T> &b) const;
bool operator<(const Node<T> &b) const;
friend std::ostream &operator<< <>(std::ostream &os, const Node<T> &node);
};

template <typename T>
Node<T>::Node(const std::string &id, const T &data) {
this->userId = id;
// the userid is set as sha512 hash of the user provided id
setId(id);
this->data = data;
}

template <typename T>
const std::size_t &Node<T>::getId() const
{
return id;
}
template <typename T>
void Node<T>::setId(const std::string &inpId) {
// const unsigned char* userId = reinterpret_cast<const unsigned char
// *>((*inpId).c_str() ); unsigned char obuf[64]; unsigned long long obuf[8];
// SHA512(userId, (*inpId).length(), reinterpret_cast<unsigned char*>(obuf));
/**
// Transform byte-array to string
std::stringstream shastr;
shastr << std::hex << std::setfill('0');
int i = 0;
//unsigned long can only store 8 bytes so we truncate the hash to 8 bytes
for (const auto &byte: obuf)
{
shastr << std::setw(2) << static_cast<int>(byte);
i++;
if (i==8) break;
}
auto idStr = shastr.str();
// convert hex string to unsigned long long
std::istringstream iss(idStr);
iss >> std::hex >> this->id;

**/
this->id = std::hash<std::string>{}(inpId);
}

template <typename T>
const std::string &Node<T>::getUserId() const
{
return userId;
}
template <typename T>
const std::size_t &Node<T>::getId() const {
return id;
}

template <typename T>
const T &Node<T>::getData() const
{
return data;
}
template <typename T>
const std::string &Node<T>::getUserId() const {
return userId;
}

template <typename T>
bool Node<T>::operator==(const Node<T> &b) const
{
return (this->id == b.id && this->data == b.data);
}
template <typename T>
const T &Node<T>::getData() const {
return data;
}

template <typename T>
bool Node<T>::operator<(const Node<T> &b) const
{
return (this->id < b.id);
}
template <typename T>
bool Node<T>::operator==(const Node<T> &b) const {
return (this->id == b.id && this->data == b.data);
}

template <typename T>
bool Node<T>::operator<(const Node<T> &b) const {
return (this->id < b.id);
}

//ostream overload
template <typename T>
std::ostream &operator<<(std::ostream &os, const Node<T> &node)
{
os << "Node: {\n"
<< " Id:\t" << node.userId << "\n Data:\t" << node.data << "\n}";
return os;
}
// ostream overload
template <typename T>
std::ostream &operator<<(std::ostream &os, const Node<T> &node) {
os << "Node: {\n"
<< " Id:\t" << node.userId << "\n Data:\t" << node.data << "\n}";
return os;
}
} // namespace CXXGRAPH

#endif // __CXXGRAPH_NODE_H__
#endif // __CXXGRAPH_NODE_H__
Loading