Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement...
Transcript of Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement...
![Page 1: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/1.jpg)
Better Code: Data StructuresSean Parent | Principal Scientist
![Page 2: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/2.jpg)
© 2015 Adobe Systems Incorporated.
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
! …
![Page 3: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/3.jpg)
© 2015 Adobe Systems Incorporated.
Goal: No incidental data structures
3
![Page 4: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/4.jpg)
© 2015 Adobe Systems Incorporated.
What is an incidental data structure?
4
![Page 5: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/5.jpg)
© 2015 Adobe Systems Incorporated.
What is a data structure?
5
Definition: A data structure is a format for organizing and storing data.
![Page 6: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/6.jpg)
© 2015 Adobe Systems Incorporated.
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.
![Page 7: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/7.jpg)
[ This slide intentionally left void ]
![Page 8: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/8.jpg)
0100
![Page 9: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/9.jpg)
0100
! !
! !
![Page 10: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/10.jpg)
0100
![Page 11: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/11.jpg)
0100
4
![Page 12: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/12.jpg)
0100
![Page 13: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/13.jpg)
0100
[-8..7]
![Page 14: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/14.jpg)
0100
[-8..7]
0011
![Page 15: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/15.jpg)
01000011
4>3
![Page 16: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/16.jpg)
01000011
hash( ) != hash( )
![Page 17: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/17.jpg)
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
0100
0100
0011
<
![Page 18: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/18.jpg)
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved.
Memory Space
18
00110100
+0111
![Page 19: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/19.jpg)
© 2015 Adobe Systems Incorporated.
Whole-Part Relationships and Composite Objects
! Connected
! Noncircular
! Logically Disjoint
! Owning
! Standard Containers are Composite Objects
19
Part A
Part B
Part C
Part A
Part B
Part C
Part A
Part B
Part C
Part A’
Part B’
Part A
Part B
Part C
Part A
Part B
Part C
Elements of Programming, Chapter 12
![Page 20: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/20.jpg)
© 2015 Adobe Systems Incorporated.
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
![Page 21: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/21.jpg)
© 2015 Adobe Systems Incorporated.
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
![Page 22: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/22.jpg)
© 2015 Adobe Systems Incorporated.
Data Structure Performance
! Locality matters - use arrays or vector
! Parallel Arrays
! Static Lookup Tables
! Closed Hash Maps
! Algorithms
22
![Page 23: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/23.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Example: Parallel Array & Algorithms
23
![Page 24: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/24.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
24
![Page 25: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/25.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
25
![Page 26: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/26.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
26
p
l
stable_partition(p, l, s)
![Page 27: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/27.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
27
p
f
stable_partition(f, p, not1(s))
![Page 28: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/28.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
28
stable_partition(f, p, not1(s)) stable_partition(p, l, s)
p
l
f
![Page 29: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/29.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
29
stable_partition(f, p, not1(s)) stable_partition(p, l, s)
![Page 30: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/30.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
30
return { stable_partition(f, p, not1(s)), stable_partition(p, l, s) };
![Page 31: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/31.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
31
template <typename I, // I models BidirectionalIterator typename S> // S models UnaryPredicate auto gather(I f, I l, I p, S s) -> pair<I, I> { return { stable_partition(f, p, not1(s)), stable_partition(p, l, s) }; }
![Page 32: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/32.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Gather
32
template <typename I, // I models BidirectionalIterator typename S> // S models UnaryPredicate auto gather(I f, I l, I p, S s) -> pair<I, I> { return { stable_partition(f, p, not1(s)), stable_partition(p, l, s) }; }
p
l
f
![Page 33: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/33.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
33
l
f
![Page 34: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/34.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
34
l
f
![Page 35: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/35.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
35
m
l
f
stable_partition(f, m, p)
stable_partition(m, l, p)
![Page 36: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/36.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
36
m
rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
stable_partition(f, m, p)
stable_partition(m, l, p)
![Page 37: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/37.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
37
rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
![Page 38: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/38.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
38
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
if (n == 1) return f + p(*f);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
r
fr
![Page 39: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/39.jpg)
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
39
if (n == 1) return f + p(*f);
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
l
f
![Page 40: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/40.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
40
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)); }
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
![Page 41: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/41.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
41
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
![Page 42: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/42.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Example: Algorithms & Minimal Work
42
![Page 43: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/43.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
43
41312795151421161610183
sf
sl
![Page 44: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/44.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
44
sf
sl
12345678910111213141516
![Page 45: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/45.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
45
sf
sl
XXXXX6789XXXXXXX
![Page 46: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/46.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
46
41312795151421161610183
f
l
sf
sl
![Page 47: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/47.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
47
41312795151421161610183
f
l
sf nth_element(f, sf, l);
![Page 48: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/48.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
48
f
l
sf nth_element(f, sf, l);
21345671412159161013811
![Page 49: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/49.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
49
nth_element(f, sf, l);
21345671412159161013811
f
l
sf
≤ *sf
≥ *sf
![Page 50: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/50.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
50
nth_element(f, sf, l);
21345671412159161013811
f
l
sl
sf
![Page 51: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/51.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
51
nth_element(f, sf, l); ++sf;
21345671412159161013811
f
l
sl
sf
![Page 52: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/52.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
52
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
21345671412159161013811
f
l
sl
sf
![Page 53: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/53.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
53
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
f
l
sl
sf
21345678915141612131011
![Page 54: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/54.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
54
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
f
l
sl
sf
21345678915141612131011
![Page 55: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/55.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
55
if (sf == sl) return;
nth_element(f, sf, l); ++sf;
partial_sort(sf, sl, l);
![Page 56: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/56.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
56
if (sf == sl) return; if (sf != f) { nth_element(f, sf, l); ++sf; } partial_sort(sf, sl, l);
![Page 57: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/57.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
57
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); }
![Page 58: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/58.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
58
sort_subrange(f, l, sf, sl); f
l
sl
sf
sl
41312795151421161610183
![Page 59: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/59.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
59
sort_subrange(f, l, sf, sl); f
l
sl
21345678915141612131011
sf
sl
nl
![Page 60: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/60.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
60
sort_subrange(f, l, sf, sl); partial_sort(sl, nl, l);
f
l
sl
21345678915141612131011
sf
nl
![Page 61: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/61.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
61
sort_subrange(f, l, sf, sl); partial_sort(sl, nl, l);
f
l
sl
sf
nl
21345678910111216151413
![Page 62: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/62.jpg)
© 2015 Adobe Systems Incorporated.
What is an incidental data structure?
62
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
![Page 63: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/63.jpg)
© 2015 Adobe Systems Incorporated.
Why no incidental data structures?
63
! They cause ambiguities and break our ability to reason about code locally
![Page 64: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/64.jpg)
© 2015 Adobe Systems Incorporated.
Incidental Data Structures
! Delegates
! Message handlers
! Any pointer or reference stored in an object which refers to another object which is not a part
64
![Page 65: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/65.jpg)
© 2015 Adobe Systems Incorporated.
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);
65
Panel
Element
PanelPanel Panel2
![Page 66: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/66.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
66
A
forest
E
B C D
![Page 67: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/67.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
67
![Page 68: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/68.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
68
![Page 69: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/69.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
69
A
list
B
![Page 70: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/70.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
70
A
forest
E
B C D
![Page 71: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/71.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
71
![Page 72: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/72.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
72
![Page 73: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/73.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
73
![Page 74: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/74.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
74
![Page 75: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/75.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
75
![Page 76: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/76.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
76
A
forest
E
B C D
end()begin()
![Page 77: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/77.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
77
leading
trailing
![Page 78: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/78.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
78
A
forest
E
B C D
end()begin()
![Page 79: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/79.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
79
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()
![Page 80: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/80.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
80
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");
auto r = depth_range(f); for (auto f = begin(r), l = end(r); f != l; ++f) { cout << string(f.depth() * 4, ' ') << (f.edge() ? "<" : "</") << *f << “>\n"; }
<A> <B> </B> <C> </C> <D> </D> </A> <E> </E>
![Page 81: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/81.jpg)
© 2015 Adobe Systems Incorporated.
Conclusions
81
! 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
! Slides and code from talk: ! https://github.com/sean-parent/sean-parent.github.io/wiki/Papers-and-Presentations
! Forest library: ! https://github.com/stlab/adobe_source_libraries
![Page 82: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/82.jpg)
© 2015 Adobe Systems Incorporated.
No incidental data structures
Composite Types
Better Code
82
![Page 83: Better Code: Data Structures...2016/08/08 · Be!er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops ! Data Structures ! Goal:](https://reader034.fdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbd/html5/thumbnails/83.jpg)
© 2015 Adobe Systems Incorporated.