473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

a static array:smart_hash

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[i]]=task[i];
for(int i=0;i<100;++i)
if(data[i]!=0) cout<<data[i]<<'\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>

Jul 22 '05 #1
3 1408
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!".
for(int i=0;i<5;++i)
data[task[i]]=task[i];
for(int i=0;i<100;++i)
if(data[i]!=0) cout<<data[i]<<'\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?

Jul 22 '05 #2
Rolf Magnus wrote:
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[i]=0;
Jul 22 '05 #3
Asfand Yar Qazi wrote:
Rolf Magnus wrote:
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[i]=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.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.