better code: data structures - stlab › ... › data-structures.pdf · be"er code 2! regular...
TRANSCRIPT
![Page 1: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/1.jpg)
Better Code: Data StructuresSean Parent | Principal Scientist
![Page 2: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/2.jpg)
© 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
! …
![Page 3: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/3.jpg)
© 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
! …
![Page 4: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/4.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Goal: No incidental data structures
3
![Page 5: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/5.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is an incidental data structure?
4
![Page 6: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/6.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a data structure?
5
![Page 7: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/7.jpg)
© 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.
![Page 8: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/8.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a structure?
6
![Page 9: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/9.jpg)
© 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.
![Page 10: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/10.jpg)
![Page 11: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/11.jpg)
![Page 12: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/12.jpg)
0100
![Page 13: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/13.jpg)
0100
! !
! !
![Page 14: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/14.jpg)
0100
![Page 15: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/15.jpg)
0100
4
![Page 16: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/16.jpg)
0100
![Page 17: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/17.jpg)
0100
[-8..7]
![Page 18: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/18.jpg)
0100
[-8..7]
0011
![Page 19: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/19.jpg)
01000011
4>3
![Page 20: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/20.jpg)
01000011
hash( ) != hash( )
![Page 21: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/21.jpg)
01000011
hash( ) != hash( )
![Page 22: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/22.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
01000011
![Page 23: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/23.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
0100
0100
0011
![Page 24: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/24.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
01000011
![Page 25: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/25.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
01000011
![Page 26: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/26.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001101001110010001011111100110011011100101000111111100111100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
01000011
<
![Page 27: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/27.jpg)
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
17
01000011
<
![Page 28: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/28.jpg)
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
18
00110100
![Page 29: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/29.jpg)
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
18
00110100
+
![Page 30: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/30.jpg)
110000101111011111110111101100110011001110001100101001111010011011010100100001001000110000101000110011000000111001010011010100100110010001101110011100001010011111011101000111001111000001100111110010001011111100110011011100101000111111101001100010110100011010110010101101101000010000010000110100000001000000110110101000011100001100011000000110001100010001010111110011100011101101
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Memory Space
18
00110100
+0111
![Page 31: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/31.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Whole-Part Relationships and Composite Objects
19
Elements of Programming, Chapter 12
![Page 32: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/32.jpg)
© 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
![Page 33: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/33.jpg)
© 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
![Page 34: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/34.jpg)
© 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
![Page 35: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/35.jpg)
© 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
![Page 36: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/36.jpg)
© 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
![Page 37: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/37.jpg)
© 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
![Page 38: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/38.jpg)
© 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
![Page 39: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/39.jpg)
© 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
![Page 40: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/40.jpg)
© 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
![Page 41: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/41.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 20
![Page 42: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/42.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is a data structure?
20
![Page 43: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/43.jpg)
© 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
![Page 44: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/44.jpg)
© 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
![Page 45: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/45.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Data Structure Performance
21
3GHz processor, from Chandler Carruth talk - Credit to Jeff Dean
![Page 46: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/46.jpg)
© 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
![Page 47: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/47.jpg)
© 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
![Page 48: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/48.jpg)
© 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
![Page 49: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/49.jpg)
© 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
![Page 50: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/50.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Example: Parallel Array & Algorithms
23
![Page 51: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/51.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
24
l
f
![Page 52: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/52.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
24
l
f
![Page 53: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/53.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
25
l
f
![Page 54: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/54.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
26
m
l
f
![Page 55: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/55.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
26
m
l
f
![Page 56: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/56.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
26
m
l
f
stable_partition(f, m, p)
stable_partition(m, l, p)
![Page 57: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/57.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
27
m
stable_partition(f, m, p)
stable_partition(m, l, p)
![Page 58: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/58.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
27
m
stable_partition(f, m, p)
stable_partition(m, l, p)
![Page 59: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/59.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
27
m
rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
![Page 60: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/60.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
28
rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
![Page 61: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/61.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Stable Partition
28
return rotate(stable_partition(f, m, p), m, stable_partition(m, l, p));
![Page 62: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/62.jpg)
© 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
![Page 63: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/63.jpg)
© 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
![Page 64: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/64.jpg)
© 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
![Page 65: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/65.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
30
l
f
![Page 66: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/66.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
30
l
f
![Page 67: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/67.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
30
l
f
![Page 68: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/68.jpg)
© 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
![Page 69: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/69.jpg)
© 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
![Page 70: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/70.jpg)
© 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;
![Page 71: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/71.jpg)
© 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
![Page 72: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/72.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Example: Algorithms & Minimal Work
33
![Page 73: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/73.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
34
41312795151421161610183
sf
sl
![Page 74: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/74.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
35
sf
sl
12345678910111213141516
![Page 75: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/75.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
36
sf
sl
XXXXX6789XXXXXXX
![Page 76: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/76.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
37
41312795151421161610183
f
l
sf
sl
![Page 77: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/77.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
38
41312795151421161610183
f
l
sf
![Page 78: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/78.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
38
41312795151421161610183
f
l
sf nth_element(f, sf, l);
![Page 79: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/79.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
39
f
l
sf nth_element(f, sf, l);
21345671412159161013811
![Page 80: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/80.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
40
nth_element(f, sf, l);
21345671412159161013811
f
l
sf
![Page 81: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/81.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
40
nth_element(f, sf, l);
21345671412159161013811
f
l
sf
≤ *sf
![Page 82: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/82.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
40
nth_element(f, sf, l);
21345671412159161013811
f
l
sf
≤ *sf
≥ *sf
![Page 83: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/83.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
41
nth_element(f, sf, l);
21345671412159161013811
f
l
sl
sf
![Page 84: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/84.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
42
nth_element(f, sf, l); ++sf;
21345671412159161013811
f
l
sl
sf
![Page 85: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/85.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
42
nth_element(f, sf, l); ++sf;
21345671412159161013811
f
l
sl
sf
![Page 86: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/86.jpg)
© 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
![Page 87: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/87.jpg)
© 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
![Page 88: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/88.jpg)
© 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
![Page 89: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/89.jpg)
© 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);
![Page 90: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/90.jpg)
© 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);
![Page 91: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/91.jpg)
© 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); }
![Page 92: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/92.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
49
sort_subrange(f, l, sf, sl); f
l
sl
sf
sl
41312795151421161610183
![Page 93: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/93.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
50
sort_subrange(f, l, sf, sl); f
l
sl
21345678915141612131011
sf
sl
![Page 94: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/94.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Work
50
sort_subrange(f, l, sf, sl); f
l
sl
21345678915141612131011
sf
sl
nl
![Page 95: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/95.jpg)
© 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
![Page 96: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/96.jpg)
© 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
![Page 97: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/97.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is an incidental data structure?
53
![Page 98: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/98.jpg)
© 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.
![Page 99: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/99.jpg)
© 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
![Page 100: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/100.jpg)
© 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
![Page 101: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/101.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
55
![Page 102: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/102.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Delegates
55
![Page 103: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/103.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Delegates
! Message handlers
55
![Page 104: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/104.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
56
![Page 105: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/105.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Incidental Data Structures
! Self-referential interface
56
![Page 106: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/106.jpg)
© 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
![Page 107: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/107.jpg)
© 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
![Page 108: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/108.jpg)
© 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
![Page 109: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/109.jpg)
© 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
![Page 110: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/110.jpg)
© 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
![Page 111: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/111.jpg)
© 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
![Page 112: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/112.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
57
A
forest
E
B C D
![Page 113: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/113.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
58
![Page 114: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/114.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
59
![Page 115: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/115.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
60
A
list
B
![Page 116: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/116.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
61
A
forest
E
B C D
![Page 117: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/117.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
62
![Page 118: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/118.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
63
![Page 119: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/119.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
64
![Page 120: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/120.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
65
![Page 121: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/121.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
66
A
forest
E
B C D
end()begin()
![Page 122: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/122.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
67
leading
trailing
![Page 123: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/123.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Hierarchies
68
A
forest
E
B C D
end()begin()
![Page 124: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/124.jpg)
© 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()
![Page 125: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/125.jpg)
© 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
![Page 126: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/126.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 71
![Page 127: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/127.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
No incidental data structures
71
![Page 128: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/128.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
No incidental data structures
Composite Types
71
![Page 129: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/129.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
No incidental data structures
Composite Types
Better Code
71
![Page 130: Better Code: Data Structures - stlab › ... › data-structures.pdf · Be"er Code 2! Regular Types ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No Raw Loops](https://reader034.vdocuments.net/reader034/viewer/2022042404/5f1a942b58b5ff2a257badbe/html5/thumbnails/130.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.