comparison layer/ScrollableMagRangeCache.cpp @ 1324:13d9b422f7fe zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:31 +0100
parents a34a2a25907c
children 6dc3a36d9794
comparison
equal deleted inserted replaced
1183:57d192e26331 1324:13d9b422f7fe
21 21
22 //#define DEBUG_SCROLLABLE_MAG_RANGE_CACHE 1 22 //#define DEBUG_SCROLLABLE_MAG_RANGE_CACHE 1
23 23
24 void 24 void
25 ScrollableMagRangeCache::scrollTo(const LayerGeometryProvider *v, 25 ScrollableMagRangeCache::scrollTo(const LayerGeometryProvider *v,
26 sv_frame_t newStartFrame) 26 sv_frame_t newStartFrame)
27 { 27 {
28 static HitCount count("ScrollableMagRangeCache: scrolling"); 28 static HitCount count("ScrollableMagRangeCache: scrolling");
29 29
30 int dx = (v->getXForFrame(m_startFrame) - 30 int dx = (v->getXForFrame(m_startFrame) -
31 v->getXForFrame(newStartFrame)); 31 v->getXForFrame(newStartFrame));
32 32
33 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE 33 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
34 cerr << "ScrollableMagRangeCache::scrollTo: start frame " << m_startFrame 34 cerr << "ScrollableMagRangeCache::scrollTo: start frame " << m_startFrame
35 << " -> " << newStartFrame << ", dx = " << dx << endl; 35 << " -> " << newStartFrame << ", dx = " << dx << endl;
36 #endif 36 #endif
37 37
38 if (m_startFrame == newStartFrame) { 38 if (m_startFrame == newStartFrame) {
39 // haven't moved 39 // haven't moved
40 count.hit(); 40 count.hit();
41 return; 41 return;
42 } 42 }
43 43
44 m_startFrame = newStartFrame; 44 m_startFrame = newStartFrame;
45 45
46 if (dx == 0) { 46 if (dx == 0) {
47 // haven't moved visibly (even though start frame may have changed) 47 // haven't moved visibly (even though start frame may have changed)
48 count.hit(); 48 count.hit();
49 return; 49 return;
50 } 50 }
51 51
52 int w = int(m_ranges.size()); 52 int w = int(m_ranges.size());
53 53
54 if (dx <= -w || dx >= w) { 54 if (dx <= -w || dx >= w) {
55 // scrolled entirely off 55 // scrolled entirely off
56 invalidate(); 56 invalidate();
57 count.miss(); 57 count.miss();
58 return; 58 return;
59 } 59 }
60 60
61 count.partial(); 61 count.partial();
62 62
63 // dx is in range, cache is scrollable 63 // dx is in range, cache is scrollable
64 64
65 if (dx < 0) { 65 if (dx < 0) {
66 // The new start frame is to the left of the old start 66 // The new start frame is to the left of the old start
67 // frame. We need to add some empty ranges at the left (start) 67 // frame. We need to add some empty ranges at the left (start)
68 // end and clip the right end. Assemble -dx new values, then 68 // end and clip the right end. Assemble -dx new values, then
69 // w+dx old values starting at index 0. 69 // w+dx old values starting at index 0.
70 70
71 auto newRanges = vector<MagnitudeRange>(-dx); 71 auto newRanges = vector<MagnitudeRange>(-dx);
72 newRanges.insert(newRanges.end(), 72 newRanges.insert(newRanges.end(),
73 m_ranges.begin(), m_ranges.begin() + (w + dx)); 73 m_ranges.begin(), m_ranges.begin() + (w + dx));
74 m_ranges = newRanges; 74 m_ranges = newRanges;
75 75
76 } else { 76 } else {
77 // The new start frame is to the right of the old start 77 // The new start frame is to the right of the old start
78 // frame. We want to clip the left (start) end and add some 78 // frame. We want to clip the left (start) end and add some
79 // empty ranges at the right end. Assemble w-dx old values 79 // empty ranges at the right end. Assemble w-dx old values
80 // starting at index dx, then dx new values. 80 // starting at index dx, then dx new values.
81 81
82 auto newRanges = vector<MagnitudeRange>(dx); 82 auto newRanges = vector<MagnitudeRange>(dx);
83 newRanges.insert(newRanges.begin(), 83 newRanges.insert(newRanges.begin(),
84 m_ranges.begin() + dx, m_ranges.end()); 84 m_ranges.begin() + dx, m_ranges.end());
85 m_ranges = newRanges; 85 m_ranges = newRanges;
86 } 86 }
87 87
88 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE 88 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
89 cerr << "maxes (" << m_ranges.size() << ") now: "; 89 cerr << "maxes (" << m_ranges.size() << ") now: ";
90 for (int i = 0; in_range_for(m_ranges, i); ++i) { 90 for (int i = 0; in_range_for(m_ranges, i); ++i) {
91 cerr << m_ranges[i].getMax() << " "; 91 cerr << m_ranges[i].getMax() << " ";
92 } 92 }
93 cerr << endl; 93 cerr << endl;
94 #endif 94 #endif
95 } 95 }
96 96
100 MagnitudeRange r; 100 MagnitudeRange r;
101 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE 101 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
102 cerr << "ScrollableMagRangeCache::getRange(" << x << ", " << count << ")" << endl; 102 cerr << "ScrollableMagRangeCache::getRange(" << x << ", " << count << ")" << endl;
103 #endif 103 #endif
104 for (int i = 0; i < count; ++i) { 104 for (int i = 0; i < count; ++i) {
105 r.sample(m_ranges.at(x + i)); 105 r.sample(m_ranges.at(x + i));
106 } 106 }
107 return r; 107 return r;
108 } 108 }
109 109
110 void 110 void
111 ScrollableMagRangeCache::sampleColumn(int column, const MagnitudeRange &r) 111 ScrollableMagRangeCache::sampleColumn(int column, const MagnitudeRange &r)
112 { 112 {
113 if (!in_range_for(m_ranges, column)) { 113 if (!in_range_for(m_ranges, column)) {
114 cerr << "ERROR: ScrollableMagRangeCache::sampleColumn: column " << column 114 cerr << "ERROR: ScrollableMagRangeCache::sampleColumn: column " << column
115 << " is out of range for cache of width " << m_ranges.size() 115 << " is out of range for cache of width " << m_ranges.size()
116 << " (with start frame " << m_startFrame << ")" << endl; 116 << " (with start frame " << m_startFrame << ")" << endl;
117 throw logic_error("column out of range"); 117 throw logic_error("column out of range");
118 } else { 118 } else {
119 m_ranges[column].sample(r); 119 m_ranges[column].sample(r);
120 } 120 }
121 } 121 }
122 122