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

Commit a54888a

Browse files
committed
Make DataViewInfo a struct instead of class
1 parent 4f0553d commit a54888a

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/UI/Widgets/ScrollPool/DataHeightCache.cs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,29 @@
66

77
namespace UnityExplorer.UI.Widgets
88
{
9-
public class DataViewInfo
9+
public struct DataViewInfo
1010
{
11-
public int dataIndex;
12-
public float height, startPosition;
13-
public int normalizedSpread;
11+
// static
12+
public static DataViewInfo None => s_default;
13+
private static DataViewInfo s_default = default;
1414

1515
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();
1632
}
1733

1834
public class DataHeightCache<T> where T : ICell
@@ -53,14 +69,8 @@ public DataViewInfo this[int index]
5369
/// <summary>Get the first range (division of DefaultHeight) which the position appears in.</summary>
5470
private int GetRangeFloorOfPosition(float position) => (int)Math.Floor((decimal)position / (decimal)DefaultHeight);
5571

56-
/// <summary>Get the data index at the specified position of the total height cache.</summary>
57-
public int GetFirstDataIndexAtPosition(float desiredHeight) => GetFirstDataIndexAtPosition(desiredHeight, out _);
58-
59-
/// <summary>Get the data index and DataViewInfo at the specified position of the total height cache.</summary>
60-
public int GetFirstDataIndexAtPosition(float desiredHeight, out DataViewInfo cache)
72+
public int GetFirstDataIndexAtPosition(float desiredHeight)
6173
{
62-
cache = default;
63-
6474
if (!heightCache.Any())
6575
return 0;
6676

@@ -72,12 +82,11 @@ public int GetFirstDataIndexAtPosition(float desiredHeight, out DataViewInfo cac
7282
if (rangeIndex >= rangeCache.Count)
7383
{
7484
int idx = ScrollPool.DataSource.ItemCount - 1;
75-
cache = heightCache[idx];
7685
return idx;
7786
}
7887

7988
int dataIndex = rangeCache[rangeIndex];
80-
cache = heightCache[dataIndex];
89+
var cache = heightCache[dataIndex];
8190

8291
// if the DataViewInfo is outdated, need to rebuild
8392
int expectedMin = GetRangeCeilingOfPosition(cache.startPosition);
@@ -88,7 +97,7 @@ public int GetFirstDataIndexAtPosition(float desiredHeight, out DataViewInfo cac
8897

8998
rangeIndex = GetRangeFloorOfPosition(desiredHeight);
9099
dataIndex = rangeCache[rangeIndex];
91-
cache = heightCache[dataIndex];
100+
//cache = heightCache[dataIndex];
92101
}
93102

94103
return dataIndex;
@@ -141,8 +150,7 @@ public void RemoveLast()
141150
if (!heightCache.Any())
142151
return;
143152

144-
var val = heightCache[heightCache.Count - 1];
145-
totalHeight -= val;
153+
totalHeight -= heightCache[heightCache.Count - 1];
146154
heightCache.RemoveAt(heightCache.Count - 1);
147155

148156
int idx = heightCache.Count;
@@ -214,6 +222,9 @@ public void SetIndex(int dataIndex, float height)
214222

215223
SetSpread(dataIndex, rangeIndex, spreadDiff);
216224
}
225+
226+
// set the struct back to the array (TODO necessary?)
227+
heightCache[dataIndex] = cache;
217228
}
218229

219230
private void SetSpread(int dataIndex, int rangeIndex, int spreadDiff)
@@ -244,12 +255,12 @@ private void RecalculateStartPositions(int toIndex)
244255
return;
245256

246257
DataViewInfo cache;
247-
DataViewInfo prev = null;
258+
DataViewInfo prev = DataViewInfo.None;
248259
for (int i = 0; i <= toIndex && i < heightCache.Count; i++)
249260
{
250261
cache = heightCache[i];
251262

252-
if (prev != null)
263+
if (prev != DataViewInfo.None)
253264
cache.startPosition = prev.startPosition + prev.height;
254265
else
255266
cache.startPosition = 0;
@@ -262,19 +273,5 @@ private void RecalculateStartPositions(int toIndex)
262273
prev = cache;
263274
}
264275
}
265-
266-
//private void HardRebuildRanges()
267-
//{
268-
// var tempList = new List<float>();
269-
// for (int i = 0; i < heightCache.Count; i++)
270-
// tempList.Add(heightCache[i]);
271-
//
272-
// heightCache.Clear();
273-
// rangeCache.Clear();
274-
// totalHeight = 0;
275-
//
276-
// for (int i = 0; i < tempList.Count; i++)
277-
// SetIndex(i, tempList[i]);
278-
//}
279276
}
280277
}

0 commit comments

Comments
 (0)