PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > template with integer parameter and special implementation for N=1
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
template with integer parameter and special implementation for N=1

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 15h12   #1
marcomoeller@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut template with integer parameter and special implementation for N=1

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

marco

  Réponse avec citation
Vieux 16/10/2007, 15h18   #2
Ondra Holub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template with integer parameter and special implementation for N=1


marcomoeller@googlemail.com 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
>
> marco


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

  Réponse avec citation
Vieux 16/10/2007, 15h19   #3
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template with integer parameter and special implementation forN=1

* marcomoeller@googlemail.com:
> 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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 16/10/2007, 15h31   #4
marcomoeller@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template with integer parameter and special implementation for N=1

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];
};

  Réponse avec citation
Vieux 16/10/2007, 15h34   #5
marcomoeller@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template with integer parameter and special implementation for N=1

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

marco

  Réponse avec citation
Vieux 16/10/2007, 16h02   #6
marcomoeller@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template with integer parameter and special implementation for N=1

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 !

marco

  Réponse avec citation
Vieux 16/10/2007, 17h27   #7
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template with integer parameter and special implementation forN=1

* marcomoeller@googlemail.com:
> 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"....


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.

>
>> 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.... :-)


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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 17/10/2007, 12h40   #8
terminator
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template with integer parameter and special implementation for N=1

On Oct 16, 5:31 pm, "marcomoel...@googlemail.com"
<marcomoel...@googlemail.com> wrote:
> 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?
>


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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h32.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13728 seconds with 16 queries