better code: data structures...2016/08/08  · be!er code 2! regular types ! goal: implement...

83
Better Code: Data Structures Sean Parent | Principal Scientist

Upload: others

Post on 03-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

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:

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:

© 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:

© 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:

© 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:

© 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:

© 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:

[ 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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 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:

© 2015 Adobe Systems Incorporated.