template with integer parameter and special implementation for N=1

M

marcomoeller

hi all,

i need for an project an "finiteQue" class. what save the last N
pushed values and forgot the other ones..

like this:
template <class T,std::size_t N>
class FinitQue {
public:
inline void push_front(const T newDat);
inline T get(std::size_t idx) const ;
inline T operator[](std::size_t idx);
inline T get_back() const;
inline T get_front() const;
inline std::size_t getAmount() const;
inline std::size_t getSize() const;
inline bool operator==(const FinitQue<T,N> & h2) const;
};

Now I whant to give two different implementations, one for N == 1
(data of type T) and one for N>=1 (Array of fix size)

how to do this? it is REALY speed critical.. with dynamic memory the
runtime of my simulation is going up by times 4!!!

THX for you help

marco
 
O

Ondra Holub

(e-mail address removed) napsal(a):
hi all,

i need for an project an "finiteQue" class. what save the last N
pushed values and forgot the other ones..

like this:
template <class T,std::size_t N>
class FinitQue {
public:
inline void push_front(const T newDat);
inline T get(std::size_t idx) const ;
inline T operator[](std::size_t idx);
inline T get_back() const;
inline T get_front() const;
inline std::size_t getAmount() const;
inline std::size_t getSize() const;
inline bool operator==(const FinitQue<T,N> & h2) const;
};

Now I whant to give two different implementations, one for N == 1
(data of type T) and one for N>=1 (Array of fix size)

how to do this? it is REALY speed critical.. with dynamic memory the
runtime of my simulation is going up by times 4!!!

THX for you help

marco

You have to create specialization of the whole class:
template <class T>
class FinitQue<T, 1>
{
....
 
A

Alf P. Steinbach

* (e-mail address removed):
hi all,

i need for an project an "finiteQue" class. what save the last N
pushed values and forgot the other ones..

like this:
template <class T,std::size_t N>
class FinitQue {
public:
inline void push_front(const T newDat);
inline T get(std::size_t idx) const ;
inline T operator[](std::size_t idx);
inline T get_back() const;
inline T get_front() const;
inline std::size_t getAmount() const;
inline std::size_t getSize() const;
inline bool operator==(const FinitQue<T,N> & h2) const;
};

Now I whant to give two different implementations, one for N == 1
(data of type T) and one for N>=1 (Array of fix size)

how to do this? it is REALY speed critical.. with dynamic memory the
runtime of my simulation is going up by times 4!!!

You can specialize for N == 1,

template<class T>
class FinitQue<T, 1> { ... };

but why don't you use an array in all cases?

boost::array or whatever.

By the way, what's with the "get" prefixes and mix of naming
conventions? Makes me suspect Java background. What on Earth do you
expect the prefixes will do of good in C++?

Cheers,

- Alf
 
M

marcomoeller

Hi,
You have to create specialization of the whole class:
template <class T>
class FinitQue<T, 1>
{
...

I tried something like this: but I get following "error: previous
declaration 'template<class T> class FinitQueFix'....."

how do i this better?



template <class T,std::size_t N>
class FinitQueFix {
public:
inline FinitQueFix() ;
inline void push_front(const T newDat);
inline T get(std::size_t idx) const ;
inline T get_front() const;
inline std::size_t getAmount() const;
inline std::size_t getSize() const;
};


template <class T>
class FinitQueFix<T,1> {
public:
inline void push_front(const T newDat){
data = newDat;
}
inline T get(std::size_t idx) const {
return data;
}
inline T get_front() const{
return data;
}
inline std::size_t getAmount() const {
return 1;
}
inline std::size_t getSize() const {
return 1;
}
private:
T data;
};


template <class T,std::size_t N>
class FinitQueFix {
public:
inline FinitQueFix() {
amount = 0;
posTop = 0;
}

inline void push_front(const T newDat){
posTop += N-1;
posTop %= N;
amount = min(N, amount+1);
arr[posTop] = newDat;
}

inline T get_front() const{
return arr[posTop];
}

inline std::size_t getAmount() const {
return amount;
}

inline std::size_t getSize() const {
return N;
}

private:
std::size_t posTop;
std::size_t amount;
T arr[N];
};
 
M

marcomoeller

hi!
but why don't you use an array in all cases?
I whant to save the extra code to find the correct array element... it
has to be VERY fast.... and i can save by this some extra lines of
"amount checking code"....
boost::array or whatever.

By the way, what's with the "get" prefixes and mix of naming
conventions? Makes me suspect Java background. What on Earth do you
expect the prefixes will do of good in C++?
what do you mean exactly? I have fist learned c/c++ .. was conctrained
to learn java.. and now Im back.... :)

Thx for your help

marco
 
M

marcomoeller

oh ... I just fund out.... I have to change the ordering of the
code... and dont need the declaration stuff.....

ok.. 10 % faster.... less than expected... but better than noting :)

Thx for your help!

marco
 
A

Alf P. Steinbach

* (e-mail address removed):
hi!

I whant to save the extra code to find the correct array element... it
has to be VERY fast.... and i can save by this some extra lines of
"amount checking code"....

Your preconceived idea of what will be fast is most likely wrong.

Measure.

Second, special-casing a single-element structure leads to more code,
not less. Also, more to maintain.
what do you mean exactly? I have fist learned c/c++ .. was conctrained
to learn java.. and now Im back.... :)

In Java a get prefix can be useful for introspection.

In C++ there is no introspection, thus, in C++ it's just added noise.


Cheers, & hth.,

- Alf
 
T

terminator

Hi,


I tried something like this: but I get following "error: previous
declaration 'template<class T> class FinitQueFix'....."

how do i this better?

I am not sure about the source of your trouble some compilers fall
short when it comes to specialize template classes with multiple
arguments (eg. vs.net 2002).I think you`ll feel better by seperating
parameters:

template<typename type>
class TQue{
template <size_t N>
struct finite{
//etc...
};
//template<>
struct finite<1>{
//etc...
};
};

regards,
FM.
 

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
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top