sharplib/imm/List.cs
Marc Hernandez 3f850cc9b0 Implement Xml Serialization Framework with Type Handling and Metadata Caching
- Introduced XmlSer class for XML serialization and deserialization.
- Added TypeMetaCache for caching type metadata and reflection information.
- Implemented various ITypeHandler implementations for handling different types (Primitive, Proxy, ISerializable, Collection, Object).
- Enhanced type resolution with TypeResolver to manage type lookups and conversions.
- Established a configuration class (XmlCfg) to manage serialization settings.
- Integrated support for handling graphs and references in serialized objects.
- Added extensive member processing and filtering based on attributes.
- Ensured compatibility with immutable collections and various data structures.
- Implemented detailed error handling and logging for serialization processes.
2025-05-28 10:46:00 -07:00

77 lines
4.6 KiB
C#

#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
namespace imm;
/// <summary>
/// An immutable list implementation that tracks history, metadata, and time.
/// </summary>
public record class List<T> : Timed<List<T>>, IImmutableList<T>
{
static public List<T> Empty { get; } = new();
public ImmutableList<T> Values { get; init; } = ImmutableList<T>.Empty;
public List() { }
// Required for 'with' expressions to work with the base class hierarchy
protected List(Timed<List<T>> original) : base(original) { }
// Helper to apply changes using the Process method
private List<T> Change(
Func<ImmutableList<T>, ImmutableList<T>> listChange,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0,
[CallerArgumentExpression("listChange")] string reason = "")
{
var newValues = listChange(Values);
return ReferenceEquals(Values, newValues)
? this
: Process(l => l with { Values = newValues }, reason, memberName, filePath, lineNumber, reason);
}
// --- IImmutableList<T> implementation using the Change helper ---
public T this[int index] => Values[index];
public int Count => Values.Count;
public List<T> Add(T value) => Change(v => v.Add(value));
public List<T> AddRange(IEnumerable<T> items) => Change(v => v.AddRange(items));
public List<T> Clear() => Change(v => v.Clear());
// ... Implement all other IImmutableList methods similarly ...
#region IImmutableList Implementation
public List<T> Insert( int index, T element ) => Change( v => v.Insert( index, element ) );
public List<T> InsertRange( int index, IEnumerable<T> items ) => Change( v => v.InsertRange( index, items ) );
public List<T> Remove( T value, IEqualityComparer<T>? equalityComparer ) => Change( v => v.Remove( value, equalityComparer ) );
public List<T> Remove( T value ) => Remove( value, EqualityComparer<T>.Default );
public List<T> RemoveAll( Predicate<T> match ) => Change( v => v.RemoveAll( match ) );
public List<T> RemoveAt( int index ) => Change( v => v.RemoveAt( index ) );
public List<T> RemoveRange( IEnumerable<T> items, IEqualityComparer<T>? equalityComparer ) => Change( v => v.RemoveRange( items, equalityComparer ) );
public List<T> RemoveRange( int index, int count ) => Change( v => v.RemoveRange( index, count ) );
public List<T> Replace( T oldValue, T newValue, IEqualityComparer<T>? equalityComparer ) => Change( v => v.Replace( oldValue, newValue, equalityComparer ) );
public List<T> SetItem( int index, T value ) => Change( v => v.SetItem( index, value ) );
public int IndexOf( T item, int index, int count, IEqualityComparer<T>? equalityComparer ) => Values.IndexOf( item, index, count, equalityComparer ?? EqualityComparer<T>.Default );
public int IndexOf( T item ) => IndexOf( item, 0, Count, EqualityComparer<T>.Default );
public int LastIndexOf( T item, int index, int count, IEqualityComparer<T>? equalityComparer ) => Values.LastIndexOf( item, index, count, equalityComparer ?? EqualityComparer<T>.Default );
IImmutableList<T> IImmutableList<T>.Clear() => Clear();
IImmutableList<T> IImmutableList<T>.Add( T value ) => Add( value );
IImmutableList<T> IImmutableList<T>.AddRange( IEnumerable<T> items ) => AddRange( items );
IImmutableList<T> IImmutableList<T>.Insert( int index, T element ) => Insert( index, element );
IImmutableList<T> IImmutableList<T>.InsertRange( int index, IEnumerable<T> items ) => InsertRange( index, items );
IImmutableList<T> IImmutableList<T>.Remove( T value, IEqualityComparer<T>? equalityComparer ) => Remove( value, equalityComparer );
IImmutableList<T> IImmutableList<T>.RemoveAll( Predicate<T> match ) => RemoveAll( match );
IImmutableList<T> IImmutableList<T>.RemoveAt( int index ) => RemoveAt( index );
IImmutableList<T> IImmutableList<T>.RemoveRange( IEnumerable<T> items, IEqualityComparer<T>? equalityComparer ) => RemoveRange( items, equalityComparer );
IImmutableList<T> IImmutableList<T>.RemoveRange( int index, int count ) => RemoveRange( index, count );
IImmutableList<T> IImmutableList<T>.Replace( T oldValue, T newValue, IEqualityComparer<T>? equalityComparer ) => Replace( oldValue, newValue, equalityComparer );
IImmutableList<T> IImmutableList<T>.SetItem( int index, T value ) => SetItem( index, value );
#endregion
// --- Standard Interfaces ---
public IEnumerator<T> GetEnumerator() => Values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)Values).GetEnumerator();
}