SlideShare ist ein Scribd-Unternehmen logo
1 von 52
Downloaden Sie, um offline zu lesen
Introduction to GIL,
Boost and Generic
Programming

Hailin Jin
Advanced Technology Labs
Adobe Systems Incorporated




http://www.adobe.com/technology/people/sanjose/jin.html
                                                        1
2010 Adobe Systems Incorporated. All Rights Reserved.
Outline

     GIL
     Boost
     Generic programming
     STL
     Summary




2010 Adobe Systems Incorporated. All Rights Reserved.
What is GIL?

     Generic Image Library
     C++ image library
     Open-source
     http://opensource.adobe.com/gil
     Well documented
     The core library only contains header files; No need to link anything
     Thread-safe
     Compatible with most C++ compilers (Visual Studio, gcc, etc)




2010 Adobe Systems Incorporated. All Rights Reserved.
GIL overview

     Image containers
     1-D iterators for traversing rows and columns
     2-D iterators (locator) for arbitrary 2D traversal
     Image views
     Image processing algorithms
     Extensions
              io extension for reading and writing images of popular formats (PNG, JPEG, TIFF, …)
                              rgb8_image_t im;
                              read_png_image("test.png", im);
              numeric extension for convolutions against fixed and variable-size kernels
                              kernel_1d_fixed<float, 7> kernel;
                              gray8_image_t im_in, im_out;
                              convolve_rows_fixed<gray32f_pixel_t>(const_view(im_in),
                                                                   kernel,
                                                                   view(im_out));




2010 Adobe Systems Incorporated. All Rights Reserved.
People




     Lubomir Bourdev                                    Hailin Jin   Christian Henning
         Adobe                                           Adobe         Independent



2010 Adobe Systems Incorporated. All Rights Reserved.
GIL history

     Started as an Adobe internal project in 2005
     Version 1.0 in June 2006
     Accepted into Boost in November 2006
     Version 2.0 in March 2007
     Constantly updated and patched with Boost releases




2010 Adobe Systems Incorporated. All Rights Reserved.
License

     Initially: MIT license
     Currently: Boost Software License (similar to the MIT license)
     Free to use, reproduce, display, distribute, execute, and transmit, including
      derivative works
     We retain the copyright




2010 Adobe Systems Incorporated. All Rights Reserved.
Image processing libraries

     Your own image libraries
     OpenCV
     CImg
     Vigra
     GIL works third-party algorithms, libraries, etc




2010 Adobe Systems Incorporated. All Rights Reserved.
     http://www.boost.org
     High-quality C++ libraries
     Open-source
     Started in 1998
     Latest version: 1.43.0 on May 6, 2010
     Strict review system
     10 of the Boost libraries are included in the C++ Standard Library TR
     “It is usually a really dumb idea to go and reinvent a wheel that Boost already
      offers. ”
      — Bjarne Stroustrup, inventor of C++




2010 Adobe Systems Incorporated. All Rights Reserved.
Boost usage

     http://www.boost.org/users/uses.html
     Adobe
     Google
     SAP
     McAFee
     Real Networks
     …




2010 Adobe Systems Incorporated. All Rights Reserved.
Boost components for computer vision

     Image (GIL)
     Graph (BGL)
     Thread
     Filesystem
     uBLAS
     Program options
     Math related libraries




2010 Adobe Systems Incorporated. All Rights Reserved.
Boost Graph Library

     Shortest paths
              Dijkstra's shortest paths
              Bellman-Ford shortest paths
              Johnson's all-Pairs shortest paths
     Minimum spanning trees
              Kruskal's minimum spanning tree
              Prim's minimum spanning tree
     Connected components
              Connected components
              Strongly connected components
     Maximum flow
              Edmonds Karp
              Push-relabel
              Kolmogorov
     Topological sort, sparse matrix ordering, and many more




2010 Adobe Systems Incorporated. All Rights Reserved.
Outline

     GIL
     Boost
     Generic programming
     STL
     Summary




2010 Adobe Systems Incorporated. All Rights Reserved.
GIL design principles

     Followed the paradigm of generic programming
     Clean separation between algorithms and containers
     Work with third-party algorithms, data structures, and libraries
     Toward optimal performance




2010 Adobe Systems Incorporated. All Rights Reserved.
Generic programming

     Programming paradigm for developing efficient, reusable software code
     Pioneered by Alexander Stepanov and David Musser
     First successful example is the Standard Template Library (STL) which is now
      part of the C++ standard
     Programming based on parameterization
              Parameterize a data structure with different types (example: a std::vector<> with its
               element types)
              Parameterize an algorithm with different types and algorithms (example: std::sort()
               function for int* and float* and with a comparison function)

     Generalize an algorithm or data structure to its most general and useful form
              Better code
              Fewer bugs




2010 Adobe Systems Incorporated. All Rights Reserved.
Polymorphism

     Allows values of different data types to be handled using a uniform interface
     Object-oriented programming (OOP)
              Ad-hoc polymorphism
              Class hierarchies: specified inheritance
              Function/method overloading: different and potentially heterogeneous implementations
               depending on a limited range of individually specified types and combinations
              Dynamic dispatch

     Generic programming
              Parametric polymorphism (data structure and algorithms)
              Code is written without mention of any specific type and thus can be used transparently
               with any number of new types
              Static (compile-time) dispatch




2010 Adobe Systems Incorporated. All Rights Reserved.
Generic programming: a case study

     Compute the sum of a set of data




                                                        17
2010 Adobe Systems Incorporated. All Rights Reserved.
Object-oriented style

          class int_vector {
          private:
              int* array;
              int n;
              ...
          public:
              ...
              int accumulate() {
                  int result = 0;
                      for (int i = 0; i < n; ++i)
                          result = result + array[i];
                  return result;
              }
              ...
          };




2010 Adobe Systems Incorporated. All Rights Reserved.
Separate the algorithm from the container

          int accumulate(int* array, int n) {
               int result = 0;
               for (int i = 0; i < n; ++i)
                   result = result + array[i];
               return result;
          }

          class int_vector {
          private:
               int* array;
               int n;
               ...
          public:
               ...
               int size() {return n;}
               int* begin() {return array;}
               ...
          };

          int_vector a_vec;
          int result = accumulate(a_vec.begin(), a_vec.size());




2010 Adobe Systems Incorporated. All Rights Reserved.
Same algorithm with different types

          int accumulate(int* array, int n) {
               int result = 0;
               for (int i = 0; i < n; ++i)
                   result = result + array[i];
               return result;
          }

          float accumulate(float* array, int n) {
               float result = 0;
               for (int i = 0; i < n; ++i)
                   result = result + array[i];
               return result;
          }




2010 Adobe Systems Incorporated. All Rights Reserved.
C++ templates

          template <typename T>
          T accumulate(T* array, int n) {
               T result = 0;
               for (int i = 0; i < n; ++i)
                   result = result + array[i];
               return result;
          }




2010 Adobe Systems Incorporated. All Rights Reserved.
String concatenation

          template <typename T>
          T accumulate(T* array, int n) {
               T result = 0;
               for (int i = 0; i < n; ++i)
                   result = result + array[i];
               return result;
          }

          std::string concatenate(std::string* array, int n) {
               std::string result = ””;
               for (int i = 0; i < n; ++i)
                   result = result + array[i];
               return result;
          }

          template <typename T>
          T accumulate(T* array, int n) {
               T result = T();                T result;
               for (int i = 0; i < n; ++i)
                   result = result + array[i];
               return result;
          }



2010 Adobe Systems Incorporated. All Rights Reserved.
Generalize the interface and type requirements

          template <typename T>
          T accumulate(T* array, int n, T init) {
               for (int i = 0; i < n; ++i)
                   init = init + array[i];
               return init;
          }


         Type requirements on T
         •       T must have a copy constructor T(const T&)
         •       T must have an assignment operator=()
         •       T must have a binary operator+(,)




2010 Adobe Systems Incorporated. All Rights Reserved.
Linked list

 template <typename T>
 struct list_node {
     list_node<T>* next;
     T value;
 };

 template <typename T>
 T accumulate(list_node<T>* first,
              T init) {
     while (first!=0) {
         init = init + first->value;
         first = first->next;
     }
     return init;
 }




2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                                  template <typename T>
 struct list_node {                                     T accumulate(T* array, int n, T init) {
     list_node<T>* next;                                     for (int i = 0; i < n; ++i)
     T value;                                                     init = init + array[i];
 };                                                          return init;
                                                        }
 template <typename T>
 T accumulate(list_node<T>* first,
              T init) {
     while (first!=0) {
         init = init + first->value;
         first = first->next;
     }
     return init;
 }




2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                                  template <typename T>
 struct list_node {                                     T accumulate(T* first, int n, T init) {
     list_node<T>* next;                                     T* last = first + n;
     T value;                                                for (;first!=last;++first) {
 };                                                               init = init + *first;
                                                             }
 template <typename T>                                       return init;
 T accumulate(list_node<T>* first,                      }
              T init) {
     while (first!=0) {
         init = init + first->value;
         first = first->next;
     }
     return init;
 }




2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                                  template <typename T>
 struct list_node {                                     T accumulate(T* first, int n, T init) {
     list_node<T>* next;                                     T* last = first + n;
     T value;                                                while (first!=last) {
 };                                                               init = init + *first;
                                                                  ++first;
 template <typename T>                                       }
 T accumulate(list_node<T>* first,                           return init;
              T init) {                                 }
     while (first!=0) {
         init = init + first->value;
         first = first->next;
     }
     return init;
 }




2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                                  template <typename T>
 struct list_node {                                     T accumulate(T* first, T* last, T init) {
     list_node<T>* next;                                     while (first!=last) {
     T value;                                                     init = init + *first;
 };                                                               ++first;
                                                             }
 template <typename T>                                       return init;
 T accumulate(list_node<T>* first,                      }
              T init) {
     while (first!=0) {
         init = init + first->value;
         first = first->next;
     }
     return init;
 }




2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                                  template <typename T>
 struct list_node {                                     T accumulate(T* first, T* last, T init) {
     list_node<T>* next;                                     while (first!=last) {
     T value;                                                     init = init + *first;
 };                                                               ++first;
                                                             }
 template <typename T>                                       return init;
 struct list_iterator {                                 }
     list_node<T>* node;
 };

 template <typename T>
 T accumulate(list_iterator<T> first,
              T init) {
     while (first.node!=0) {
         init = init + first.node->value;
         first.node = first.node->next;
     }
     return init;
 }




2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                                  template <typename T>
 struct list_node {                                     T accumulate(T* first, T* last, T init) {
     list_node<T>* next;                                     while (first!=last) {
     T value;                                                     init = init + *first;
 };                                                               ++first;
                                                             }
 template <typename T>                                       return init;
 struct list_iterator {                                 }
     list_node<T>* node;
 };

 template <typename T>
 T accumulate(list_iterator<T> first,
              list_iterator<T> last,
              T init) {
     while (first!=last) {
         init = init + first.node->value;
         first.node = first.node->next;
     }
     return init;
 }




2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                template <typename T>
 struct list_node {                   T accumulate(T* first, T* last, T init) {
     list_node<T>* next;                   while (first!=last) {
     T value;                                   init = init + *first;
 };                                             ++first;
                                           }
 template <typename T>                     return init;
 struct list_iterator {               }
     list_node<T>* node;                  template <typename T>
 };                                       bool operator!=(list_iterator<T> x,
                                                          list_iterator<T> y) {
 template <typename T>                        return x.node!=y.node;
 T accumulate(list_iterator<T> first,     }
              list_iterator<T> last,
              T init) {
     while (first!=last) {
         init = init + first.node->value;
         first.node = first.node->next;
     }
     return init;
 }




2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                template <typename T>
 struct list_node {                   T accumulate(T* first, T* last, T init) {
     list_node<T>* next;                   while (first!=last) {
     T value;                                   init = init + *first;
 };                                             ++first;
                                           }
 template <typename T>                     return init;
 struct list_iterator {               }
     list_node<T>* node;                  template <typename T>
 };                                       bool operator!=(list_iterator<T> x,
                                                          list_iterator<T> y) {
 template <typename T>                        return x.node!=y.node;
 T accumulate(list_iterator<T> first,     }
              list_iterator<T> last,
              T init) {                   template <typename T>
     while (first!=last) {                T operator*(list_iterator<T> x) {
         init = init + first.node->value;     return x.node->value;
         first.node = first.node->next; }
     }
     return init;                         template <typename T>
 }                                        list_iterator<T>&
                                          operator++(list_iterator<T>& x) {
                                              x.node = x.node->next;
                                              return x;
                                          }



2010 Adobe Systems Incorporated. All Rights Reserved.
Unifying linked lists and arrays

 template <typename T>                template <typename T>
 struct list_node {                   T accumulate(T* first, T* last, T init) {
     list_node<T>* next;                   while (first!=last) {
     T value;                                   init = init + *first;
 };                                             ++first;
                                           }
 template <typename T>                     return init;
 struct list_iterator {               }
     list_node<T>* node;                  template <typename T>
 };                                       bool operator!=(list_iterator<T> x,
                                                          list_iterator<T> y) {
 template <typename T>                        return x.node!=y.node;
 T accumulate(list_iterator<T> first,     }
              list_iterator<T> last,
              T init) {                   template <typename T>
     while (first!=last) {                T operator*(list_iterator<T> x) {
         init = init + *first;                return x.node->value;
         ++first;                         }
     }
     return init;                         template <typename T>
 }                                        list_iterator<T>&
                                          operator++(list_iterator<T>& x) {
                                              x.node = x.node->next;
                                              return x;
                                          }



2010 Adobe Systems Incorporated. All Rights Reserved.
Unified version

        template <typename I,
                  typename T>
        T accumulate(I first, I last, T init) {
            while (first!=last) {
                init = init + *first;
                ++first;
            }
            return init;
        }




2010 Adobe Systems Incorporated. All Rights Reserved.
Type requirements

        template <typename I,
                  typename T>
        T accumulate(I first, I last, T init) {
            while (first!=last) {
                init = init + *first;
                ++first;
            }
            return init;
        }
              • I must have a copy constructor I(const                  I&)
                          •       I must have a binary operator!=(,)
                          •       I must have an operator*()
                          •       I must have an operator++()
                          •       T must have a copy constructor T(const T&)
                          •       T must have an assignment operator=()
                          •       T must have a binary operator+(,)


2010 Adobe Systems Incorporated. All Rights Reserved.
Further generalization with function objects

        template <typename I,
                  typename T>
        T accumulate(I first, I last, T init) {
            while (first!=last) {
                init = init + *first;
                ++first;
            }
            return init;
        }

        template <typename I,
                  typename T,
                  typename B>
        T accumulate(I first, I last, T init, B op) {
            while (first!=last) {
                init = op(init, *first);
                ++first;
            }
            return init;
        }




2010 Adobe Systems Incorporated. All Rights Reserved.
Type requirements

       template <typename I,
                 typename T,
                 typename B>
       T accumulate(I first, I last, T init, B op) {
           while (first!=last) {
               init = op(init, *first);
               ++first;
           }              • I must have a copy constructor I(const                                  I&)
           return init;
       }                  • I must have a binary operator!=(,)

                                                        •   I must have an operator*()
                                                        •   I must have an operator++()
                                                        •   T must have a copy constructor T(const T&)
                                                        •   T must have an assignment operator=()
                                                        •   B must have a copy constructor B(const B&)
                                                        •   B must have a binary application operator()(,)


2010 Adobe Systems Incorporated. All Rights Reserved.
Function object example


    template <typename T>
    struct moment_2 {
         T m1, m2;
         moment_2(T m1_, T m2_) : m1(m1_), m2(m2_) {}
    };
    template <typename T>
    moment_2<T>
    compute_moment_2(T* first, T* last, moment_2<T> init) {
         while (first!=last) {
             init.m1 += *first;
             init.m2 += *first**first;
             ++first;
         }
         return init;
    }




2010 Adobe Systems Incorporated. All Rights Reserved.
Function object example


    template <typename T>
    struct compute_moment_2_s {
         moment_2<T> operator()(moment_2<T> m, T x) const {
             m.m1 += x; m.m2 += x*x;
             return m;
         }
    };
    template <typename T>
    moment_2<T>
    compute_moment_2(T* first, T* last, moment_2<T> init) {
         return accumulate(first, last, init,
                           compute_moment_2_s<T>());
    }
    template <typename T>
    struct max_s {
        T operator()(T x, T y) const {return x>y?x:y;}
    };
    template <typename T>
    T range_max(T* first, T* last, T init) {
        return accumulate(first, last, init, max_s());
    }



2010 Adobe Systems Incorporated. All Rights Reserved.
Lessons learned: #1

      Separate between an algorithms and a data structure

                                                        int accumulate(int* array, int n) {
                                                             int result = 0;
                                                             for (int i = 0; i < n; ++i)
                                                                  result = result + array[i];
 class int_vector {
                                                             return result;
 private:
                                                        }
     int* array;
     int n;
                                           class int_vector {
     ...
                                           private:
 public:
                                                int* array;
     ...
                                                int n;
     int accumulate() {
                                                ...
          int result = 0;
                                           public:
              for (int i = 0; i < n; ++i)
                                                ...
                  result = result + array[i];
                                                int size() {return n;}
                                                int* begin() {return array;}
          return result;
                                                ...
     }
                                           };
     ...
 };
                                           int_vector a_vec;
                                           int result = accumulate(a_vec.begin()




2010 Adobe Systems Incorporated. All Rights Reserved.
Lessons learned: #2

      Generalize an algorithm to a general form




    int accumulate(int* array, int n) {
         int result = 0;
         for (int i = 0; i < n; ++i)
              result = result + array[i];
                                            template <typename T>
         return result;                     T accumulate(T* array, int n) {
    }                                            T result = T();
                                                 for (int i = 0; i < n; ++i)
    float accumulate(float* array, int n) {           result = result + array[i];
         float result = 0;                       return result;
         for (int i = 0; i < n; ++i)        }
              result = result + array[i];
         return result;
    }




2010 Adobe Systems Incorporated. All Rights Reserved.
Lessons learned: #3

      Generalize the interface of an algorithm



                      template <typename T>
                      T accumulate(T* array, int n) {
                           T result = T();
                           for (int i = 0; i < n; ++i)
                               result = result + array[i];
                           return result;
                      }




                      template <typename T>
                      T accumulate(T* array, int n, T init) {
                           for (int i = 0; i < n; ++i)
                               init = init + array[i];
                           return init;
                      }



2010 Adobe Systems Incorporated. All Rights Reserved.
Lessons learned: #4

      Generalize through iterators




template <typename T>
T accumulate(list_iterator<T> first,
              T init) {
    while (first!=0) {
         init = init + first.node->value;
                                          template <typename I,
         first.node = first.node->next;
                                                    typename T>
    }
                                          T accumulate(I first, I last, T init) {
    return init;
                                              while (first!=last) {
}
                                                  init = init + *first;
                                                  ++first;
template <typename T>                         }
T accumulate(T* array, int n, T init) {       return init;
      for (int i = 0; i < n; ++i)         }
           init = init + array[i];
      return init;
}




2010 Adobe Systems Incorporated. All Rights Reserved.
Lessons learned: #5

      Generalize through function objects

                                       template <typename I,
                                                 typename T>
                                       T accumulate(I first, I last, T init) {
                                           while (first!=last) {
                                               init = init + *first;
                                               ++first;
                                           }
                                           return init;
                                       }



                                       template <typename I,
                                                 typename T,
                                                 typename B>
                                       T accumulate(I first, I last, T init, B op) {
                                           while (first!=last) {
                                               init = op(init, *first);
                                               ++first;
                                           }
                                           return init;
                                       }



2010 Adobe Systems Incorporated. All Rights Reserved.
Concepts/Models

     Type requirements
     Concept: description of requirements on one or more types stated in terms
      of the existence and properties of procedures, type attributes, and type
      functions defined on the types
     Model: satisfies all the requirements of a concept
     Example                                           Concept
                                                        •   T must have a copy constructor T(const T&)
template <typename T>
T accumulate(T* array, int n, T init) { • T must have an assignment operator=()
     for (int i = 0; i < n; ++i)        • T must have a binary operator+(,)
          init = init + array[i];
     return init;                       Model
}
                                        • int, float, std::string, …


     Concepts are not designed or invented. Instead they are discovered




2010 Adobe Systems Incorporated. All Rights Reserved.
Iterators

     An interface between sequential data structures and algorithms
     Separates containers from algorithms
     Models: raw points and linked list iterators

                                       template <typename I,
                                                 typename T>
                                       T accumulate(I first, I last, T init) {
                                           while (first!=last) {
                                               init = init + *first;
                                               ++first;
                                           }
                                           return init;
                                       }
                                        •       I must have a copy constructor I(const I&)
                                        •       I must have a binary operator!=(,)
                                        •       I must have operator*()
                                        •       I must have operator++()


2010 Adobe Systems Incorporated. All Rights Reserved.
More models of iterators

        template <typename T>                                            template <typename T>
        class step_pointer {                                             class indirect_pointer {
        privte:                                                          privte:
            T* x;                                                            T* x;
            int step;                                                        int* y;
        public:                                                          public:
            ...                                                              ...
            T operator*() {return *x;}                                       T operator*() {return x[*y];}
            step_pointer& operator++() {                                     step_pointer& operator++() {
                x += step; return *this;                                         ++y; return *this;
            }                                                                }
            ...                                                              ...
        };                                                               };
                                                        template <typename T>
                                                        class value_iterator {
                                                        privte:
                                                            T x;
                                                        public:
                                                            ...
                                                            T operator*() {return x;}
                                                            step_pointer& operator++() {
                                                                 ++x; return *this;
                                                            }
                                                            ...
                                                        };


                                                                    47
2010 Adobe Systems Incorporated. All Rights Reserved.
Compilers are working hard for us

     C++ compilers resolves all templates at compile time
     No dynamic (run-time) dispatch
     Function objects can be inlined; No function calls overhead




2010 Adobe Systems Incorporated. All Rights Reserved.
STL

     Standard Template Library
     http://www.sgi.com/tech/stl
     First widespread application of generic programming
     Part of the C++ standard




              Containers                                Iterators   Algorithms




2010 Adobe Systems Incorporated. All Rights Reserved.
STL

     Containers
              vector, list, slist
              set, map, multiset, multimap

     Algorithms
              find(), for_each(), fill(), transform(), copy()
              partition(), stable_partition()
              sort(), lower_bound(), nth_element(), merge()
              set_difference(), set_union(), set_intersection()
              accumulate(), inner_product(), partial_sum()




2010 Adobe Systems Incorporated. All Rights Reserved.
Summary

     GIL
     Boost
     Generic programming
     STL
     Code reuse
     Better understanding of algorithms
     Better code




2010 Adobe Systems Incorporated. All Rights Reserved.
Book recommendation

     For those who desire a deep understanding
      of programming




                                                        52
2010 Adobe Systems Incorporated. All Rights Reserved.

Weitere ähnliche Inhalte

Was ist angesagt?

Approximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming ApplicationsApproximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming ApplicationsDebasish Ghosh
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm ComplexityIntro C# Book
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1Sunil OS
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variablesIntro C# Book
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Featurestarun308
 
The Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationThe Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationJoel Falcou
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
Python For Scientists
Python For ScientistsPython For Scientists
Python For Scientistsaeberspaecher
 
Python-oop
Python-oopPython-oop
Python-oopRTS Tech
 
Syntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserSyntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserMindbowser Inc
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
Python as number crunching code glue
Python as number crunching code gluePython as number crunching code glue
Python as number crunching code glueJiahao Chen
 
Designing Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.ProtoDesigning Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.ProtoJoel Falcou
 

Was ist angesagt? (20)

Approximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming ApplicationsApproximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming Applications
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
Unit 3
Unit 3Unit 3
Unit 3
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
The Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationThe Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 Migration
 
C++ Chapter IV
C++ Chapter IVC++ Chapter IV
C++ Chapter IV
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
 
Python For Scientists
Python For ScientistsPython For Scientists
Python For Scientists
 
Python-oop
Python-oopPython-oop
Python-oop
 
Syntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserSyntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - Mindbowser
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
Oops concept
Oops conceptOops concept
Oops concept
 
Arduino reference
Arduino   referenceArduino   reference
Arduino reference
 
Bc0037
Bc0037Bc0037
Bc0037
 
On metadata for Open Data
On metadata for Open DataOn metadata for Open Data
On metadata for Open Data
 
Python as number crunching code glue
Python as number crunching code gluePython as number crunching code glue
Python as number crunching code glue
 
Designing Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.ProtoDesigning Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.Proto
 

Andere mochten auch

Roche Statistical Programming Brochure
Roche Statistical Programming BrochureRoche Statistical Programming Brochure
Roche Statistical Programming BrochureCK Group
 
Case Study on the role of radio based extension and advisory services: Lesson...
Case Study on the role of radio based extension and advisory services: Lesson...Case Study on the role of radio based extension and advisory services: Lesson...
Case Study on the role of radio based extension and advisory services: Lesson...Andrea Bohn
 
ETHZ CV2012: Information
ETHZ CV2012: InformationETHZ CV2012: Information
ETHZ CV2012: Informationzukun
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVzukun
 

Andere mochten auch (6)

Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Roche Statistical Programming Brochure
Roche Statistical Programming BrochureRoche Statistical Programming Brochure
Roche Statistical Programming Brochure
 
Case Study on the role of radio based extension and advisory services: Lesson...
Case Study on the role of radio based extension and advisory services: Lesson...Case Study on the role of radio based extension and advisory services: Lesson...
Case Study on the role of radio based extension and advisory services: Lesson...
 
ETHZ CV2012: Information
ETHZ CV2012: InformationETHZ CV2012: Information
ETHZ CV2012: Information
 
Swift for-rubyists
Swift for-rubyistsSwift for-rubyists
Swift for-rubyists
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCV
 

Ähnlich wie Cvpr2010 open source vision software, intro and training part-iii introduction to gil , boost and generic programming - jin - unknown - 2010

Ähnlich wie Cvpr2010 open source vision software, intro and training part-iii introduction to gil , boost and generic programming - jin - unknown - 2010 (20)

PRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptxPRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptx
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1
 
Lab 1.pptx
Lab 1.pptxLab 1.pptx
Lab 1.pptx
 
Java parallel programming made simple
Java parallel programming made simpleJava parallel programming made simple
Java parallel programming made simple
 
Priming Java for Speed at Market Open
Priming Java for Speed at Market OpenPriming Java for Speed at Market Open
Priming Java for Speed at Market Open
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
 
U-SQL Killer Scenarios: Custom Processing, Big Cognition, Image and JSON Proc...
U-SQL Killer Scenarios: Custom Processing, Big Cognition, Image and JSON Proc...U-SQL Killer Scenarios: Custom Processing, Big Cognition, Image and JSON Proc...
U-SQL Killer Scenarios: Custom Processing, Big Cognition, Image and JSON Proc...
 
Eclipse Modeling Framework
Eclipse Modeling FrameworkEclipse Modeling Framework
Eclipse Modeling Framework
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
What is Python?
What is Python?What is Python?
What is Python?
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and Java
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 

Mehr von zukun

My lyn tutorial 2009
My lyn tutorial 2009My lyn tutorial 2009
My lyn tutorial 2009zukun
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statisticsSiwei lyu: natural image statistics
Siwei lyu: natural image statisticszukun
 
Lecture9 camera calibration
Lecture9 camera calibrationLecture9 camera calibration
Lecture9 camera calibrationzukun
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionBrunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionzukun
 
Modern features-part-4-evaluation
Modern features-part-4-evaluationModern features-part-4-evaluation
Modern features-part-4-evaluationzukun
 
Modern features-part-3-software
Modern features-part-3-softwareModern features-part-3-software
Modern features-part-3-softwarezukun
 
Modern features-part-2-descriptors
Modern features-part-2-descriptorsModern features-part-2-descriptors
Modern features-part-2-descriptorszukun
 
Modern features-part-1-detectors
Modern features-part-1-detectorsModern features-part-1-detectors
Modern features-part-1-detectorszukun
 
Modern features-part-0-intro
Modern features-part-0-introModern features-part-0-intro
Modern features-part-0-introzukun
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video searchzukun
 
Lecture 01 internet video search
Lecture 01 internet video searchLecture 01 internet video search
Lecture 01 internet video searchzukun
 
Lecture 03 internet video search
Lecture 03 internet video searchLecture 03 internet video search
Lecture 03 internet video searchzukun
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learningIcml2012 tutorial representation_learning
Icml2012 tutorial representation_learningzukun
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionAdvances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionzukun
 
Gephi tutorial: quick start
Gephi tutorial: quick startGephi tutorial: quick start
Gephi tutorial: quick startzukun
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysisEM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysiszukun
 
Object recognition with pictorial structures
Object recognition with pictorial structuresObject recognition with pictorial structures
Object recognition with pictorial structureszukun
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities zukun
 
Icml2012 learning hierarchies of invariant features
Icml2012 learning hierarchies of invariant featuresIcml2012 learning hierarchies of invariant features
Icml2012 learning hierarchies of invariant featureszukun
 
ECCV2010: Modeling Temporal Structure of Decomposable Motion Segments for Act...
ECCV2010: Modeling Temporal Structure of Decomposable Motion Segments for Act...ECCV2010: Modeling Temporal Structure of Decomposable Motion Segments for Act...
ECCV2010: Modeling Temporal Structure of Decomposable Motion Segments for Act...zukun
 

Mehr von zukun (20)

My lyn tutorial 2009
My lyn tutorial 2009My lyn tutorial 2009
My lyn tutorial 2009
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statisticsSiwei lyu: natural image statistics
Siwei lyu: natural image statistics
 
Lecture9 camera calibration
Lecture9 camera calibrationLecture9 camera calibration
Lecture9 camera calibration
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionBrunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer vision
 
Modern features-part-4-evaluation
Modern features-part-4-evaluationModern features-part-4-evaluation
Modern features-part-4-evaluation
 
Modern features-part-3-software
Modern features-part-3-softwareModern features-part-3-software
Modern features-part-3-software
 
Modern features-part-2-descriptors
Modern features-part-2-descriptorsModern features-part-2-descriptors
Modern features-part-2-descriptors
 
Modern features-part-1-detectors
Modern features-part-1-detectorsModern features-part-1-detectors
Modern features-part-1-detectors
 
Modern features-part-0-intro
Modern features-part-0-introModern features-part-0-intro
Modern features-part-0-intro
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video search
 
Lecture 01 internet video search
Lecture 01 internet video searchLecture 01 internet video search
Lecture 01 internet video search
 
Lecture 03 internet video search
Lecture 03 internet video searchLecture 03 internet video search
Lecture 03 internet video search
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learningIcml2012 tutorial representation_learning
Icml2012 tutorial representation_learning
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionAdvances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer vision
 
Gephi tutorial: quick start
Gephi tutorial: quick startGephi tutorial: quick start
Gephi tutorial: quick start
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysisEM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysis
 
Object recognition with pictorial structures
Object recognition with pictorial structuresObject recognition with pictorial structures
Object recognition with pictorial structures
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities
 
Icml2012 learning hierarchies of invariant features
Icml2012 learning hierarchies of invariant featuresIcml2012 learning hierarchies of invariant features
Icml2012 learning hierarchies of invariant features
 
ECCV2010: Modeling Temporal Structure of Decomposable Motion Segments for Act...
ECCV2010: Modeling Temporal Structure of Decomposable Motion Segments for Act...ECCV2010: Modeling Temporal Structure of Decomposable Motion Segments for Act...
ECCV2010: Modeling Temporal Structure of Decomposable Motion Segments for Act...
 

Cvpr2010 open source vision software, intro and training part-iii introduction to gil , boost and generic programming - jin - unknown - 2010

  • 1. Introduction to GIL, Boost and Generic Programming Hailin Jin Advanced Technology Labs Adobe Systems Incorporated http://www.adobe.com/technology/people/sanjose/jin.html 1 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 2. Outline  GIL  Boost  Generic programming  STL  Summary 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 3. What is GIL?  Generic Image Library  C++ image library  Open-source  http://opensource.adobe.com/gil  Well documented  The core library only contains header files; No need to link anything  Thread-safe  Compatible with most C++ compilers (Visual Studio, gcc, etc) 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 4. GIL overview  Image containers  1-D iterators for traversing rows and columns  2-D iterators (locator) for arbitrary 2D traversal  Image views  Image processing algorithms  Extensions  io extension for reading and writing images of popular formats (PNG, JPEG, TIFF, …) rgb8_image_t im; read_png_image("test.png", im);  numeric extension for convolutions against fixed and variable-size kernels kernel_1d_fixed<float, 7> kernel; gray8_image_t im_in, im_out; convolve_rows_fixed<gray32f_pixel_t>(const_view(im_in), kernel, view(im_out)); 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 5. People Lubomir Bourdev Hailin Jin Christian Henning Adobe Adobe Independent 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 6. GIL history  Started as an Adobe internal project in 2005  Version 1.0 in June 2006  Accepted into Boost in November 2006  Version 2.0 in March 2007  Constantly updated and patched with Boost releases 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 7. License  Initially: MIT license  Currently: Boost Software License (similar to the MIT license)  Free to use, reproduce, display, distribute, execute, and transmit, including derivative works  We retain the copyright 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 8. Image processing libraries  Your own image libraries  OpenCV  CImg  Vigra  GIL works third-party algorithms, libraries, etc 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 9. http://www.boost.org  High-quality C++ libraries  Open-source  Started in 1998  Latest version: 1.43.0 on May 6, 2010  Strict review system  10 of the Boost libraries are included in the C++ Standard Library TR  “It is usually a really dumb idea to go and reinvent a wheel that Boost already offers. ” — Bjarne Stroustrup, inventor of C++ 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 10. Boost usage  http://www.boost.org/users/uses.html  Adobe  Google  SAP  McAFee  Real Networks  … 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 11. Boost components for computer vision  Image (GIL)  Graph (BGL)  Thread  Filesystem  uBLAS  Program options  Math related libraries 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 12. Boost Graph Library  Shortest paths  Dijkstra's shortest paths  Bellman-Ford shortest paths  Johnson's all-Pairs shortest paths  Minimum spanning trees  Kruskal's minimum spanning tree  Prim's minimum spanning tree  Connected components  Connected components  Strongly connected components  Maximum flow  Edmonds Karp  Push-relabel  Kolmogorov  Topological sort, sparse matrix ordering, and many more 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 13. Outline  GIL  Boost  Generic programming  STL  Summary 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 14. GIL design principles  Followed the paradigm of generic programming  Clean separation between algorithms and containers  Work with third-party algorithms, data structures, and libraries  Toward optimal performance 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 15. Generic programming  Programming paradigm for developing efficient, reusable software code  Pioneered by Alexander Stepanov and David Musser  First successful example is the Standard Template Library (STL) which is now part of the C++ standard  Programming based on parameterization  Parameterize a data structure with different types (example: a std::vector<> with its element types)  Parameterize an algorithm with different types and algorithms (example: std::sort() function for int* and float* and with a comparison function)  Generalize an algorithm or data structure to its most general and useful form  Better code  Fewer bugs 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 16. Polymorphism  Allows values of different data types to be handled using a uniform interface  Object-oriented programming (OOP)  Ad-hoc polymorphism  Class hierarchies: specified inheritance  Function/method overloading: different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations  Dynamic dispatch  Generic programming  Parametric polymorphism (data structure and algorithms)  Code is written without mention of any specific type and thus can be used transparently with any number of new types  Static (compile-time) dispatch 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 17. Generic programming: a case study  Compute the sum of a set of data 17 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 18. Object-oriented style class int_vector { private: int* array; int n; ... public: ... int accumulate() { int result = 0; for (int i = 0; i < n; ++i) result = result + array[i]; return result; } ... }; 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 19. Separate the algorithm from the container int accumulate(int* array, int n) { int result = 0; for (int i = 0; i < n; ++i) result = result + array[i]; return result; } class int_vector { private: int* array; int n; ... public: ... int size() {return n;} int* begin() {return array;} ... }; int_vector a_vec; int result = accumulate(a_vec.begin(), a_vec.size()); 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 20. Same algorithm with different types int accumulate(int* array, int n) { int result = 0; for (int i = 0; i < n; ++i) result = result + array[i]; return result; } float accumulate(float* array, int n) { float result = 0; for (int i = 0; i < n; ++i) result = result + array[i]; return result; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 21. C++ templates template <typename T> T accumulate(T* array, int n) { T result = 0; for (int i = 0; i < n; ++i) result = result + array[i]; return result; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 22. String concatenation template <typename T> T accumulate(T* array, int n) { T result = 0; for (int i = 0; i < n; ++i) result = result + array[i]; return result; } std::string concatenate(std::string* array, int n) { std::string result = ””; for (int i = 0; i < n; ++i) result = result + array[i]; return result; } template <typename T> T accumulate(T* array, int n) { T result = T(); T result; for (int i = 0; i < n; ++i) result = result + array[i]; return result; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 23. Generalize the interface and type requirements template <typename T> T accumulate(T* array, int n, T init) { for (int i = 0; i < n; ++i) init = init + array[i]; return init; } Type requirements on T • T must have a copy constructor T(const T&) • T must have an assignment operator=() • T must have a binary operator+(,) 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 24. Linked list template <typename T> struct list_node { list_node<T>* next; T value; }; template <typename T> T accumulate(list_node<T>* first, T init) { while (first!=0) { init = init + first->value; first = first->next; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 25. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* array, int n, T init) { list_node<T>* next; for (int i = 0; i < n; ++i) T value; init = init + array[i]; }; return init; } template <typename T> T accumulate(list_node<T>* first, T init) { while (first!=0) { init = init + first->value; first = first->next; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 26. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* first, int n, T init) { list_node<T>* next; T* last = first + n; T value; for (;first!=last;++first) { }; init = init + *first; } template <typename T> return init; T accumulate(list_node<T>* first, } T init) { while (first!=0) { init = init + first->value; first = first->next; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 27. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* first, int n, T init) { list_node<T>* next; T* last = first + n; T value; while (first!=last) { }; init = init + *first; ++first; template <typename T> } T accumulate(list_node<T>* first, return init; T init) { } while (first!=0) { init = init + first->value; first = first->next; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 28. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* first, T* last, T init) { list_node<T>* next; while (first!=last) { T value; init = init + *first; }; ++first; } template <typename T> return init; T accumulate(list_node<T>* first, } T init) { while (first!=0) { init = init + first->value; first = first->next; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 29. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* first, T* last, T init) { list_node<T>* next; while (first!=last) { T value; init = init + *first; }; ++first; } template <typename T> return init; struct list_iterator { } list_node<T>* node; }; template <typename T> T accumulate(list_iterator<T> first, T init) { while (first.node!=0) { init = init + first.node->value; first.node = first.node->next; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 30. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* first, T* last, T init) { list_node<T>* next; while (first!=last) { T value; init = init + *first; }; ++first; } template <typename T> return init; struct list_iterator { } list_node<T>* node; }; template <typename T> T accumulate(list_iterator<T> first, list_iterator<T> last, T init) { while (first!=last) { init = init + first.node->value; first.node = first.node->next; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 31. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* first, T* last, T init) { list_node<T>* next; while (first!=last) { T value; init = init + *first; }; ++first; } template <typename T> return init; struct list_iterator { } list_node<T>* node; template <typename T> }; bool operator!=(list_iterator<T> x, list_iterator<T> y) { template <typename T> return x.node!=y.node; T accumulate(list_iterator<T> first, } list_iterator<T> last, T init) { while (first!=last) { init = init + first.node->value; first.node = first.node->next; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 32. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* first, T* last, T init) { list_node<T>* next; while (first!=last) { T value; init = init + *first; }; ++first; } template <typename T> return init; struct list_iterator { } list_node<T>* node; template <typename T> }; bool operator!=(list_iterator<T> x, list_iterator<T> y) { template <typename T> return x.node!=y.node; T accumulate(list_iterator<T> first, } list_iterator<T> last, T init) { template <typename T> while (first!=last) { T operator*(list_iterator<T> x) { init = init + first.node->value; return x.node->value; first.node = first.node->next; } } return init; template <typename T> } list_iterator<T>& operator++(list_iterator<T>& x) { x.node = x.node->next; return x; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 33. Unifying linked lists and arrays template <typename T> template <typename T> struct list_node { T accumulate(T* first, T* last, T init) { list_node<T>* next; while (first!=last) { T value; init = init + *first; }; ++first; } template <typename T> return init; struct list_iterator { } list_node<T>* node; template <typename T> }; bool operator!=(list_iterator<T> x, list_iterator<T> y) { template <typename T> return x.node!=y.node; T accumulate(list_iterator<T> first, } list_iterator<T> last, T init) { template <typename T> while (first!=last) { T operator*(list_iterator<T> x) { init = init + *first; return x.node->value; ++first; } } return init; template <typename T> } list_iterator<T>& operator++(list_iterator<T>& x) { x.node = x.node->next; return x; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 34. Unified version template <typename I, typename T> T accumulate(I first, I last, T init) { while (first!=last) { init = init + *first; ++first; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 35. Type requirements template <typename I, typename T> T accumulate(I first, I last, T init) { while (first!=last) { init = init + *first; ++first; } return init; } • I must have a copy constructor I(const I&) • I must have a binary operator!=(,) • I must have an operator*() • I must have an operator++() • T must have a copy constructor T(const T&) • T must have an assignment operator=() • T must have a binary operator+(,) 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 36. Further generalization with function objects template <typename I, typename T> T accumulate(I first, I last, T init) { while (first!=last) { init = init + *first; ++first; } return init; } template <typename I, typename T, typename B> T accumulate(I first, I last, T init, B op) { while (first!=last) { init = op(init, *first); ++first; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 37. Type requirements template <typename I, typename T, typename B> T accumulate(I first, I last, T init, B op) { while (first!=last) { init = op(init, *first); ++first; } • I must have a copy constructor I(const I&) return init; } • I must have a binary operator!=(,) • I must have an operator*() • I must have an operator++() • T must have a copy constructor T(const T&) • T must have an assignment operator=() • B must have a copy constructor B(const B&) • B must have a binary application operator()(,) 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 38. Function object example template <typename T> struct moment_2 { T m1, m2; moment_2(T m1_, T m2_) : m1(m1_), m2(m2_) {} }; template <typename T> moment_2<T> compute_moment_2(T* first, T* last, moment_2<T> init) { while (first!=last) { init.m1 += *first; init.m2 += *first**first; ++first; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 39. Function object example template <typename T> struct compute_moment_2_s { moment_2<T> operator()(moment_2<T> m, T x) const { m.m1 += x; m.m2 += x*x; return m; } }; template <typename T> moment_2<T> compute_moment_2(T* first, T* last, moment_2<T> init) { return accumulate(first, last, init, compute_moment_2_s<T>()); } template <typename T> struct max_s { T operator()(T x, T y) const {return x>y?x:y;} }; template <typename T> T range_max(T* first, T* last, T init) { return accumulate(first, last, init, max_s()); } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 40. Lessons learned: #1  Separate between an algorithms and a data structure int accumulate(int* array, int n) { int result = 0; for (int i = 0; i < n; ++i) result = result + array[i]; class int_vector { return result; private: } int* array; int n; class int_vector { ... private: public: int* array; ... int n; int accumulate() { ... int result = 0; public: for (int i = 0; i < n; ++i) ... result = result + array[i]; int size() {return n;} int* begin() {return array;} return result; ... } }; ... }; int_vector a_vec; int result = accumulate(a_vec.begin() 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 41. Lessons learned: #2  Generalize an algorithm to a general form int accumulate(int* array, int n) { int result = 0; for (int i = 0; i < n; ++i) result = result + array[i]; template <typename T> return result; T accumulate(T* array, int n) { } T result = T(); for (int i = 0; i < n; ++i) float accumulate(float* array, int n) { result = result + array[i]; float result = 0; return result; for (int i = 0; i < n; ++i) } result = result + array[i]; return result; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 42. Lessons learned: #3  Generalize the interface of an algorithm template <typename T> T accumulate(T* array, int n) { T result = T(); for (int i = 0; i < n; ++i) result = result + array[i]; return result; } template <typename T> T accumulate(T* array, int n, T init) { for (int i = 0; i < n; ++i) init = init + array[i]; return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 43. Lessons learned: #4  Generalize through iterators template <typename T> T accumulate(list_iterator<T> first, T init) { while (first!=0) { init = init + first.node->value; template <typename I, first.node = first.node->next; typename T> } T accumulate(I first, I last, T init) { return init; while (first!=last) { } init = init + *first; ++first; template <typename T> } T accumulate(T* array, int n, T init) { return init; for (int i = 0; i < n; ++i) } init = init + array[i]; return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 44. Lessons learned: #5  Generalize through function objects template <typename I, typename T> T accumulate(I first, I last, T init) { while (first!=last) { init = init + *first; ++first; } return init; } template <typename I, typename T, typename B> T accumulate(I first, I last, T init, B op) { while (first!=last) { init = op(init, *first); ++first; } return init; } 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 45. Concepts/Models  Type requirements  Concept: description of requirements on one or more types stated in terms of the existence and properties of procedures, type attributes, and type functions defined on the types  Model: satisfies all the requirements of a concept  Example Concept • T must have a copy constructor T(const T&) template <typename T> T accumulate(T* array, int n, T init) { • T must have an assignment operator=() for (int i = 0; i < n; ++i) • T must have a binary operator+(,) init = init + array[i]; return init; Model } • int, float, std::string, …  Concepts are not designed or invented. Instead they are discovered 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 46. Iterators  An interface between sequential data structures and algorithms  Separates containers from algorithms  Models: raw points and linked list iterators template <typename I, typename T> T accumulate(I first, I last, T init) { while (first!=last) { init = init + *first; ++first; } return init; } • I must have a copy constructor I(const I&) • I must have a binary operator!=(,) • I must have operator*() • I must have operator++() 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 47. More models of iterators template <typename T> template <typename T> class step_pointer { class indirect_pointer { privte: privte: T* x; T* x; int step; int* y; public: public: ... ... T operator*() {return *x;} T operator*() {return x[*y];} step_pointer& operator++() { step_pointer& operator++() { x += step; return *this; ++y; return *this; } } ... ... }; }; template <typename T> class value_iterator { privte: T x; public: ... T operator*() {return x;} step_pointer& operator++() { ++x; return *this; } ... }; 47 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 48. Compilers are working hard for us  C++ compilers resolves all templates at compile time  No dynamic (run-time) dispatch  Function objects can be inlined; No function calls overhead 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 49. STL  Standard Template Library  http://www.sgi.com/tech/stl  First widespread application of generic programming  Part of the C++ standard Containers Iterators Algorithms 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 50. STL  Containers  vector, list, slist  set, map, multiset, multimap  Algorithms  find(), for_each(), fill(), transform(), copy()  partition(), stable_partition()  sort(), lower_bound(), nth_element(), merge()  set_difference(), set_union(), set_intersection()  accumulate(), inner_product(), partial_sum() 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 51. Summary  GIL  Boost  Generic programming  STL  Code reuse  Better understanding of algorithms  Better code 2010 Adobe Systems Incorporated. All Rights Reserved.
  • 52. Book recommendation  For those who desire a deep understanding of programming 52 2010 Adobe Systems Incorporated. All Rights Reserved.