Microsoft PowerPoint - Lecture 20 - Templates

Document Sample
Microsoft PowerPoint - Lecture 20 - Templates Powered By Docstoc
					             CSE 212
          Prof. Izaguirre
               Templates
                Hashing




Outline
  Objective: understand the design and
     implementation of class templates,
     with the example of a hash table
     template
  1. Templates
  2. Hash tables
  3. Implementation




                                          1
Class Template I
   A class template allows     template <typename T>
   us to implement a           class T_CLASS {
   generic class                  public:
   This allows type               // constructor with
   variation                       // argument of type T
                                    T_CLASS (const T& item);
   The instantiation of a          // returns a value of
   class template is a            // type T
   class                            T f() const;
   We have already used           // argument of type T
   templates such as              void g(const T& item);
   list<t> and vector<t>          private:
                                    T dataValue;
                               };




Class Template II
   Breaking declaration        template <typename T>
   and definition of class     T T_CLASS<T>::f() const {
   template member               T result;
   functions is an
   exquisite exercise on         //…
   troglodyte syntax…            return result;
   We will put both            }
   declaration and
   definition in a header      template <typename T>
   file, but will split both   void T_CLASS<T>::g(
   in the same file               const T& item) {
   This is how we would           //…
   write the definitions          }
   of f() and g(const T&)
   above:




                                                               2
Hashing

   Motivation
      Amortized constant-time insert and search
   Hash functions
      Good distribution minimizes collisions
      Difficult because of birthday paradox
      Fast computation is machine dependent.
      Horner’s rule is useful
   Collision resolution
      Chaining
      Efficiency analysis indicates performance
      degrades when table is full or for large tables
   Implementation




Chaining with Separate Lists
   < bucket 0 >


   < bucket 1 >

   < bucket 2 >

                                   ....
   < bucket n-1 >

                    < Bucket 0 >   77(1)

                    < Bucket 1 >   89(1)   45(2)

                    < Bucket 2 >   35(1)

                    < Bucket 3 >   14(1)

                    < Bucket 4 >
                    < Bucket 5 >
                    < Bucket 6 >   94(1)
                    < Bucket 7 >
                    < Bucket 8 >
                    < Bucket 9 >
                    < Bucket 10>   54(1)   76(2)




                                                        3
     Hash Iterator hIter
                      hash<int, hFintID> ht;
                      hash<int, hFintID>::iterator hIter;

                                 hashTable = &ht
                    hIter        currentBucket=2      *hIter = 22.
                                   currentLoc


                                      hf(x) = x

       buckets[0]           10

       buckets[1]                        emp ty

ht     buckets[2]           2          22

       buckets[3]                        emp ty
       buckets[4]           29




     Function objects
         Function objects are               template <typename T>
         instances of classes that          class GREATER_THAN {
         have the function-call               public:
         operator() defined
                                              bool operator() (
         These objects behave like             const T& arg1,
         functions
                                               const T& arg2) const {
         operator() may take many                return arg1 > arg2;
         arguments
                                               }
         Using function objects
         generates more efficient           };
         code                               vector<int> my_vec;
                                            GREATER_THAN<int> my_g;
                                            sort(my_vec.begin(),
                                               my_vec.end(), my_g)




                                                                        4