1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::LocExpr;

use crate::{function::FuncVal, Context, Result, Thunk, Val};

mod spec;
use spec::*;

/// Represents a Jsonnet array value.
#[derive(Debug, Clone, Trace)]
// may contrain other ArrValue
#[trace(tracking(force))]
pub enum ArrValue {
	/// Layout optimized byte array.
	Bytes(BytesArray),
	/// Every element is lazy evaluated.
	Lazy(LazyArray),
	/// Every element is defined somewhere in source code
	Expr(ExprArray),
	/// Every field is already evaluated.
	Eager(EagerArray),
	/// Concatenation of two arrays of any kind.
	Extended(Cc<ExtendedArray>),
	/// Represents a integer array in form `[start, start + 1, ... end - 1, end]`.
	/// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.
	Range(RangeArray),
	/// Sliced array view.
	Slice(Cc<SliceArray>),
	/// Reversed array view.
	/// Returned by `std.reverse(other)` call
	Reverse(Cc<ReverseArray>),
	/// Returned by `std.map` call
	Mapped(MappedArray),
	/// Returned by `std.repeat` call
	Repeated(RepeatedArray),
}

pub trait ArrayLikeIter<T>: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator {}
impl<I, T> ArrayLikeIter<T> for I where
	I: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator
{
}

impl ArrValue {
	pub fn empty() -> Self {
		Self::Range(RangeArray::empty())
	}

	pub fn expr(ctx: Context, exprs: impl IntoIterator<Item = LocExpr>) -> Self {
		Self::Expr(ExprArray::new(ctx, exprs))
	}

	pub fn lazy(thunks: Cc<Vec<Thunk<Val>>>) -> Self {
		Self::Lazy(LazyArray(thunks))
	}

	pub fn eager(values: Vec<Val>) -> Self {
		Self::Eager(EagerArray(Cc::new(values)))
	}

	pub fn repeated(data: ArrValue, repeats: usize) -> Option<Self> {
		Some(Self::Repeated(RepeatedArray::new(data, repeats)?))
	}

	pub fn bytes(bytes: IBytes) -> Self {
		Self::Bytes(BytesArray(bytes))
	}

	#[must_use]
	pub fn map(self, mapper: FuncVal) -> Self {
		Self::Mapped(MappedArray::new(self, mapper))
	}

	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
		// TODO: ArrValue::Picked(inner, indexes) for large arrays
		let mut out = Vec::new();
		for i in self.iter() {
			let i = i?;
			if filter(&i)? {
				out.push(i);
			};
		}
		Ok(Self::eager(out))
	}

	pub fn extended(a: ArrValue, b: ArrValue) -> Self {
		// TODO: benchmark for an optimal value, currently just a arbitrary choice
		const ARR_EXTEND_THRESHOLD: usize = 100;

		if a.is_empty() {
			b
		} else if b.is_empty() {
			a
		} else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {
			Self::Extended(Cc::new(ExtendedArray::new(a, b)))
		} else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {
			let mut out = Vec::with_capacity(a.len() + b.len());
			out.extend(a);
			out.extend(b);
			Self::eager(out)
		} else {
			let mut out = Vec::with_capacity(a.len() + b.len());
			out.extend(a.iter_lazy());
			out.extend(b.iter_lazy());
			Self::lazy(Cc::new(out))
		}
	}

	pub fn range_exclusive(a: i32, b: i32) -> Self {
		Self::Range(RangeArray::new_exclusive(a, b))
	}
	pub fn range_inclusive(a: i32, b: i32) -> Self {
		Self::Range(RangeArray::new_inclusive(a, b))
	}

	#[must_use]
	pub fn slice(
		self,
		from: Option<usize>,
		to: Option<usize>,
		step: Option<usize>,
	) -> Option<Self> {
		let len = self.len();
		let from = from.unwrap_or(0);
		let to = to.unwrap_or(len).min(len);
		let step = step.unwrap_or(1);
		if from >= to || step == 0 {
			return None;
		}

		Some(Self::Slice(Cc::new(SliceArray {
			inner: self,
			from: from as u32,
			to: to as u32,
			step: step as u32,
		})))
	}

	/// Array length.
	pub fn len(&self) -> usize {
		pass!(self.len())
	}

	/// Is array contains no elements?
	pub fn is_empty(&self) -> bool {
		pass!(self.is_empty())
	}

	/// Get array element by index, evaluating it, if it is lazy.
	///
	/// Returns `None` on out-of-bounds condition.
	pub fn get(&self, index: usize) -> Result<Option<Val>> {
		pass!(self.get(index))
	}

	/// Returns None if get is either non cheap, or out of bounds
	fn get_cheap(&self, index: usize) -> Option<Val> {
		pass!(self.get_cheap(index))
	}

	/// Get array element by index, without evaluation.
	///
	/// Returns `None` on out-of-bounds condition.
	pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
		pass!(self.get_lazy(index))
	}

	#[cfg(feature = "nightly")]
	pub fn iter(&self) -> UnknownArrayIter<'_> {
		pass_iter_call!(self.iter => UnknownArrayIter)
	}
	#[cfg(not(feature = "nightly"))]
	pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {
		(0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))
	}

	/// Iterate over elements, returning lazy values.
	#[cfg(feature = "nightly")]
	pub fn iter_lazy(&self) -> UnknownArrayIterLazy<'_> {
		pass_iter_call!(self.iter_lazy => UnknownArrayIterLazy)
	}
	#[cfg(not(feature = "nightly"))]
	pub fn iter_lazy(&self) -> impl ArrayLikeIter<Thunk<Val>> + '_ {
		(0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))
	}

	#[cfg(feature = "nightly")]
	pub fn iter_cheap(&self) -> Option<UnknownArrayIterCheap<'_>> {
		macro_rules! question {
			($v:expr) => {
				$v?
			};
		}
		Some(pass_iter_call!(self.iter_cheap in question => UnknownArrayIterCheap))
	}

	#[cfg(not(feature = "nightly"))]
	pub fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {
		if self.is_cheap() {
			Some((0..self.len()).map(|i| self.get_cheap(i).expect("length and is_cheap checked")))
		} else {
			None
		}
	}

	/// Return a reversed view on current array.
	#[must_use]
	pub fn reversed(self) -> Self {
		Self::Reverse(Cc::new(ReverseArray(self)))
	}

	pub fn ptr_eq(a: &Self, b: &Self) -> bool {
		match (a, b) {
			(ArrValue::Bytes(a), ArrValue::Bytes(b)) => a.0 == b.0,
			(ArrValue::Lazy(a), ArrValue::Lazy(b)) => Cc::ptr_eq(&a.0, &b.0),
			(ArrValue::Expr(a), ArrValue::Expr(b)) => Cc::ptr_eq(&a.0, &b.0),
			(ArrValue::Eager(a), ArrValue::Eager(b)) => Cc::ptr_eq(&a.0, &b.0),
			(ArrValue::Extended(a), ArrValue::Extended(b)) => Cc::ptr_eq(a, b),
			(ArrValue::Range(a), ArrValue::Range(b)) => a == b,
			_ => false,
		}
	}

	pub fn is_cheap(&self) -> bool {
		match self {
			ArrValue::Eager(_) | ArrValue::Range(..) | ArrValue::Bytes(_) => true,
			ArrValue::Extended(v) => v.a.is_cheap() && v.b.is_cheap(),
			ArrValue::Slice(r) => r.inner.is_cheap(),
			ArrValue::Reverse(i) => i.0.is_cheap(),
			ArrValue::Repeated(v) => v.is_cheap(),
			ArrValue::Expr(_) | ArrValue::Lazy(_) | ArrValue::Mapped(_) => false,
		}
	}
}
impl From<Vec<Val>> for ArrValue {
	fn from(value: Vec<Val>) -> Self {
		Self::eager(value)
	}
}
impl From<Vec<Thunk<Val>>> for ArrValue {
	fn from(value: Vec<Thunk<Val>>) -> Self {
		Self::lazy(Cc::new(value))
	}
}
impl FromIterator<Val> for ArrValue {
	fn from_iter<T: IntoIterator<Item = Val>>(iter: T) -> Self {
		Self::eager(iter.into_iter().collect())
	}
}

#[cfg(target_pointer_width = "64")]
static_assertions::assert_eq_size!(ArrValue, [u8; 16]);