/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections;
using System.Collections.Generic;
namespace Opc.Ua.SourceGeneration
{
///
/// A read-only list that compares by content.
///
///
/// Roslyn caches an incremental step by comparing its output with the
/// previous run's output. A record carrying a freshly allocated array or
/// list compares that member by reference, so the record is never equal
/// to its previous value and every downstream step runs again. Carrying
/// the list as this type instead keeps the record equatable by value,
/// including when it is typed as : a
/// record's generated equality dispatches to .
///
internal sealed class EquatableArray : IReadOnlyList, IEquatable>
{
///
/// The empty list.
///
public static readonly EquatableArray Empty = new([]);
///
/// Wrap the items. The array is owned by the new instance and must
/// not be modified afterwards.
///
public EquatableArray(T[] items)
{
m_items = items ?? [];
}
///
/// Copy the items into a new list.
///
public static EquatableArray From(IEnumerable items)
{
return items == null ? Empty : new EquatableArray([.. items]);
}
///
public T this[int index] => m_items[index];
///
public int Count => m_items.Length;
///
public bool Equals(EquatableArray other)
{
if (other is null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
if (m_items.Length != other.m_items.Length)
{
return false;
}
EqualityComparer comparer = EqualityComparer.Default;
for (int ii = 0; ii < m_items.Length; ii++)
{
if (!comparer.Equals(m_items[ii], other.m_items[ii]))
{
return false;
}
}
return true;
}
///
public override bool Equals(object obj)
{
return Equals(obj as EquatableArray);
}
///
public override int GetHashCode()
{
int hash = 17;
foreach (T item in m_items)
{
hash = (hash * 31) + (item?.GetHashCode() ?? 0);
}
return hash;
}
///
public IEnumerator GetEnumerator()
{
return ((IEnumerable)m_items).GetEnumerator();
}
///
IEnumerator IEnumerable.GetEnumerator()
{
return m_items.GetEnumerator();
}
private readonly T[] m_items;
}
}