c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (1)
Adaptable component frameworks: Using vector from the C ++ standard - - PowerPoint PPT Presentation
Adaptable component frameworks: Using vector from the C ++ standard library as an example Bo Simonsen University of Copenhagen Joint work with Jyrki Katajainen These slides are available at http://cphstl.dk Performance Engineering
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (1)
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (2)
Component frameworks A skeleton of a software component which is to be filled with implemen- tation specific details. Generic component frameworks A component framework where the user provides the implementation specific details in form of policies. Vector framework A vector container stores a sequence
indices or iterators at constant cost.
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (3)
container data structure vector dynamic array
container data structures extensions vector dynamic array hashed array tree levelwise- allocated pile no extension
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (4)
1 2 3 3 3 2 2 1 1 7 3 6 2 5 1 4 11 3 10 2 9 1 8 15 3 14 2 13 1 12
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (5)
1 3 1 2
insert(begin()++, 2)
1 2 1 3 2
2 3 1 2
insert(begin(), 1) rollback is not necessarily possible
1 2 1 3 2
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (6)
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (7)
Factorizes container
Realizes a minimal implementation
a data structure: grow, shrink, access. Encapsulates a value in a small object. This
can either be allocated
stored directly in the array. Provides a proxy for the kernel to ensure swap does not invalidate iterators. Framework Kernel i Surrogate pointer v i Encapsulator Iterator Surrogate
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (8)
Elements stored directly Elements stored indirectly Elements stored doubly indirectly v . . . . . . . . . . . .
. . . . . .
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (9)
vector framework value type kernel encapsulator int float dynamic array hashed array tree direct encapsulator indirect encapsulator
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (10)
20 40 60 80 100 120 140 160 180 200 217 218 219 220 221 222 223 Execution time per operation [in nanoseconds] Number of operations dynamic array, doubly indirect encapsulation levelwise−allocated pile, indirect encapsulation hashed array tree, indirect encapsulation dynamic array, indirect encapsulation levelwise−allocated pile, direct encapsulation dynamic array, direct encapsulation hashed array tree, direct encapsulation std::vector
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (11)
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (12)
template arguments desirable properties component framework
highly
component
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (13)
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (14)
template <typename V , typename A> class encapsulator_selector { public : typedef cphstl : : direct_encapsulator<V , A> E ; typedef cphstl : : indirect_encapsulator<V , A> F ; typedef typename cphstl : : if_then_else<std : : tr1 : : is_class<V >:: value , F , E >:: type type ;
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (15)
template <typename V , typename A , typename K> void vector_framework<V , A , K >:: block_copy_backward ( size_type start , size_type end , size_type s) { for ( size_type i = end + s − 1; i ≥ start + s ;
−−i) {
slot_swap ((∗ this ). k . access (i ) , (∗ this ). k . access (i − s ));
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (16)
template arguments policies component framework
highly customized component
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (17)
#include <stdexcept> // defines std : : domain error #include <stl−vector . h+
+
cphstl : : vector class my_class { public : my_class ( int const& a) {
my_class const& operator=(my_class const&) { throw std : : domain_error (” . . . ”);
int main () { cphstl : : vector<my_class> v ; v . insert (v . begin () , my_class (5)); v[0] = my class(6);
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (18)
typedef int V ; typedef std : : allocator<V> A ; typedef cphstl : : direct_encapsulator<V , A> E ; typedef cphstl : : dynamic_array<V , A , E> K ; typedef cphstl : : vector_framework<V , A , K> R ; typedef cphstl : : rank_iterator<R , false> I ; typedef cphstl : : rank_iterator<R , true> J ; typedef cphstl : : vector<V , A , R , I , J> C ;
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (19)
typedef cphstl : : vector!<V=int , R=cphstl : : vector_framework!< K=cphstl : : dynamic_array!< E=cphstl : : direct_encapsulator !> !>, I=cphstl : : rank_iterator!<is_const=false !>, J=cphstl : : rank_iterator!<is_const=true!> !> C ;
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (20)
c
Performance Engineering Laboratory
Workshop on Generic Programming, August 2009 (21)