difftreelog
perf O(1) std.reverse, std.range
in: master
3 files changed
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth1use crate::function::{CallLocation, StaticBuiltin};2use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};3use crate::{4 builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},5 equals,6 error::{Error::*, Result},7 operator::evaluate_mod_op,8 primitive_equals, push_frame, throw,9 typed::{Either2, Either4},10 with_state, ArrValue, FuncVal, IndexableVal, Val,11};12use crate::{Either, ObjValue};13use format::{format_arr, format_obj};14use jrsonnet_interner::IStr;15use serde::Deserialize;16use serde_yaml::DeserializingQuirks;17use std::collections::HashMap;18use std::convert::{TryFrom, TryInto};1920pub mod stdlib;21pub use stdlib::*;2223use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};2425pub mod format;26pub mod manifest;27pub mod sort;2829pub fn std_format(str: IStr, vals: Val) -> Result<String> {30 push_frame(31 CallLocation::native(),32 || format!("std.format of {}", str),33 || {34 Ok(match vals {35 Val::Arr(vals) => format_arr(&str, &vals.evaluated()?)?,36 Val::Obj(obj) => format_obj(&str, &obj)?,37 o => format_arr(&str, &[o])?,38 })39 },40 )41}4243pub fn std_slice(44 indexable: IndexableVal,45 index: Option<usize>,46 end: Option<usize>,47 step: Option<usize>,48) -> Result<Val> {49 let index = index.unwrap_or(0);50 let end = end.unwrap_or_else(|| match &indexable {51 IndexableVal::Str(_) => usize::MAX,52 IndexableVal::Arr(v) => v.len(),53 });54 let step = step.unwrap_or(1);55 match &indexable {56 IndexableVal::Str(s) => Ok(Val::Str(57 (s.chars()58 .skip(index)59 .take(end - index)60 .step_by(step)61 .collect::<String>())62 .into(),63 )),64 IndexableVal::Arr(arr) => Ok(Val::Arr(65 (arr.iter()66 .skip(index)67 .take(end - index)68 .step_by(step)69 .collect::<Result<Vec<Val>>>()?)70 .into(),71 )),72 }73}7475type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;7677thread_local! {78 pub static BUILTINS: BuiltinsType = {79 [80 ("length".into(), builtin_length::INST),81 ("type".into(), builtin_type::INST),82 ("makeArray".into(), builtin_make_array::INST),83 ("codepoint".into(), builtin_codepoint::INST),84 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),85 ("objectHasEx".into(), builtin_object_has_ex::INST),86 ("slice".into(), builtin_slice::INST),87 ("substr".into(), builtin_substr::INST),88 ("primitiveEquals".into(), builtin_primitive_equals::INST),89 ("equals".into(), builtin_equals::INST),90 ("modulo".into(), builtin_modulo::INST),91 ("mod".into(), builtin_mod::INST),92 ("floor".into(), builtin_floor::INST),93 ("ceil".into(), builtin_ceil::INST),94 ("log".into(), builtin_log::INST),95 ("pow".into(), builtin_pow::INST),96 ("sqrt".into(), builtin_sqrt::INST),97 ("sin".into(), builtin_sin::INST),98 ("cos".into(), builtin_cos::INST),99 ("tan".into(), builtin_tan::INST),100 ("asin".into(), builtin_asin::INST),101 ("acos".into(), builtin_acos::INST),102 ("atan".into(), builtin_atan::INST),103 ("exp".into(), builtin_exp::INST),104 ("mantissa".into(), builtin_mantissa::INST),105 ("exponent".into(), builtin_exponent::INST),106 ("extVar".into(), builtin_ext_var::INST),107 ("native".into(), builtin_native::INST),108 ("filter".into(), builtin_filter::INST),109 ("map".into(), builtin_map::INST),110 ("flatMap".into(), builtin_flatmap::INST),111 ("foldl".into(), builtin_foldl::INST),112 ("foldr".into(), builtin_foldr::INST),113 ("sort".into(), builtin_sort::INST),114 ("format".into(), builtin_format::INST),115 ("range".into(), builtin_range::INST),116 ("char".into(), builtin_char::INST),117 ("encodeUTF8".into(), builtin_encode_utf8::INST),118 ("decodeUTF8".into(), builtin_decode_utf8::INST),119 ("md5".into(), builtin_md5::INST),120 ("base64".into(), builtin_base64::INST),121 ("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),122 ("base64Decode".into(), builtin_base64_decode::INST),123 ("trace".into(), builtin_trace::INST),124 ("join".into(), builtin_join::INST),125 ("escapeStringJson".into(), builtin_escape_string_json::INST),126 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),127 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),128 ("reverse".into(), builtin_reverse::INST),129 ("id".into(), builtin_id::INST),130 ("strReplace".into(), builtin_str_replace::INST),131 ("splitLimit".into(), builtin_splitlimit::INST),132 ("parseJson".into(), builtin_parse_json::INST),133 ("parseYaml".into(), builtin_parse_yaml::INST),134 ("asciiUpper".into(), builtin_ascii_upper::INST),135 ("asciiLower".into(), builtin_ascii_lower::INST),136 ("member".into(), builtin_member::INST),137 ("count".into(), builtin_count::INST),138 ("any".into(), builtin_any::INST),139 ("all".into(), builtin_all::INST),140 ].iter().cloned().collect()141 };142}143144#[jrsonnet_macros::builtin]145fn builtin_length(x: Either![IStr, VecVal, ObjValue, FuncVal]) -> Result<usize> {146 use Either4::*;147 Ok(match x {148 A(x) => x.chars().count(),149 B(x) => x.0.len(),150 C(x) => x151 .fields_visibility()152 .into_iter()153 .filter(|(_k, v)| *v)154 .count(),155 D(f) => f.args_len(),156 })157}158159#[jrsonnet_macros::builtin]160fn builtin_type(x: Any) -> Result<IStr> {161 Ok(x.0.value_type().name().into())162}163164#[jrsonnet_macros::builtin]165fn builtin_make_array(sz: usize, func: FuncVal) -> Result<VecVal> {166 let mut out = Vec::with_capacity(sz);167 for i in 0..sz {168 out.push(func.evaluate_simple(&[i as f64].as_slice())?)169 }170 Ok(VecVal(out))171}172173#[jrsonnet_macros::builtin]174const fn builtin_codepoint(str: char) -> Result<u32> {175 Ok(str as u32)176}177178#[jrsonnet_macros::builtin]179fn builtin_object_fields_ex(obj: ObjValue, inc_hidden: bool) -> Result<VecVal> {180 let out = obj.fields_ex(inc_hidden);181 Ok(VecVal(out.into_iter().map(Val::Str).collect::<Vec<_>>()))182}183184#[jrsonnet_macros::builtin]185fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {186 Ok(obj.has_field_ex(f, inc_hidden))187}188189#[jrsonnet_macros::builtin]190fn builtin_parse_json(s: IStr) -> Result<Any> {191 let value: serde_json::Value = serde_json::from_str(&s)192 .map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;193 Ok(Any(Val::try_from(&value)?))194}195196#[jrsonnet_macros::builtin]197fn builtin_parse_yaml(s: IStr) -> Result<Any> {198 let value = serde_yaml::Deserializer::from_str_with_quirks(199 &s,200 DeserializingQuirks { old_octals: true },201 );202 let mut out = vec![];203 for item in value {204 let value = serde_json::Value::deserialize(item)205 .map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;206 let val = Val::try_from(&value)?;207 out.push(val);208 }209 Ok(Any(if out.is_empty() {210 Val::Null211 } else if out.len() == 1 {212 out.into_iter().next().unwrap()213 } else {214 Val::Arr(out.into())215 }))216}217218#[jrsonnet_macros::builtin]219fn builtin_slice(220 indexable: IndexableVal,221 index: Option<usize>,222 end: Option<usize>,223 step: Option<usize>,224) -> Result<Any> {225 std_slice(indexable, index, end, step).map(Any)226}227228#[jrsonnet_macros::builtin]229fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {230 Ok(str.chars().skip(from as usize).take(len as usize).collect())231}232233#[jrsonnet_macros::builtin]234fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {235 primitive_equals(&a.0, &b.0)236}237238#[jrsonnet_macros::builtin]239fn builtin_equals(a: Any, b: Any) -> Result<bool> {240 equals(&a.0, &b.0)241}242243#[jrsonnet_macros::builtin]244fn builtin_modulo(a: f64, b: f64) -> Result<f64> {245 Ok(a % b)246}247248#[jrsonnet_macros::builtin]249fn builtin_mod(a: Either![f64, IStr], b: Any) -> Result<Any> {250 use Either2::*;251 Ok(Any(evaluate_mod_op(252 &match a {253 A(v) => Val::Num(v),254 B(s) => Val::Str(s),255 },256 &b.0,257 )?))258}259260#[jrsonnet_macros::builtin]261fn builtin_floor(x: f64) -> Result<f64> {262 Ok(x.floor())263}264265#[jrsonnet_macros::builtin]266fn builtin_ceil(x: f64) -> Result<f64> {267 Ok(x.ceil())268}269270#[jrsonnet_macros::builtin]271fn builtin_log(n: f64) -> Result<f64> {272 Ok(n.ln())273}274275#[jrsonnet_macros::builtin]276fn builtin_pow(x: f64, n: f64) -> Result<f64> {277 Ok(x.powf(n))278}279280#[jrsonnet_macros::builtin]281fn builtin_sqrt(x: PositiveF64) -> Result<f64> {282 Ok(x.0.sqrt())283}284285#[jrsonnet_macros::builtin]286fn builtin_sin(x: f64) -> Result<f64> {287 Ok(x.sin())288}289290#[jrsonnet_macros::builtin]291fn builtin_cos(x: f64) -> Result<f64> {292 Ok(x.cos())293}294295#[jrsonnet_macros::builtin]296fn builtin_tan(x: f64) -> Result<f64> {297 Ok(x.tan())298}299300#[jrsonnet_macros::builtin]301fn builtin_asin(x: f64) -> Result<f64> {302 Ok(x.asin())303}304305#[jrsonnet_macros::builtin]306fn builtin_acos(x: f64) -> Result<f64> {307 Ok(x.acos())308}309310#[jrsonnet_macros::builtin]311fn builtin_atan(x: f64) -> Result<f64> {312 Ok(x.atan())313}314315#[jrsonnet_macros::builtin]316fn builtin_exp(x: f64) -> Result<f64> {317 Ok(x.exp())318}319320fn frexp(s: f64) -> (f64, i16) {321 if 0.0 == s {322 (s, 0)323 } else {324 let lg = s.abs().log2();325 let x = (lg - lg.floor() - 1.0).exp2();326 let exp = lg.floor() + 1.0;327 (s.signum() * x, exp as i16)328 }329}330331#[jrsonnet_macros::builtin]332fn builtin_mantissa(x: f64) -> Result<f64> {333 Ok(frexp(x).0)334}335336#[jrsonnet_macros::builtin]337fn builtin_exponent(x: f64) -> Result<i16> {338 Ok(frexp(x).1)339}340341#[jrsonnet_macros::builtin]342fn builtin_ext_var(x: IStr) -> Result<Any> {343 Ok(Any(with_state(|s| s.settings().ext_vars.get(&x).cloned())344 .ok_or(UndefinedExternalVariable(x))?))345}346347#[jrsonnet_macros::builtin]348fn builtin_native(name: IStr) -> Result<FuncVal> {349 Ok(with_state(|s| s.settings().ext_natives.get(&name).cloned())350 .map(|v| FuncVal::Builtin(v.clone()))351 .ok_or(UndefinedExternalFunction(name))?)352}353354#[jrsonnet_macros::builtin]355fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {356 arr.filter(|val| bool::try_from(func.evaluate_simple(&[Any(val.clone())].as_slice())?))357}358359#[jrsonnet_macros::builtin]360fn builtin_map(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {361 arr.map(|val| func.evaluate_simple(&[Any(val)].as_slice()))362}363364#[jrsonnet_macros::builtin]365fn builtin_flatmap(func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {366 match arr {367 IndexableVal::Str(s) => {368 let mut out = String::new();369 for c in s.chars() {370 match func.evaluate_simple(&[c.to_string()].as_slice())? {371 Val::Str(o) => out.push_str(&o),372 _ => throw!(RuntimeError(373 "in std.join all items should be strings".into()374 )),375 };376 }377 Ok(IndexableVal::Str(out.into()))378 }379 IndexableVal::Arr(a) => {380 let mut out = Vec::new();381 for el in a.iter() {382 let el = el?;383 match func.evaluate_simple(&[Any(el)].as_slice())? {384 Val::Arr(o) => {385 for oe in o.iter() {386 out.push(oe?)387 }388 }389 _ => throw!(RuntimeError(390 "in std.join all items should be arrays".into()391 )),392 };393 }394 Ok(IndexableVal::Arr(out.into()))395 }396 }397}398399#[jrsonnet_macros::builtin]400fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {401 let mut acc = init.0;402 for i in arr.iter() {403 acc = func.evaluate_simple(&[Any(acc), Any(i?)].as_slice())?;404 }405 Ok(Any(acc))406}407408#[jrsonnet_macros::builtin]409fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {410 let mut acc = init.0;411 for i in arr.iter().rev() {412 acc = func.evaluate_simple(&[Any(i?), Any(acc)].as_slice())?;413 }414 Ok(Any(acc))415}416417#[jrsonnet_macros::builtin]418#[allow(non_snake_case)]419fn builtin_sort(arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {420 if arr.len() <= 1 {421 return Ok(arr);422 }423 Ok(ArrValue::Eager(sort::sort(424 arr.evaluated()?,425 keyF.as_ref(),426 )?))427}428429#[jrsonnet_macros::builtin]430fn builtin_format(str: IStr, vals: Any) -> Result<String> {431 std_format(str, vals.0)432}433434#[jrsonnet_macros::builtin]435fn builtin_range(from: i32, to: i32) -> Result<VecVal> {436 if to < from {437 return Ok(VecVal(Vec::new()));438 }439 let mut out = Vec::with_capacity((1 + to as usize - from as usize).max(0));440 for i in from as usize..=to as usize {441 out.push(Val::Num(i as f64));442 }443 Ok(VecVal(out))444}445446#[jrsonnet_macros::builtin]447fn builtin_char(n: u32) -> Result<char> {448 Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)449}450451#[jrsonnet_macros::builtin]452fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {453 Ok(Bytes(str.bytes().collect::<Vec<u8>>().into()))454}455456#[jrsonnet_macros::builtin]457fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {458 Ok(std::str::from_utf8(&arr.0)459 .map_err(|_| RuntimeError("bad utf8".into()))?460 .into())461}462463#[jrsonnet_macros::builtin]464fn builtin_md5(str: IStr) -> Result<String> {465 Ok(format!("{:x}", md5::compute(&str.as_bytes())))466}467468#[jrsonnet_macros::builtin]469fn builtin_trace(loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {470 eprint!("TRACE:");471 if let Some(loc) = loc.0 {472 with_state(|s| {473 let locs = s.map_source_locations(&loc.0, &[loc.1]);474 eprint!(475 " {}:{}",476 loc.0.file_name().unwrap().to_str().unwrap(),477 locs[0].line478 );479 });480 }481 eprintln!(" {}", str);482 Ok(rest) as Result<Any>483}484485#[jrsonnet_macros::builtin]486fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {487 use Either2::*;488 Ok(match input {489 A(a) => base64::encode(a.0),490 B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),491 })492}493494#[jrsonnet_macros::builtin]495fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {496 Ok(Bytes(497 base64::decode(&input.as_bytes())498 .map_err(|_| RuntimeError("bad base64".into()))?499 .into(),500 ))501}502503#[jrsonnet_macros::builtin]504fn builtin_base64_decode(input: IStr) -> Result<String> {505 let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;506 Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)507}508509#[jrsonnet_macros::builtin]510fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {511 Ok(match sep {512 IndexableVal::Arr(joiner_items) => {513 let mut out = Vec::new();514515 let mut first = true;516 for item in arr.iter() {517 let item = item?.clone();518 if let Val::Arr(items) = item {519 if !first {520 out.reserve(joiner_items.len());521 // TODO: extend522 for item in joiner_items.iter() {523 out.push(item?);524 }525 }526 first = false;527 out.reserve(items.len());528 // TODO: extend529 for item in items.iter() {530 out.push(item?);531 }532 } else {533 throw!(RuntimeError(534 "in std.join all items should be arrays".into()535 ));536 }537 }538539 IndexableVal::Arr(out.into())540 }541 IndexableVal::Str(sep) => {542 let mut out = String::new();543544 let mut first = true;545 for item in arr.iter() {546 let item = item?.clone();547 if let Val::Str(item) = item {548 if !first {549 out += &sep;550 }551 first = false;552 out += &item;553 } else {554 throw!(RuntimeError(555 "in std.join all items should be strings".into()556 ));557 }558 }559560 IndexableVal::Str(out.into())561 }562 })563}564565#[jrsonnet_macros::builtin]566fn builtin_escape_string_json(str_: IStr) -> Result<String> {567 Ok(escape_string_json(&str_))568}569570#[jrsonnet_macros::builtin]571fn builtin_manifest_json_ex(572 value: Any,573 indent: IStr,574 newline: Option<IStr>,575 key_val_sep: Option<IStr>,576) -> Result<String> {577 let newline = newline.as_deref().unwrap_or("\n");578 let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");579 manifest_json_ex(580 &value.0,581 &ManifestJsonOptions {582 padding: &indent,583 mtype: ManifestType::Std,584 newline,585 key_val_sep,586 },587 )588}589590#[jrsonnet_macros::builtin]591fn builtin_manifest_yaml_doc(592 value: Any,593 indent_array_in_object: Option<bool>,594 quote_keys: Option<bool>,595) -> Result<String> {596 manifest_yaml_ex(597 &value.0,598 &ManifestYamlOptions {599 padding: " ",600 arr_element_padding: if indent_array_in_object.unwrap_or(false) {601 " "602 } else {603 ""604 },605 quote_keys: quote_keys.unwrap_or(true),606 },607 )608}609610#[jrsonnet_macros::builtin]611fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {612 Ok(value.reversed())613}614615#[jrsonnet_macros::builtin]616const fn builtin_id(v: Any) -> Result<Any> {617 Ok(v)618}619620#[jrsonnet_macros::builtin]621fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {622 Ok(str.replace(&from as &str, &to as &str))623}624625#[jrsonnet_macros::builtin]626fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {627 use Either2::*;628 Ok(VecVal(Cc::new(match maxsplits {629 A(n) => str630 .splitn(n + 1, &c as &str)631 .map(|s| Val::Str(s.into()))632 .collect(),633 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),634 })))635}636637#[jrsonnet_macros::builtin]638fn builtin_ascii_upper(str: IStr) -> Result<String> {639 Ok(str.to_ascii_uppercase())640}641642#[jrsonnet_macros::builtin]643fn builtin_ascii_lower(str: IStr) -> Result<String> {644 Ok(str.to_ascii_lowercase())645}646647#[jrsonnet_macros::builtin]648fn builtin_member(arr: IndexableVal, x: Any) -> Result<bool> {649 match arr {650 IndexableVal::Str(s) => {651 let x: IStr = IStr::try_from(x.0)?;652 Ok(!x.is_empty() && s.contains(&*x))653 }654 IndexableVal::Arr(a) => {655 for item in a.iter() {656 let item = item?;657 if equals(&item, &x.0)? {658 return Ok(true);659 }660 }661 Ok(false)662 }663 }664}665666#[jrsonnet_macros::builtin]667fn builtin_count(arr: Vec<Any>, v: Any) -> Result<usize> {668 let mut count = 0;669 for item in arr.iter() {670 if equals(&item.0, &v.0)? {671 count += 1;672 }673 }674 Ok(count)675}676677#[jrsonnet_macros::builtin]678fn builtin_any(arr: ArrValue) -> Result<bool> {679 for v in arr.iter() {680 let v: bool = v?.try_into()?;681 if v {682 return Ok(true);683 }684 }685 Ok(false)686}687688#[jrsonnet_macros::builtin]689fn builtin_all(arr: ArrValue) -> Result<bool> {690 for v in arr.iter() {691 let v: bool = v?.try_into()?;692 if !v {693 return Ok(false);694 }695 }696 Ok(true)697}crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -285,7 +285,7 @@
}
/// Specialization, provides faster TryFrom<VecVal> for Val
-pub struct VecVal(pub Vec<Val>);
+pub struct VecVal(pub Cc<Vec<Val>>);
impl Typed for VecVal {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);
@@ -296,7 +296,7 @@
fn try_from(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
- Val::Arr(a) => Ok(Self(a.evaluated()?.to_vec())),
+ Val::Arr(a) => Ok(Self(a.evaluated()?)),
_ => unreachable!(),
}
}
@@ -305,7 +305,7 @@
type Error = LocError;
fn try_from(value: VecVal) -> Result<Self> {
- Ok(Self::Arr(value.0.into()))
+ Ok(Self::Arr(ArrValue::Eager(value.0)))
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -178,11 +178,17 @@
Lazy(Cc<Vec<LazyVal>>),
Eager(Cc<Vec<Val>>),
Extended(Box<(Self, Self)>),
+ Range(i32, i32),
+ Reversed(Box<Self>),
}
impl ArrValue {
pub fn new_eager() -> Self {
Self::Eager(Cc::new(Vec::new()))
}
+ pub fn new_range(a: i32, b: i32) -> Self {
+ assert!(a <= b);
+ Self::Range(a, b)
+ }
pub fn len(&self) -> usize {
match self {
@@ -190,6 +196,8 @@
Self::Lazy(l) => l.len(),
Self::Eager(e) => e.len(),
Self::Extended(v) => v.0.len() + v.1.len(),
+ Self::Range(a, b) => a.abs_diff(*b) as usize,
+ Self::Reversed(i) => i.len(),
}
}
@@ -218,6 +226,19 @@
v.1.get(index - a_len)
}
}
+ Self::Range(a, _) => {
+ if index >= self.len() {
+ return Ok(None);
+ }
+ Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))
+ }
+ Self::Reversed(v) => {
+ let len = v.len();
+ if index >= len {
+ return Ok(None);
+ }
+ v.get(len - index - 1)
+ }
}
}
@@ -236,6 +257,21 @@
v.1.get_lazy(index - a_len)
}
}
+ Self::Range(a, _) => {
+ if index >= self.len() {
+ return None;
+ }
+ Some(LazyVal::new_resolved(Val::Num(
+ ((*a as isize) + index as isize) as f64,
+ )))
+ }
+ Self::Reversed(v) => {
+ let len = v.len();
+ if index >= len {
+ return None;
+ }
+ v.get_lazy(len - index - 1)
+ }
}
}
@@ -263,46 +299,50 @@
}
Cc::new(out)
}
+ Self::Range(a, b) => {
+ let mut out = Vec::with_capacity(self.len());
+ for i in *a..*b {
+ out.push(Val::Num(i as f64));
+ }
+ Cc::new(out)
+ }
+ Self::Reversed(r) => {
+ let mut r = r.evaluated()?;
+ Cc::update_with(&mut r, |v| v.reverse());
+ r
+ }
})
}
pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
- (0..self.len()).map(move |idx| match self {
+ // if let Self::Reversed(v) = self {
+ // return v.iter().rev();
+ // }
+ let len = self.len();
+ (0..len).map(move |idx| match self {
Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),
Self::Lazy(l) => l[idx].evaluate(),
Self::Eager(e) => Ok(e[idx].clone()),
Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),
+ Self::Range(..) => self.get(idx).map(|e| e.unwrap()),
+ Self::Reversed(..) => self.get(len - idx - 1).map(|e| e.unwrap()),
})
}
pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
- (0..self.len()).map(move |idx| match self {
+ let len = self.len();
+ (0..len).map(move |idx| match self {
Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),
Self::Lazy(l) => l[idx].clone(),
Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
Self::Extended(_) => self.get_lazy(idx).unwrap(),
+ Self::Range(..) => self.get_lazy(idx).unwrap(),
+ Self::Reversed(..) => self.get_lazy(len - idx - 1).unwrap(),
})
}
pub fn reversed(self) -> Self {
- match self {
- Self::Bytes(b) => {
- let mut out = b.to_vec();
- out.reverse();
- Self::Bytes(out.into())
- }
- Self::Lazy(vec) => {
- let mut out = (&vec as &Vec<_>).clone();
- out.reverse();
- Self::Lazy(Cc::new(out))
- }
- Self::Eager(vec) => {
- let mut out = (&vec as &Vec<_>).clone();
- out.reverse();
- Self::Eager(Cc::new(out))
- }
- Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),
- }
+ Self::Reversed(Box::new(self))
}
pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {