paulirwin commented on code in PR #1066:
URL: https://github.com/apache/lucenenet/pull/1066#discussion_r2354170619
##########
src/Lucene.Net/Support/CastingListAdapter.cs:
##########
@@ -0,0 +1,75 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Lucene.Net.Support
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ /// <summary>
+ /// LUCENENET specific class used to adapt a <see cref="IList{T}"/> to one
with a different type parameter
+ /// where the elements are cast to the new type where needed.
+ /// </summary>
+ /// <typeparam name="T">The type of the elements in the original
list.</typeparam>
+ /// <typeparam name="U">The type of the elements in the adapted
list.</typeparam>
+ internal class CastingListAdapter<T, U> : IList<U>
+ where T : U
+ {
+ private readonly IList<T> list;
+
+ public CastingListAdapter(IList<T> list)
+ {
+ this.list = list;
+ }
+
+ public U this[int index]
+ {
+ get => list[index];
+ set => list[index] = (T)value;
+ }
+
+ public int Count => list.Count;
+
+ public bool IsReadOnly => list.IsReadOnly;
+
+ public void Add(U item) => list.Add((T)item);
+
+ public void Clear() => list.Clear();
+
+ public bool Contains(U item) => list.Contains((T)item);
+
+ public void CopyTo(U[] array, int arrayIndex)
+ {
+ for (int i = 0; i < list.Count; i++)
+ {
+ array[arrayIndex + i] = list[i];
+ }
+ }
+
+ public IEnumerator<U> GetEnumerator() =>
list.Cast<U>().GetEnumerator();
Review Comment:
Fixed, and also made CastingEnumeratorAdapter a readonly struct to reduce an
allocation. In most cases it still will likely be boxed, but there might be
some runtime optimizations that could take advantage of that. At a minimum, it
can't hurt anyways.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]