a static array:smart_hash

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>
 
R

Rolf Magnus

dingounan said:
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;

Ok, here you're invoking undefined behavior 100 times. Now write 100 times:
"I will not access arrays beyond their bounds!".
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];


Well, there are other sort algorithms that need less space and less time.
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>

But those will exist for the whole lifetime of the program, won't they?
 
A

Asfand Yar Qazi

Rolf said:
dingounan wrote:

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;


Ok, here you're invoking undefined behavior 100 times. Now write 100 times:
"I will not access arrays beyond their bounds!".

It's pretty obvious he meant:
int data[100];for(int i=0;i<100;++i) data=0;
 
R

Rolf Magnus

Asfand said:
Rolf said:
dingounan wrote:

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;


Ok, here you're invoking undefined behavior 100 times. Now write 100
times: "I will not access arrays beyond their bounds!".

It's pretty obvious he meant:
int data[100];for(int i=0;i<100;++i) data=0;


I know. That's why I didn't answer so seriously. If I had thought that
wasn't a typo, I would have explained the problem in more detail and with
less irony.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top