D
dingounan
a static array:smart_hash
it's a usual way to use native array in C++:
//this is a special "sort" algorithm
#include <iostream>
using namespace std;
int main()
{
int task[5]={1,3,2,5,4};
int data[100];for(int i=0;i<100;++i) data[100]=0;
for(int i=0;i<5;++i)
data[task]=task;
for(int i=0;i<100;++i)
if(data!=0) cout<<data<<'\t';
return 0;
}
//
the fail of above is:
it's too waste to use data[100] sort a task[5];
smart_hash will change that:
it will allocate the memory for object only when you use this object.
smart_hash is like this:
#ifndef SMART_HASH_HPP
#define SMART_HASH_HPP
#include <stddef.h>
class nulltype;
template<size_t N,typename t1=char,typename t2=char> class duo
{
public:
typedef typename duo<N,t1,typename duo<N-1,t1,t2>::type> type;
};
template<typename t1,typename t2> class duo<0,t1,t2>
{
public:
typedef typename nulltype type;
};
template<typename T,size_t index,typename ID=duo<index>::type> class
smart_hash
{
public:
static T& value()
{
return native_interface;
}
private:
static T native_interface;
smart_hash();
smart_hash& operator= (smart_hash const&);
~smart_hash();
};
template<typename T,size_t index,typename ID>
T smart_hash<T,index,ID>::native_interface=T(0);
#endif
you can write a test program:
#include "smart_hash.hpp"
#include <iostream>
using namespace std;
int main()
{
smart_hash<int,1>::value()=5;
smart_hash<int,6>::value()=6;
cout<<smart_hash<int,1>::value()<<endl; //print: 5
cout<<smart_hash<int,6>::value()<<endl; //print: 6
return 0;
}
though you use the index:1,6 ,the object smart_hash<int,0>(and
smart_hash<int,2> ...)
doesn't exist in the memory.there are only two objects:
smart_hash<int,1> and smart_hash<int,6>
it's a usual way to use native array in C++:
//this is a special "sort" algorithm
#include <iostream>
using namespace std;
int main()
{
int task[5]={1,3,2,5,4};
int data[100];for(int i=0;i<100;++i) data[100]=0;
for(int i=0;i<5;++i)
data[task]=task;
for(int i=0;i<100;++i)
if(data!=0) cout<<data<<'\t';
return 0;
}
//
the fail of above is:
it's too waste to use data[100] sort a task[5];
smart_hash will change that:
it will allocate the memory for object only when you use this object.
smart_hash is like this:
#ifndef SMART_HASH_HPP
#define SMART_HASH_HPP
#include <stddef.h>
class nulltype;
template<size_t N,typename t1=char,typename t2=char> class duo
{
public:
typedef typename duo<N,t1,typename duo<N-1,t1,t2>::type> type;
};
template<typename t1,typename t2> class duo<0,t1,t2>
{
public:
typedef typename nulltype type;
};
template<typename T,size_t index,typename ID=duo<index>::type> class
smart_hash
{
public:
static T& value()
{
return native_interface;
}
private:
static T native_interface;
smart_hash();
smart_hash& operator= (smart_hash const&);
~smart_hash();
};
template<typename T,size_t index,typename ID>
T smart_hash<T,index,ID>::native_interface=T(0);
#endif
you can write a test program:
#include "smart_hash.hpp"
#include <iostream>
using namespace std;
int main()
{
smart_hash<int,1>::value()=5;
smart_hash<int,6>::value()=6;
cout<<smart_hash<int,1>::value()<<endl; //print: 5
cout<<smart_hash<int,6>::value()<<endl; //print: 6
return 0;
}
though you use the index:1,6 ,the object smart_hash<int,0>(and
smart_hash<int,2> ...)
doesn't exist in the memory.there are only two objects:
smart_hash<int,1> and smart_hash<int,6>