Better Code: Data Structures Sean Parent | Principal Scientist
Better Code: Data StructuresSean Parent | Principal Scientist
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Better Code
2
! Regular Types ! Goal: Implement Complete and Efficient Types
! Algorithms ! Goal: No Raw Loops
! Data Structures ! Goal: No Incidental Data Structures
! Runtime Polymorphism ! Goal: No Raw Pointers
! Concurrency ! Goal: No Raw Synchronization Primitives
! …
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Better Code
2
! Regular Types ! Goal: Implement Complete and Efficient Types
! Algorithms ! Goal: No Raw Loops
! Data Structures ! Goal: No Incidental Data Structures
! Runtime Polymorphism ! Goal: No Raw Pointers
! Concurrency ! Goal: No Raw Synchronization Primitives
! …
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Goal: No incidental data structures
3
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is an incidental data structure?
4
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a data structure?
5
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a data structure?
5
Definition: A data structure is a format for organizing and storing data.
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a structure?
6
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a structure?
6
Definition: A structure on a set consists of additional entities that, in some manner, relate to the set, endowing the
collection with meaning or significance.
0100
0100
! !
! !
0100
0100
4
0100
0100
[-8..7]
0100
[-8..7]
0011
01000011
4>3
01000011
hash( ) != hash( )
01000011
hash( ) != hash( )
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
01000011
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
0100
0100
0011
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
01000011
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
01000011
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
01000011
<
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
01000011
<
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
18
00110100
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
18
00110100
+
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
18
00110100
+0111
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
19
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected
19
Part A
Part B
Part C
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected! Noncircular
19
Part A
Part B
Part C
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected! Noncircular
19
Part A
Part B
Part C
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected! Noncircular! Logically Disjoint
19
Part A
Part B
Part C
Part A’
Part B’
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected! Noncircular! Logically Disjoint
19
Part A
Part B
Part C
Part A’
Part B’
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected! Noncircular! Logically Disjoint! Owning
19
Part A
Part B
Part C
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected! Noncircular! Logically Disjoint! Owning
19
Part A
Part B
Part C
Part A
Part B
Part C
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected! Noncircular! Logically Disjoint! Owning
19
Part A
Part B
Part C
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
! Connected! Noncircular! Logically Disjoint! Owning
! Standard Containers are Composite Objects
19
Part A
Part B
Part C
Elements of Programming, Chapter 12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 20
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a data structure?
20
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a data structure?
Definition: A structure utilizing value, physical, and representational relationships to encode semantic
relationships on a collection of objects.
20
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a data structure?
Definition: A structure utilizing value, physical, and representational relationships to encode semantic
relationships on a collection of objects.
The choice of encoding can make a dramatic difference on the performance of operations.
20
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Data Structure Performance
21
3GHz processor, from Chandler Carruth talk - Credit to Jeff Dean
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Data Structure Performance
! Hierarchical Memory Structure ! Register Access 0.1 ns ! L1 Cache 0.5 ns ! L2 Cache 7.0 ns ! Memory 100.0 ns
21
3GHz processor, from Chandler Carruth talk - Credit to Jeff Dean
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Data Structure Performance
! Hierarchical Memory Structure ! Register Access 0.1 ns ! L1 Cache 0.5 ns ! L2 Cache 7.0 ns ! Memory 100.0 ns
! RAM behaves much like a disk drive
21
3GHz processor, from Chandler Carruth talk - Credit to Jeff Dean
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Data Structure Performance
! Hierarchical Memory Structure ! Register Access 0.1 ns ! L1 Cache 0.5 ns ! L2 Cache 7.0 ns ! Memory 100.0 ns
! RAM behaves much like a disk drive
21
3GHz processor, from Chandler Carruth talk - Credit to Jeff Dean
log2 1,000,000,000,000 = 40
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Data Structure Performance
! Locality matters - use arrays or vector ! Parallel Arrays ! Static Lookup Tables ! Closed Hash Maps ! Algorithms
22
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Example: Parallel Array & Algorithms
23
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
24
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
24
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
25
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
26
m
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
26
m
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
26
m
l
f
stable_partition(f, m, p)
stable_partition(m, l, p)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
27
m
stable_partition(f, m, p)
stable_partition(m, l, p)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
27
m
stable_partition(f, m, p)
stable_partition(m, l, p)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
27
m
rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
28
rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
28
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
29
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
r
fr
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
29
if (n == 1) return f + p(*f);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
r
fr
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
30
if (n == 1) return f + p(*f);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
l
f
template <typename I, typename P> auto stable_partition(I f, I l, P p) -> I { auto n = l - f; if (n == 0) return f; if (n == 1) return f + p(*f); auto m = f + (n / 2);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p)); }
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
30
l
f
template <typename I, typename P> auto stable_partition(I f, I l, P p) -> I { auto n = l - f; if (n == 0) return f; if (n == 1) return f + p(*f); auto m = f + (n / 2);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p)); }
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
30
l
f
template <typename I, typename P> auto stable_partition(I f, I l, P p) -> I { auto n = l - f; if (n == 0) return f; if (n == 1) return f + p(*f); auto m = f + (n / 2);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p)); }
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
30
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
31
template <typename I, typename P> auto stable_partition(I f, I l, P p) -> I { auto n = l - f; if (n == 0) return f; if (n == 1) return f + p(*f); auto m = f + (n / 2);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p)); }
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
31
template <typename I, typename P> auto stable_partition_position(I f, I l, P p) -> I { auto n = l - f; if (n == 0) return f; if (n == 1) return f + p(f); auto m = f + (n / 2);
return rotate(stable_partition_position(f, m, p), m, stable_partition_position(m, l, p)); }
l
f
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
32
int a[] = { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 }; bool b[] = { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };
auto p = stable_partition_position(begin(a), end(a), [&](auto i) { return *(begin(b) + (i - begin(a))); });
for (auto f = begin(a), l = p; f != l; ++f) cout << *f << " "; cout << "^ "; for (auto f = p, l = end(a); f != l; ++f) cout << *f << " "; cout << endl;
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
32
int a[] = { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 }; bool b[] = { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };
auto p = stable_partition_position(begin(a), end(a), [&](auto i) { return *(begin(b) + (i - begin(a))); });
for (auto f = begin(a), l = p; f != l; ++f) cout << *f << " "; cout << "^ "; for (auto f = p, l = end(a); f != l; ++f) cout << *f << " "; cout << endl;
2 4 4 2 ^ 1 3 5 5 3 1
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Example: Algorithms & Minimal Work
33
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
34
41312795151421161610183
sf
sl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
35
sf
sl
12345678910111213141516
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
36
sf
sl
XXXXX6789XXXXXXX
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
37
41312795151421161610183
f
l
sf
sl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
38
41312795151421161610183
f
l
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
38
41312795151421161610183
f
l
sf nth_element(f, sf, l);
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
39
f
l
sf nth_element(f, sf, l);
21345671412159161013811
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
40
nth_element(f, sf, l);
21345671412159161013811
f
l
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
40
nth_element(f, sf, l);
21345671412159161013811
f
l
sf
≤ *sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
40
nth_element(f, sf, l);
21345671412159161013811
f
l
sf
≤ *sf
≥ *sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
41
nth_element(f, sf, l);
21345671412159161013811
f
l
sl
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
42
nth_element(f, sf, l); ++sf;
21345671412159161013811
f
l
sl
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
42
nth_element(f, sf, l); ++sf;
21345671412159161013811
f
l
sl
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
43
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
21345671412159161013811
f
l
sl
sf
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
44
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
f
l
sl
sf
21345678915141612131011
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
45
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
f
l
sl
sf
21345678915141612131011
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
46
if (sf == sl) return;
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
47
if (sf == sl) return; if (sf != f) { nth_element(f, sf, l); ++sf; } partial_sort(sf, sl, l);
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
48
template <typename I> // I models RandomAccessIterator void sort_subrange(I f, I l, I sf, I sl) { if (sf == sl) return; if (sf != f) { nth_element(f, sf, l); ++sf; } partial_sort(sf, sl, l); }
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
49
sort_subrange(f, l, sf, sl); f
l
sl
sf
sl
41312795151421161610183
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
50
sort_subrange(f, l, sf, sl); f
l
sl
21345678915141612131011
sf
sl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
50
sort_subrange(f, l, sf, sl); f
l
sl
21345678915141612131011
sf
sl
nl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
51
sort_subrange(f, l, sf, sl); partial_sort(sl, nl, l);
f
l
sl
21345678915141612131011
sf
nl
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
52
sort_subrange(f, l, sf, sl); partial_sort(sl, nl, l);
f
l
sl
sf
nl
21345678910111216151413
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is an incidental data structure?
53
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is an incidental data structure?
53
Definition: An incidental data structure is a data structure that occurs within a system when there is no object representing
the structure as a whole.
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is an incidental data structure?
53
Definition: An incidental data structure is a data structure that occurs within a system when there is no object representing
the structure as a whole.
Structures formed in the absence of a whole/part relationship
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Why no incidental data structures?
54
! They cause ambiguities and break our ability to reason about code locally
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
55
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Delegates
55
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Delegates
! Message handlers
55
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
56
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Self-referential interface
56
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Self-referential interface
class UIElement { };
class UIElementCollection { public: void Add(shared_ptr<UIElement>);};
class Panel : public UIElement { public: shared_ptr<UIElementCollection> Children() const;};
56
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Self-referential interface
class UIElement { };
class UIElementCollection { public: void Add(shared_ptr<UIElement>);};
class Panel : public UIElement { public: shared_ptr<UIElementCollection> Children() const;};
panel->Children()->Add(element);
56
Panel
Element
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Self-referential interface
class UIElement { };
class UIElementCollection { public: void Add(shared_ptr<UIElement>);};
class Panel : public UIElement { public: shared_ptr<UIElementCollection> Children() const;};
panel->Children()->Add(element);panel->Children()->Add(element);
56
Panel
Element
Panel
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Self-referential interface
class UIElement { };
class UIElementCollection { public: void Add(shared_ptr<UIElement>);};
class Panel : public UIElement { public: shared_ptr<UIElementCollection> Children() const;};
panel->Children()->Add(element);panel->Children()->Add(element);panel2->Children()->Add(element);
56
Panel
Element
Panel Panel2
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Self-referential interface
class UIElement { };
class UIElementCollection { public: void Add(shared_ptr<UIElement>);};
class Panel : public UIElement { public: shared_ptr<UIElementCollection> Children() const;};
panel->Children()->Add(element);panel->Children()->Add(element);panel2->Children()->Add(element);panel->Children()->Add(panel);
56
Panel
Element
PanelPanel Panel2
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Self-referential interface
class UIElement { };
class UIElementCollection { public: void Add(shared_ptr<UIElement>);};
class Panel : public UIElement { public: shared_ptr<UIElementCollection> Children() const;};
panel->Children()->Add(element);panel->Children()->Add(element);panel2->Children()->Add(element);panel->Children()->Add(panel);
56
Panel
Element
PanelPanel
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
57
A
forest
E
B C D
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
58
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
59
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
60
A
list
B
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
61
A
forest
E
B C D
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
62
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
63
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
64
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
65
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
66
A
forest
E
B C D
end()begin()
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
67
leading
trailing
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
68
A
forest
E
B C D
end()begin()
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
69
forest<string> f;
f.insert(end(f), "A"); f.insert(end(f), "E");
auto a = trailing_of(begin(f)); f.insert(a, "B"); f.insert(a, "C"); f.insert(a, "D");
A
forest
E
B C D
end()begin()
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Conclusions
70
! Understand the structures created by relationships ! Encapsulate structure invariants in composite types ! Learn to use the tools at your disposal ! And how to create new ones
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 71
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
No incidental data structures
71
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
No incidental data structures
Composite Types
71
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
No incidental data structures
Composite Types
Better Code
71
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.