use super::Value;
use indexmap::IndexMap;
use std::collections::BTreeMap;
pub const MAX_ARRAY_LENGTH: u32 = u32::MAX;
#[derive(Clone, Default)]
pub struct Array {
elements: Elements,
named: Option<Box<IndexMap<String, Value>>>,
}
#[derive(Clone)]
enum Elements {
Dense(Vec<Value>),
Sparse {
length: u32,
present: BTreeMap<u32, Value>,
},
}
impl Default for Elements {
fn default() -> Self {
Elements::Dense(Vec::new())
}
}
impl Array {
pub fn new(items: Vec<Value>) -> Self {
Array {
elements: Elements::Dense(items),
named: None,
}
}
pub fn from_parts(
length: u32,
present: impl IntoIterator<Item = (u32, Value)>,
named: IndexMap<String, Value>,
) -> Self {
let mut map: BTreeMap<u32, Value> = BTreeMap::new();
for (i, v) in present {
if i < length {
map.insert(i, v);
}
}
let elements = if map.len() == length as usize {
Elements::Dense(map.into_values().collect())
} else {
Elements::Sparse {
length,
present: map,
}
};
Array {
elements,
named: (!named.is_empty()).then(|| Box::new(named)),
}
}
pub fn len(&self) -> usize {
match &self.elements {
Elements::Dense(items) => items.len(),
Elements::Sparse { length, .. } => *length as usize,
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn get(&self, index: usize) -> Option<&Value> {
match &self.elements {
Elements::Dense(items) => items.get(index),
Elements::Sparse { present, .. } => {
u32::try_from(index).ok().and_then(|i| present.get(&i))
}
}
}
pub fn is_dense(&self) -> bool {
matches!(self.elements, Elements::Dense(_))
}
pub fn as_dense(&self) -> Option<&[Value]> {
match &self.elements {
Elements::Dense(items) => Some(items),
Elements::Sparse { .. } => None,
}
}
pub fn present(&self) -> Present<'_> {
match &self.elements {
Elements::Dense(items) => Present::Dense(items.iter().enumerate()),
Elements::Sparse { present, .. } => Present::Sparse(present.iter()),
}
}
pub fn present_len(&self) -> usize {
match &self.elements {
Elements::Dense(items) => items.len(),
Elements::Sparse { present, .. } => present.len(),
}
}
pub fn named(&self) -> impl Iterator<Item = (&String, &Value)> {
self.named.iter().flat_map(|n| n.iter())
}
pub fn has_named(&self) -> bool {
self.named.as_ref().is_some_and(|n| !n.is_empty())
}
pub fn named_get(&self, key: &str) -> Option<&Value> {
self.named.as_ref().and_then(|n| n.get(key))
}
pub fn to_options(&self) -> Vec<Option<Value>> {
match &self.elements {
Elements::Dense(items) => items.iter().cloned().map(Some).collect(),
Elements::Sparse { length, present } => {
let mut out = vec![None; *length as usize];
for (i, v) in present {
out[*i as usize] = Some(v.clone());
}
out
}
}
}
pub(super) fn take_values(&mut self) -> Vec<Value> {
let mut out = match std::mem::take(&mut self.elements) {
Elements::Dense(items) => items,
Elements::Sparse { present, .. } => present.into_values().collect(),
};
if let Some(named) = self.named.take() {
out.extend(named.into_values());
}
out
}
pub fn into_vec(mut self) -> Option<Vec<Value>> {
if self.has_named() {
return None;
}
match std::mem::take(&mut self.elements) {
Elements::Dense(items) => Some(items),
sparse @ Elements::Sparse { .. } => {
self.elements = sparse;
None
}
}
}
}
pub enum Present<'a> {
Dense(std::iter::Enumerate<std::slice::Iter<'a, Value>>),
Sparse(std::collections::btree_map::Iter<'a, u32, Value>),
}
impl<'a> Iterator for Present<'a> {
type Item = (u32, &'a Value);
fn next(&mut self) -> Option<Self::Item> {
match self {
Present::Dense(items) => items.next().map(|(i, v)| (i as u32, v)),
Present::Sparse(present) => present.next().map(|(i, v)| (*i, v)),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
match self {
Present::Dense(items) => items.size_hint(),
Present::Sparse(present) => present.size_hint(),
}
}
}
impl From<Vec<Value>> for Array {
fn from(items: Vec<Value>) -> Self {
Array::new(items)
}
}