using System;
using System.Linq;
using System.Runtime.Serialization;
namespace Python.Runtime
{
///
/// Represents a standard Python list object. See the documentation at
/// PY2: https://docs.python.org/2/c-api/list.html
/// PY3: https://docs.python.org/3/c-api/list.html
/// for details.
///
[Serializable]
public class PyList : PySequence
{
internal PyList(in StolenReference reference) : base(reference) { }
///
/// Creates new pointing to the same object, as the given reference.
///
internal PyList(BorrowedReference reference) : base(reference) { }
protected PyList(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
private static BorrowedReference FromObject(PyObject o)
{
if (o == null || !IsListType(o))
{
throw new ArgumentException("object is not a list");
}
return o.Reference;
}
///
/// PyList Constructor
///
///
/// Copy constructor - obtain a PyList from a generic PyObject. An
/// ArgumentException will be thrown if the given object is not a
/// Python list object.
///
public PyList(PyObject o) : base(FromObject(o))
{
}
///
/// Creates a new empty Python list object.
///
public PyList() : base(Runtime.PyList_New(0).StealOrThrow())
{
}
private static StolenReference FromArray(PyObject[] items)
{
if (items is null) throw new ArgumentNullException(nameof(items));
if (items.Any(item => item is null))
throw new ArgumentException(message: Util.UseNone, paramName: nameof(items));
int count = items.Length;
using var val = Runtime.PyList_New(count);
for (var i = 0; i < count; i++)
{
int r = Runtime.PyList_SetItem(val.Borrow(), i, new NewReference(items[i]).Steal());
if (r < 0)
{
val.Dispose();
throw PythonException.ThrowLastAsClrException();
}
}
return val.Steal();
}
///
/// Creates a new Python list object from an array of objects.
///
public PyList(PyObject[] items) : base(FromArray(items))
{
}
///
/// Returns true if the given object is a Python list.
///
public static bool IsListType(PyObject value)
{
if (value is null) throw new ArgumentNullException(nameof(value));
return Runtime.PyList_Check(value.obj);
}
///
/// Converts a Python object to a Python list if possible, raising
/// a PythonException if the conversion is not possible. This is
/// equivalent to the Python expression "list(object)".
///
public static PyList AsList(PyObject value)
{
if (value is null) throw new ArgumentNullException(nameof(value));
NewReference op = Runtime.PySequence_List(value.Reference);
if (op.IsNull())
{
throw PythonException.ThrowLastAsClrException();
}
return new PyList(op.Steal());
}
///
/// Append an item to the list object.
///
public void Append(PyObject item)
{
if (item is null) throw new ArgumentNullException(nameof(item));
int r = Runtime.PyList_Append(this.Reference, item.Reference);
if (r < 0)
{
throw PythonException.ThrowLastAsClrException();
}
}
///
/// Insert an item in the list object at the given index.
///
public void Insert(int index, PyObject item)
{
if (item is null) throw new ArgumentNullException(nameof(item));
int r = Runtime.PyList_Insert(this, index, item);
if (r < 0)
{
throw PythonException.ThrowLastAsClrException();
}
}
///
/// Reverse Method
///
///
/// Reverse the order of the list object in place.
///
public void Reverse()
{
int r = Runtime.PyList_Reverse(this.Reference);
if (r < 0)
{
throw PythonException.ThrowLastAsClrException();
}
}
///
/// Sort Method
///
///
/// Sort the list in place.
///
public void Sort()
{
int r = Runtime.PyList_Sort(this.Reference);
if (r < 0)
{
throw PythonException.ThrowLastAsClrException();
}
}
public override int GetHashCode() => rawPtr.GetHashCode();
public override bool Equals(PyObject? other)
{
if (other is null) return false;
if (obj == other.obj) return true;
if (other is PyList || IsListType(other)) return base.Equals(other);
return false;
}
}
}