|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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> { .... |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
* 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? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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]; }; |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
* 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? |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|