Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 9992029

Browse files
committed
Set DataViewInfo struct back to array in rebuild, cleanup
1 parent 1410578 commit 9992029

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed

src/UI/Widgets/ScrollPool/DataHeightCache.cs

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@
66

77
namespace UnityExplorer.UI.Widgets
88
{
9-
public struct DataViewInfo
10-
{
11-
// static
12-
public static DataViewInfo None => s_default;
13-
private static DataViewInfo s_default = default;
14-
15-
public static implicit operator float(DataViewInfo it) => it.height;
16-
17-
// instance
18-
public int dataIndex, normalizedSpread;
19-
public float height, startPosition;
20-
21-
public override bool Equals(object obj)
22-
{
23-
var other = (DataViewInfo)obj;
24-
25-
return this.dataIndex == other.dataIndex
26-
&& this.height == other.height
27-
&& this.startPosition == other.startPosition
28-
&& this.normalizedSpread == other.normalizedSpread;
29-
}
30-
31-
public override int GetHashCode() => base.GetHashCode();
32-
}
33-
349
public class DataHeightCache<T> where T : ICell
3510
{
3611
private ScrollPool<T> ScrollPool { get; }
@@ -93,11 +68,10 @@ public int GetFirstDataIndexAtPosition(float desiredHeight)
9368
int expectedMax = expectedMin + cache.normalizedSpread - 1;
9469
if (rangeIndex < expectedMin || rangeIndex > expectedMax)
9570
{
96-
RecalculateStartPositions(Math.Max(dataIndex, expectedMax));
71+
RecalculateStartPositions(ScrollPool.DataSource.ItemCount - 1);
9772

9873
rangeIndex = GetRangeFloorOfPosition(desiredHeight);
9974
dataIndex = rangeCache[rangeIndex];
100-
//cache = heightCache[dataIndex];
10175
}
10276

10377
return dataIndex;
@@ -125,17 +99,11 @@ private int GetRangeSpread(float startPosition, float height)
12599
/// <summary>Append a data index to the cache with the provided height value.</summary>
126100
public void Add(float value)
127101
{
128-
value = (float)Math.Floor(value);
129102
value = Math.Max(DefaultHeight, value);
130103

131104
int spread = GetRangeSpread(totalHeight, value);
132105

133-
heightCache.Add(new DataViewInfo()
134-
{
135-
height = value,
136-
startPosition = TotalHeight,
137-
normalizedSpread = spread,
138-
});
106+
heightCache.Add(new DataViewInfo(heightCache.Count, value, totalHeight, spread));
139107

140108
int dataIdx = heightCache.Count - 1;
141109
for (int i = 0; i < spread; i++)
@@ -203,31 +171,31 @@ public void SetIndex(int dataIndex, float height)
203171
int spread = GetRangeSpread(cache.startPosition, height);
204172

205173
// If the previous item in the range cache is not the previous data index, there is a gap.
206-
if (dataIndex > 0 && rangeCache[rangeIndex - 1] != (dataIndex - 1))
174+
if (rangeCache[rangeIndex] != dataIndex)
207175
{
208176
// Recalculate start positions up to this index. The gap could be anywhere before here.
209-
RecalculateStartPositions(dataIndex + 1);
177+
RecalculateStartPositions(ScrollPool.DataSource.ItemCount - 1);
210178
// Get the range index and spread again after rebuilding
211179
rangeIndex = GetRangeCeilingOfPosition(cache.startPosition);
212180
spread = GetRangeSpread(cache.startPosition, height);
213181
}
214182

215-
if (rangeCache.Count <= rangeIndex || rangeCache[rangeIndex] != dataIndex)
216-
throw new Exception("ScrollPool data height cache is corrupt or invalid, rebuild failed!");
183+
if (rangeCache[rangeIndex] != dataIndex)
184+
throw new IndexOutOfRangeException($"Trying to set dataIndex {dataIndex} at rangeIndex {rangeIndex}, but cache is corrupt or invalid!");
217185

218186
if (spread != cache.normalizedSpread)
219187
{
220188
int spreadDiff = spread - cache.normalizedSpread;
221189
cache.normalizedSpread = spread;
222190

223-
SetSpread(dataIndex, rangeIndex, spreadDiff);
191+
UpdateSpread(dataIndex, rangeIndex, spreadDiff);
224192
}
225193

226-
// set the struct back to the array (TODO necessary?)
194+
// set the struct back to the array
227195
heightCache[dataIndex] = cache;
228196
}
229197

230-
private void SetSpread(int dataIndex, int rangeIndex, int spreadDiff)
198+
private void UpdateSpread(int dataIndex, int rangeIndex, int spreadDiff)
231199
{
232200
if (spreadDiff > 0)
233201
{
@@ -244,8 +212,6 @@ private void SetSpread(int dataIndex, int rangeIndex, int spreadDiff)
244212
rangeCache.RemoveAt(rangeIndex);
245213
spreadDiff++;
246214
}
247-
//for (int i = 0; i < -spreadDiff; i++)
248-
// rangeCache.RemoveAt(rangeIndex);
249215
}
250216
}
251217

@@ -254,24 +220,60 @@ private void RecalculateStartPositions(int toIndex)
254220
if (heightCache.Count <= 1)
255221
return;
256222

223+
rangeCache.Clear();
224+
257225
DataViewInfo cache;
258226
DataViewInfo prev = DataViewInfo.None;
259-
for (int i = 0; i <= toIndex && i < heightCache.Count; i++)
227+
for (int idx = 0; idx <= toIndex && idx < heightCache.Count; idx++)
260228
{
261-
cache = heightCache[i];
229+
cache = heightCache[idx];
262230

263-
if (prev != DataViewInfo.None)
231+
if (!prev.Equals(DataViewInfo.None))
264232
cache.startPosition = prev.startPosition + prev.height;
265233
else
266234
cache.startPosition = 0;
267235

268-
var origSpread = cache.normalizedSpread;
269236
cache.normalizedSpread = GetRangeSpread(cache.startPosition, cache.height);
270-
if (cache.normalizedSpread != origSpread)
271-
SetSpread(i, GetRangeCeilingOfPosition(cache.startPosition), cache.normalizedSpread - origSpread);
237+
for (int i = 0; i < cache.normalizedSpread; i++)
238+
rangeCache.Add(cache.dataIndex);
239+
240+
heightCache[idx] = cache;
272241

273242
prev = cache;
274243
}
275244
}
245+
246+
public struct DataViewInfo
247+
{
248+
// static
249+
public static DataViewInfo None => s_default;
250+
private static DataViewInfo s_default = default;
251+
252+
public static implicit operator float(DataViewInfo it) => it.height;
253+
254+
public DataViewInfo(int index, float height, float startPos, int spread)
255+
{
256+
this.dataIndex = index;
257+
this.height = height;
258+
this.startPosition = startPos;
259+
this.normalizedSpread = spread;
260+
}
261+
262+
// instance
263+
public int dataIndex, normalizedSpread;
264+
public float height, startPosition;
265+
266+
public override bool Equals(object obj)
267+
{
268+
var other = (DataViewInfo)obj;
269+
270+
return this.dataIndex == other.dataIndex
271+
&& this.height == other.height
272+
&& this.startPosition == other.startPosition
273+
&& this.normalizedSpread == other.normalizedSpread;
274+
}
275+
276+
public override int GetHashCode() => base.GetHashCode();
277+
}
276278
}
277279
}

0 commit comments

Comments
 (0)