#include <memory>
#include <map>
#include <stdexcept>
using namespace std;
class NoSuchNode:public runtime_error
{
public:
NoSuchNode(const char* str):runtime_error(str){}
NoSuchNode():runtime_error("can not find th node"){}
};
class NodeHasExist:public runtime_error
{
public:
NodeHasExist():runtime_error("the node has existed"){}
};
template <typename T>
struct Edge
{
T weight;
};
template <typename X,typename Y>
class AdMatrix
{
int num;
map<int,unique_ptr<Y>> nodes;
unique_ptr<unique_ptr<Edge<X>[]>[]> edges;
bool isNode(int no);
public:
AdMatrix(int num);
void addEdge(int from,int to, const X & weight);
void addEdge(int from,int to, X && weight);
int addNode(int no);
Edge<X>& getEdge(int from,int to);
Y& getNode(int no);
};
template <typename X,typename Y>
Y& AdMatrix<X,Y>::getNode(int no)
{
if(this->isNode(no))
return *this->nodes[no];
else
throw NoSuchNode();
}
template <typename X,typename Y>
Edge<X>& AdMatrix<X,Y>::getEdge(int from,int to)
{
if(this->isNode(from) && this->isNode(to))
return this->edges[from][to];
else
throw NoSuchNode();
}
template <typename X,typename Y>
AdMatrix<X,Y>::AdMatrix(int num)
{
this->edges = unique_ptr<unique_ptr<Edge<X>[]>[]>(new unique_ptr<Edge<X>[]>[num]);
for (int i=0; i<num; i++) {
this->edges[i] = unique_ptr<Edge<X>[]>(new Edge<X>[num]);
}
this->num = num;
}
template <typename X,typename Y>
bool AdMatrix<X,Y>::isNode(int no)
{
auto it = this->nodes.find(no);
if(it!=this->nodes.end())
return true;
else
return false;
}
template <typename X,typename Y>
int AdMatrix<X,Y>::addNode(int no)
{
if(this->isNode(no))
throw NodeHasExist();
else
this->nodes[no]=unique_ptr<Y>(new Y());
return no;
}
template <typename X,typename Y>
void AdMatrix<X,Y>::addEdge(int from, int to, const X & weight)
{
if(this->isNode(from) && this->isNode(to))
this->edges[from][to].weight=weight;
else
throw NoSuchNode();
}
template <typename X,typename Y>
void AdMatrix<X,Y>::addEdge(int from, int to,X && weight)
{
if(this->isNode(from) && this->isNode(to))
this->edges[from][to].weight=move(weight);
else
throw NoSuchNode();
}
----main
#include "AdMatrix.h"
#include <vector>
class EdgeWeight
{
int character_t;
bool isEmpty_t;
public:
EdgeWeight(){this->isEmpty_t=true;this->character_t=-1;}
EdgeWeight(int character)
{
this->character_t = character;
this->isEmpty_t=false;
}
bool isChar(int character);
void setEmpty(bool isEmpty){this->isEmpty_t = isEmpty;}
bool isEmpty(){return this->isEmpty_t;}
};
struct Node
{
bool isVisited;
vector<int> edges_to; //even other container,set
Node(){this->isVisited=false;}
};
int main(int args,char** argvs)
{
AdMatrix<unique_ptr<EdgeWeight>,Node> matrix(5);
matrix.addNode(1);
matrix.addNode(2);
matrix.addEdge(1, 2, unique_ptr<EdgeWeight>(new EdgeWeight(10)));
auto & node = matrix.getNode(1);
node.edges_to.push_back(1);
return 0;
}
someone who ran it on Linux has not find any error. Even checked with Valgrind, and did not find any memory issues.
My develop environment:
Mac OS X 10.10.2
dialect c++11
libc++
Xcode Version 6.3.2 (6D2105)
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
The error:(I can not upload the image)
libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector