use std::fmt;
use std::ops::Index;
use crate::value::{MapKey, Value};
#[derive(Clone, Default)]
pub struct List(imbl::Vector<Value>);
impl List {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn iter(&self) -> imbl::vector::Iter<'_, Value, imbl::shared_ptr::DefaultSharedPtr> {
self.0.iter()
}
#[must_use]
pub fn get(&self, index: usize) -> Option<&Value> {
self.0.get(index)
}
#[must_use]
pub fn first(&self) -> Option<&Value> {
self.0.front()
}
#[must_use]
pub fn last(&self) -> Option<&Value> {
self.0.back()
}
pub fn push_back(&mut self, value: Value) {
self.0.push_back(value);
}
pub fn push_front(&mut self, value: Value) {
self.0.push_front(value);
}
pub fn append(&mut self, other: List) {
if self.0.is_empty() {
self.0 = other.0;
} else if other.0.len() <= 32 {
self.0.extend(other.0);
} else {
self.0.append(other.0);
}
}
#[must_use]
pub fn skip(&self, start: usize) -> List {
List(self.0.skip(start.min(self.0.len())))
}
#[must_use]
pub fn take(&self, count: usize) -> List {
List(self.0.take(count.min(self.0.len())))
}
#[must_use]
pub fn to_vec(&self) -> Vec<Value> {
self.0.iter().cloned().collect()
}
#[must_use]
pub fn ptr_eq(&self, other: &List) -> bool {
self.0.ptr_eq(&other.0)
}
}
impl Index<usize> for List {
type Output = Value;
fn index(&self, index: usize) -> &Value {
&self.0[index]
}
}
impl From<Vec<Value>> for List {
fn from(v: Vec<Value>) -> Self {
List(v.into_iter().collect())
}
}
impl FromIterator<Value> for List {
fn from_iter<I: IntoIterator<Item = Value>>(iter: I) -> Self {
List(iter.into_iter().collect())
}
}
impl Extend<Value> for List {
fn extend<I: IntoIterator<Item = Value>>(&mut self, iter: I) {
self.0.extend(iter);
}
}
impl<'a> IntoIterator for &'a List {
type Item = &'a Value;
type IntoIter = imbl::vector::Iter<'a, Value, imbl::shared_ptr::DefaultSharedPtr>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl IntoIterator for List {
type Item = Value;
type IntoIter = imbl::vector::ConsumingIter<Value, imbl::shared_ptr::DefaultSharedPtr>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl fmt::Debug for List {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.0.iter()).finish()
}
}
#[derive(Clone, Default)]
pub struct Map(imbl::HashMap<MapKey, Value>);
impl Map {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn get(&self, key: &MapKey) -> Option<&Value> {
self.0.get(key)
}
#[must_use]
pub fn contains_key(&self, key: &MapKey) -> bool {
self.0.contains_key(key)
}
pub fn insert(&mut self, key: MapKey, value: Value) -> Option<Value> {
self.0.insert(key, value)
}
pub fn remove(&mut self, key: &MapKey) -> Option<Value> {
self.0.remove(key)
}
pub fn iter(&self) -> impl Iterator<Item = (&MapKey, &Value)> + '_ {
self.0.iter()
}
pub fn keys(&self) -> impl Iterator<Item = &MapKey> + '_ {
self.0.keys()
}
pub fn values(&self) -> impl Iterator<Item = &Value> + '_ {
self.0.values()
}
}
impl IntoIterator for Map {
type Item = (MapKey, Value);
type IntoIter =
imbl::hashmap::ConsumingIter<(MapKey, Value), imbl::shared_ptr::DefaultSharedPtr>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl FromIterator<(MapKey, Value)> for Map {
fn from_iter<I: IntoIterator<Item = (MapKey, Value)>>(iter: I) -> Self {
Map(iter.into_iter().collect())
}
}
impl Extend<(MapKey, Value)> for Map {
fn extend<I: IntoIterator<Item = (MapKey, Value)>>(&mut self, iter: I) {
self.0.extend(iter);
}
}
impl From<std::collections::HashMap<MapKey, Value>> for Map {
fn from(m: std::collections::HashMap<MapKey, Value>) -> Self {
m.into_iter().collect()
}
}
impl fmt::Debug for Map {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.0.iter()).finish()
}
}