Skip to content

Commit 8f84ff3

Browse files
committed
chore(Util): Make array util rely on interface
1 parent d7a092f commit 8f84ff3

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

Assets/JCSUnity/Scripts/Managers/Others/JCS_PortalManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private void SetPlayerToPortalByLabel()
5555
if (!ps.RESET_POSITION_AT_START)
5656
return;
5757

58-
mPortals = JCS_Array.RemoveEmptyMissing(mPortals);
58+
mPortals = JCS_Array.RemoveEmptyMissing(mPortals).ToArray();
5959

6060
foreach (JCS_2DPortal portal in mPortals)
6161
{

Assets/JCSUnity/Scripts/Util/JCS_Array.cs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ public static T[] Merge2<T>(T[] arr1, T[] arr2)
9595
/// <summary>
9696
/// Check if the list empty.
9797
/// </summary>
98-
public static bool IsEmpty(string[] list)
98+
public static bool IsEmpty(ICollection<string> list)
9999
{
100-
for (int index = 0; index < list.Length; ++index)
100+
for (int index = 0; index < list.Count; ++index)
101101
{
102-
if (list[index] != "")
102+
if (list.ElementAt(index) != "")
103103
return false;
104104
}
105105

@@ -146,15 +146,14 @@ public static T PopBack<T>(List<T> list)
146146
/// <param name="len"> Target length to initialize. </param>
147147
/// <param name="with"> Initialize object type. </param>
148148
/// <returns> Return filled array. </returns>
149-
public static T[] Fill<T>(T[] inArray, int len, T with)
149+
public static List<T> Fill<T>(ICollection<T> coll, int len, T with)
150150
{
151-
return Fill(inArray.ToList(), len, with).ToArray();
152-
}
153-
public static List<T> Fill<T>(List<T> inList, int len, T with)
154-
{
155-
for (int index = inList.Count; index < len; ++index)
156-
inList.Add(with);
157-
return inList;
151+
List<T> result = new();
152+
153+
for (int index = coll.Count; index < len; ++index)
154+
result.Add(with);
155+
156+
return result;
158157
}
159158

160159
/// <summary>
@@ -163,23 +162,33 @@ public static List<T> Fill<T>(List<T> inList, int len, T with)
163162
/// <typeparam name="T"> Type of the List. </typeparam>
164163
/// <param name="inArray"> Array list. </param>
165164
/// <returns> Cleaned up Array object. </returns>
166-
public static T[] RemoveEmpty<T>(T[] inArray)
167-
{
168-
return RemoveEmpty(inArray.ToList()).ToArray();
169-
}
170-
public static List<T> RemoveEmpty<T>(List<T> inList)
165+
public static List<T> RemoveEmpty<T>(ICollection<T> coll)
171166
{
172-
List<T> newArray = new(inList.Count);
167+
List<T> newArray = new();
173168

174-
for (int index = 0; index < inList.Count; ++index)
169+
for (int index = 0; index < coll.Count; ++index)
175170
{
176171
// Add itself if exists.
177-
if (inList[index] != null)
178-
newArray.Add(inList[index]);
172+
if (coll.ElementAt(index) != null)
173+
newArray.Add(coll.ElementAt(index));
179174
}
180175

181176
return newArray;
182177
}
178+
public static Dictionary<T, V> RemoveEmpty<T, V>(IDictionary<T, V> dict)
179+
where T : UnityEngine.Object
180+
{
181+
Dictionary<T, V> result = new();
182+
183+
JCS_Loop.ForEach(dict, (entry) =>
184+
{
185+
// Add if exists.
186+
if (entry.Key != null)
187+
result.Add(entry.Key, entry.Value);
188+
});
189+
190+
return result;
191+
}
183192

184193
/// <summary>
185194
/// Remove the empty slot in the list including remove
@@ -194,27 +203,36 @@ public static List<T> RemoveEmpty<T>(List<T> inList)
194203
/// <typeparam name="T"> Type of the List. </typeparam>
195204
/// <param name="inList"> List object. </param>
196205
/// <returns> Cleaned up List object. </returns>
197-
public static T[] RemoveEmptyMissing<T>(T[] inArray)
206+
public static List<T> RemoveEmptyMissing<T>(ICollection<T> coll)
198207
where T : UnityEngine.Object
199208
{
200-
return RemoveEmptyMissing(inArray.ToList()).ToArray();
201-
}
202-
public static List<T> RemoveEmptyMissing<T>(List<T> inList)
203-
where T : UnityEngine.Object
204-
{
205-
List<T> newArray = new(inList.Count);
209+
List<T> result = new();
206210

207-
for (int index = 0; index < inList.Count; ++index)
211+
for (int index = 0; index < coll.Count; ++index)
208212
{
209-
// Add itself if exists.
213+
// Add if exists.
210214
//
211215
// SOURCE(jenchieh): https://answers.unity.com/questions/131158/how-can-i-check-if-an-object-is-null.html
212216
// INFORMATION(jenchieh): https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/
213-
if (inList[index] ?? false)
214-
newArray.Add(inList[index]);
217+
if (coll.ElementAt(index) ?? false)
218+
result.Add(coll.ElementAt(index));
215219
}
216220

217-
return newArray;
221+
return result;
222+
}
223+
public static Dictionary<T, V> RemoveEmptyMissing<T, V>(IDictionary<T, V> dict)
224+
where T : UnityEngine.Object
225+
{
226+
Dictionary<T, V> result = new();
227+
228+
JCS_Loop.ForEach(dict, (entry) =>
229+
{
230+
// Add if exists.
231+
if (entry.Key ?? false)
232+
result.Add(entry.Key, entry.Value);
233+
});
234+
235+
return result;
218236
}
219237
}
220238
}

0 commit comments

Comments
 (0)