www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Reg :: Comment my Code

reply "Sumit Adhikari" <sumit.adhikari gmail.com> writes:
I am new in D Language. Following code is working fine. Still I
want you people to comment on issues of my code.

Every code is designed to accomplish some particular job -
consider my code is generic. I will be obliged if you may please
spend some time for my D learning.


===========================================

module filter ;

abstract class filter_base(Tinp,Tout,Tcof)
{
private :

      static immutable enum {
         FLOATING,
         INTEGRAL,
         UNKNOWN
      };

      Tcof[]    fcoeffs   ;
      Tcof[]    icoeffs   ;
      Tinp[]    fir_delay ;
      Tout[]    iir_delay ;

      static      if (__traits(isFloating, Tinp)) static immutable
int itype = FLOATING ;
      else static if (__traits(isIntegral, Tinp)) static immutable
int itype = INTEGRAL ;
      else                                        static immutable
int itype = UNKNOWN  ;

      static      if (__traits(isFloating, Tout)) static immutable
int otype = FLOATING ;
      else static if (__traits(isIntegral, Tout)) static immutable
int otype = INTEGRAL ;
      else                                        static immutable
int otype = UNKNOWN  ;

      static      if (__traits(isFloating, Tcof)) static immutable
int ctype = FLOATING ;
      else static if (__traits(isIntegral, Tcof)) static immutable
int ctype = INTEGRAL ;
      else                                        static immutable
int ctype = UNKNOWN  ;

public :

      this(immutable Tcof[] fcoeffs_) {

         if ( (itype == UNKNOWN) | (otype == UNKNOWN) |(ctype ==
UNKNOWN) ){
             throw new Exception(" FATAL :: Template type trait
exception for filter_base");
         }

         assert(fcoeffs_.length > 0);
         fcoeffs = fcoeffs_ ;
         fir_delay.length = fcoeffs.length - 1 ;
      }

      this(immutable Tcof[] fcoeffs_, immutable Tcof[] icoeffs_){

         if ( (itype == UNKNOWN) | (otype == UNKNOWN) | (ctype ==
UNKNOWN) ){
             throw new Exception(" FATAL :: Template type trait
exception for filter_base");
         }

         assert(fcoeffs_.length > 0);
         fcoeffs = fcoeffs_ ;
         fir_delay.length = fcoeffs.length - 1 ;

         assert(icoeffs_.length > 0);
         icoeffs = icoeffs_ ;
         iir_delay.length = icoeffs.length - 1 ;
      }

      ~this(){}

} ;


class fir(Tinp,Tout,Tcof) : filter_base!(Tinp,Tout,Tcof)
{

      this(immutable Tcof[] fcoeffs_) {
         super(fcoeffs_);
      }

      ~this(){}

      Tout read(immutable Tinp inp_){

        synchronized {

            Tout fir_out = fcoeffs[0]  * inp_;

            for (uint i = 0 ; i < cast(uint)(fcoeffs.length - 1);
++i)  fir_out += fcoeffs[i+1]*fir_delay[i] ;

            for (uint i = cast(uint)(fcoeffs.length - 2) ; i > 0 ;
--i) fir_delay[i] = fir_delay[i-1];
            fir_delay[0] = inp_ ;

            return (fir_out);

         }
      }
};


==============================================

Regards, Sumit
Nov 09 2013
next sibling parent John J <john.joyus gmail.com> writes:
On 11/09/2013 12:25 PM, Sumit Adhikari wrote:
 I am new in D Language. Following code is working fine. Still I
 want you people to comment on issues of my code.

 Every code is designed to accomplish some particular job -
 consider my code is generic. I will be obliged if you may please
 spend some time for my D learning.
The "D.learn" forum is more appropriate for your request.
Nov 09 2013
prev sibling parent John J <john.joyus gmail.com> writes:
On 11/09/2013 12:25 PM, Sumit Adhikari wrote:
 I am new in D Language. Following code is working fine. Still I
 want you people to comment on issues of my code.

 Every code is designed to accomplish some particular job -
 consider my code is generic. I will be obliged if you may please
 spend some time for my D learning.
Also, when you have long example to show, you can paste it to http://dpaste.dzfl.pl/ save it there and provide the link. That makes it more readable and easy to even compile with different D compilers.
Nov 09 2013