1use std::collections::{HashMap, HashSet};
4use std::sync::Arc;
5
6use crate::data::source::{collect_source, copy_source_string, reserve_source};
7use crate::data::{binding::VectorSource, DataSourceInfo, RawKey};
8use crate::Error;
9
10#[derive(Debug, Clone)]
12pub struct SourceRows {
13 rows: Arc<[usize]>,
14 source: VectorSource,
15}
16
17impl SourceRows {
18 pub fn new(rows: impl IntoIterator<Item = usize>) -> Result<Self, Error> {
20 let rows = collect_source(rows.into_iter().map(Ok))?;
21 let mut seen = HashSet::new();
22 crate::operation::check()?;
23 seen.try_reserve(rows.len())
24 .map_err(|error| Error::Data(format!("source-row identity storage: {error}").into()))?;
25 for &row in &rows {
26 crate::operation::check()?;
27 if !seen.insert(row) {
28 return Err(Error::Data(
29 format!("source-row identities must be unique; found duplicate {row}").into(),
30 ));
31 }
32 }
33 crate::operation::check()?;
34 Ok(Self {
35 source: VectorSource::direct(rows.len()),
36 rows: rows.into(),
37 })
38 }
39
40 #[doc(hidden)]
42 pub fn with_source(mut self, source: DataSourceInfo) -> crate::Result<Self> {
43 self.source = VectorSource::repeated(source, 1, self.len())?;
44 Ok(self)
45 }
46
47 #[doc(hidden)]
49 pub fn get_source(&self) -> &DataSourceInfo {
50 self.source.info()
51 }
52
53 pub(crate) fn vector_source(&self) -> &VectorSource {
54 &self.source
55 }
56
57 #[must_use]
59 pub fn len(&self) -> usize {
60 self.rows.len()
61 }
62
63 #[must_use]
65 pub fn is_empty(&self) -> bool {
66 self.rows.is_empty()
67 }
68
69 #[must_use]
71 pub fn as_slice(&self) -> &[usize] {
72 &self.rows
73 }
74
75 #[must_use]
77 pub fn get(&self, index: usize) -> Option<usize> {
78 self.rows.get(index).copied()
79 }
80
81 pub fn iter(&self) -> impl ExactSizeIterator<Item = usize> + '_ {
83 self.rows.iter().copied()
84 }
85}
86
87impl PartialEq for SourceRows {
88 fn eq(&self, other: &Self) -> bool {
89 self.rows == other.rows
90 }
91}
92
93impl Eq for SourceRows {}
94
95#[derive(Debug, Clone, PartialEq, Eq)]
96enum Validity {
97 All,
98 Bitmap(Arc<[u64]>),
99}
100
101impl Validity {
102 fn from_flags(flags: &[bool]) -> Self {
103 if flags.iter().all(|valid| *valid) {
104 return Self::All;
105 }
106 Self::bitmap(flags, vec![0_u64; flags.len().div_ceil(64)])
107 }
108
109 fn try_from_flags(flags: &[bool]) -> crate::Result<Self> {
110 crate::operation::check()?;
111 if flags.iter().filter(|&&valid| valid).count() == flags.len() {
112 return Ok(Self::All);
113 }
114 let count = flags.len().div_ceil(64);
115 let mut words = Vec::new();
116 reserve_source(&mut words, count)?;
117 words.resize(count, 0);
118 crate::operation::check()?;
119 Ok(Self::bitmap(flags, words))
120 }
121
122 fn bitmap(flags: &[bool], mut words: Vec<u64>) -> Self {
123 for (index, valid) in flags.iter().copied().enumerate() {
124 if valid {
125 words[index / 64] |= 1_u64 << (index % 64);
126 }
127 }
128 Self::Bitmap(words.into())
129 }
130
131 fn is_valid(&self, index: usize) -> bool {
132 match self {
133 Self::All => true,
134 Self::Bitmap(words) => words
135 .get(index / 64)
136 .is_some_and(|word| word & (1_u64 << (index % 64)) != 0),
137 }
138 }
139}
140
141#[derive(Debug, Clone)]
143pub struct NumericValues {
144 name: Arc<str>,
145 values: Arc<[f64]>,
146 validity: Validity,
147 field_key: Option<Arc<(RawKey, Arc<str>)>>,
148 source: VectorSource,
149}
150
151impl NumericValues {
152 pub(crate) fn with_value_name(mut self, name: &str) -> Self {
153 self.name = Arc::from(name);
154 self.field_key = None;
155 self
156 }
157 #[must_use]
159 pub fn new(name: impl Into<String>, values: impl IntoIterator<Item = f64>) -> Self {
160 let values = values.into_iter().collect::<Vec<_>>();
161 Self {
162 name: Arc::from(name.into()),
163 source: VectorSource::direct(values.len()),
164 values: values.into(),
165 validity: Validity::All,
166 field_key: None,
167 }
168 }
169
170 #[must_use]
172 pub fn nullable(
173 name: impl Into<String>,
174 values: impl IntoIterator<Item = Option<f64>>,
175 ) -> Self {
176 let values = values.into_iter().collect::<Vec<_>>();
177 let validity =
178 Validity::from_flags(&values.iter().map(Option::is_some).collect::<Vec<_>>());
179 let values = values
180 .into_iter()
181 .map(|value| value.unwrap_or(0.0))
182 .collect::<Vec<_>>();
183 Self {
184 name: Arc::from(name.into()),
185 source: VectorSource::direct_nullable(
186 values.len(),
187 !matches!(&validity, Validity::All),
188 ),
189 values: values.into(),
190 validity,
191 field_key: None,
192 }
193 }
194
195 #[must_use]
197 pub fn name(&self) -> &str {
198 self.field_key
199 .as_deref()
200 .map_or(&self.name, |(_, name)| name)
201 }
202
203 #[doc(hidden)]
205 pub fn raw_name(&self) -> &str {
206 &self.name
207 }
208
209 pub(crate) fn raw_field(&self) -> crate::data::category::RawField {
210 let key = self
211 .field_key()
212 .cloned()
213 .unwrap_or_else(|| RawKey::from(self.raw_name()));
214 crate::data::category::RawField::from_key(Arc::clone(&self.name), key)
215 }
216
217 #[doc(hidden)]
219 #[must_use]
220 pub fn with_field_key(mut self, key: RawKey, name: impl Into<String>) -> Self {
221 self.field_key = Some(Arc::new((key, Arc::from(name.into()))));
222 self
223 }
224
225 #[doc(hidden)]
227 pub fn field_key(&self) -> Option<&RawKey> {
228 self.field_key.as_deref().map(|(key, _)| key)
229 }
230
231 #[doc(hidden)]
233 pub fn with_source(self, source: DataSourceInfo) -> crate::Result<Self> {
234 self.with_repeated_source(source, 1)
235 }
236
237 #[doc(hidden)]
239 pub fn with_repeated_source(
240 mut self,
241 source: DataSourceInfo,
242 copies: usize,
243 ) -> crate::Result<Self> {
244 self.source = VectorSource::repeated(source, copies, self.len())?.with_original_validity(
245 self.len(),
246 self.has_nulls(),
247 |slot| self.validity.is_valid(slot),
248 )?;
249 Ok(self)
250 }
251
252 #[doc(hidden)]
254 pub fn with_source_slots(
255 mut self,
256 source: DataSourceInfo,
257 slots: Vec<usize>,
258 ) -> crate::Result<Self> {
259 self.source = VectorSource::gathered(source, slots, self.len())?.with_original_validity(
260 self.len(),
261 self.has_nulls(),
262 |slot| self.validity.is_valid(slot),
263 )?;
264 Ok(self)
265 }
266
267 #[doc(hidden)]
269 pub fn get_source(&self) -> &DataSourceInfo {
270 self.source.info()
271 }
272
273 pub(crate) fn with_row_scope(mut self) -> Self {
274 self.source.mark_rows();
275 self
276 }
277
278 pub(crate) fn vector_source(&self) -> &VectorSource {
279 &self.source
280 }
281
282 pub(crate) fn has_nulls(&self) -> bool {
283 !matches!(self.validity, Validity::All)
284 }
285
286 #[doc(hidden)]
288 pub fn source_slot(&self, index: usize) -> Option<usize> {
289 (index < self.len()).then(|| self.source.slot(index))
290 }
291
292 pub(crate) fn select(&self, indices: &[usize]) -> crate::Result<Self> {
293 let values = collect_source(indices.iter().map(|&index| {
294 self.values.get(index).copied().ok_or_else(|| {
295 Error::Data(format!("numeric selection index {index} is out of bounds").into())
296 })
297 }))?;
298 let validity = if matches!(self.validity, Validity::All) {
299 Validity::All
300 } else {
301 let flags = collect_source(
302 indices
303 .iter()
304 .map(|&index| Ok(self.validity.is_valid(index))),
305 )?;
306 Validity::try_from_flags(&flags)?
307 };
308 crate::operation::check()?;
309 Ok(Self {
310 name: self.name.clone(),
311 values: values.into(),
312 validity,
313 field_key: self.field_key.clone(),
314 source: self.source.select(indices)?,
315 })
316 }
317
318 #[must_use]
320 pub fn len(&self) -> usize {
321 self.values.len()
322 }
323
324 #[must_use]
326 pub fn is_empty(&self) -> bool {
327 self.values.is_empty()
328 }
329
330 #[must_use]
333 pub fn get(&self, row: usize) -> Option<Option<f64>> {
334 self.values
335 .get(row)
336 .map(|value| self.validity.is_valid(row).then_some(*value))
337 }
338
339 pub fn iter(&self) -> impl ExactSizeIterator<Item = Option<f64>> + '_ {
341 (0..self.len()).map(|row| self.get(row).expect("in-bounds source slot"))
342 }
343
344 #[must_use]
346 pub fn is_valid(&self, row: usize) -> Option<bool> {
347 (row < self.len()).then(|| self.validity.is_valid(row))
348 }
349}
350
351impl PartialEq for NumericValues {
352 fn eq(&self, other: &Self) -> bool {
353 self.name == other.name
354 && self.field_key == other.field_key
355 && self.len() == other.len()
356 && self
357 .iter()
358 .map(|value| value.map(f64::to_bits))
359 .eq(other.iter().map(|value| value.map(f64::to_bits)))
360 }
361}
362
363impl Eq for NumericValues {}
364
365#[derive(Debug, Clone)]
367pub struct CategoryValues {
368 name: Arc<str>,
369 levels: Arc<[String]>,
370 codes: Arc<[u32]>,
371 validity: Validity,
372 original_keys: Option<Arc<crate::data::KeyValues>>,
373 field_key: Option<Arc<(RawKey, Arc<str>)>>,
374 source: VectorSource,
375}
376
377impl CategoryValues {
378 pub fn new<S>(
380 name: impl Into<String>,
381 values: impl IntoIterator<Item = S>,
382 ) -> Result<Self, Error>
383 where
384 S: Into<String>,
385 {
386 Self::from_optional(
387 name.into(),
388 values.into_iter().map(|value| Ok(Some(value.into()))),
389 )
390 }
391
392 pub fn nullable<S>(
394 name: impl Into<String>,
395 values: impl IntoIterator<Item = Option<S>>,
396 ) -> Result<Self, Error>
397 where
398 S: Into<String>,
399 {
400 Self::from_optional(
401 name.into(),
402 values.into_iter().map(|value| Ok(value.map(Into::into))),
403 )
404 }
405
406 fn from_optional(
407 name: String,
408 values: impl IntoIterator<Item = Result<Option<String>, Error>>,
409 ) -> Result<Self, Error> {
410 let values = collect_source(values.into_iter())?;
411 let mut levels = Vec::new();
412 let mut indices = HashMap::new();
413 let mut codes = Vec::new();
414 let mut valid = Vec::new();
415 reserve_source(&mut codes, values.len())?;
416 reserve_source(&mut valid, values.len())?;
417 for value in values {
418 crate::operation::check()?;
419 let Some(value) = value else {
420 valid.push(false);
421 codes.push(0);
422 continue;
423 };
424 valid.push(true);
425 let code = if let Some(code) = indices.get(&value).copied() {
426 code
427 } else {
428 let code = u32::try_from(levels.len()).map_err(|_| {
429 Error::Data(
430 format!(
431 "categorical values {name:?} exceed the supported distinct-level count"
432 )
433 .into(),
434 )
435 })?;
436 reserve_source(&mut levels, 1)?;
437 crate::operation::check()?;
438 indices.try_reserve(1).map_err(|error| {
439 Error::Data(format!("category dictionary storage: {error}").into())
440 })?;
441 indices.insert(copy_source_string(&value)?, code);
442 levels.push(value);
443 code
444 };
445 codes.push(code);
446 }
447 let validity = Validity::try_from_flags(&valid)?;
448 crate::operation::check()?;
449 Ok(Self {
450 name: Arc::from(name),
451 levels: levels.into(),
452 source: VectorSource::direct_nullable(codes.len(), !matches!(validity, Validity::All)),
453 codes: codes.into(),
454 validity,
455 original_keys: None,
456 field_key: None,
457 })
458 }
459
460 #[must_use]
462 pub fn name(&self) -> &str {
463 &self.name
464 }
465
466 #[must_use]
468 pub fn len(&self) -> usize {
469 self.codes.len()
470 }
471
472 #[must_use]
474 pub fn is_empty(&self) -> bool {
475 self.codes.is_empty()
476 }
477
478 #[must_use]
481 pub fn get(&self, row: usize) -> Option<Option<&str>> {
482 self.codes.get(row).map(|code| {
483 if !self.validity.is_valid(row) {
484 return None;
485 }
486 usize::try_from(*code)
487 .ok()
488 .and_then(|index| self.levels.get(index))
489 .map(String::as_str)
490 })
491 }
492
493 pub fn iter(&self) -> impl ExactSizeIterator<Item = Option<&str>> + '_ {
495 (0..self.len()).map(|row| self.get(row).expect("in-bounds source slot"))
496 }
497
498 #[must_use]
500 pub fn is_valid(&self, row: usize) -> Option<bool> {
501 (row < self.len()).then(|| self.validity.is_valid(row))
502 }
503
504 #[must_use]
506 pub fn levels(&self) -> &[String] {
507 &self.levels
508 }
509
510 #[doc(hidden)]
514 pub fn with_key_values(mut self, keys: crate::data::KeyValues) -> Result<Self, Error> {
515 if self.len() != keys.len()
516 || (0..self.len()).any(|index| self.is_valid(index) != keys.is_valid(index))
517 {
518 return Err(Error::Data(
519 "raw keys must align with their category projection".into(),
520 ));
521 }
522 let key_count = match &keys {
523 crate::data::KeyValues::Strings(values) => values.levels().len(),
524 crate::data::KeyValues::Raw(values) => values.levels().len(),
525 };
526 let mut first_slots = Vec::new();
527 reserve_source(&mut first_slots, self.levels.len())?;
528 first_slots.resize(self.levels.len(), None);
529 let aligned = key_count == self.levels.len()
532 && self.codes.iter().enumerate().all(|(row, &code)| {
533 if !self.validity.is_valid(row) {
534 return true;
535 }
536 let first = *first_slots[code as usize].get_or_insert(row);
537 match &keys {
538 crate::data::KeyValues::Strings(values) => values.get(first) == values.get(row),
539 crate::data::KeyValues::Raw(values) => values.get(first) == values.get(row),
540 }
541 });
542 if !aligned {
543 return Err(Error::Data(
544 "category projection must preserve the distinct original keys".into(),
545 ));
546 }
547 let source = match &keys {
548 crate::data::KeyValues::Strings(values) => values.vector_source(),
549 crate::data::KeyValues::Raw(values) => values.vector_source(),
550 };
551 if self.source.info() == source.info() {
552 self.source.retain_original_validity(source);
553 }
554 self.original_keys = Some(Arc::new(keys));
555 Ok(self)
556 }
557
558 #[doc(hidden)]
561 #[must_use]
562 pub fn with_field_key(mut self, key: crate::data::RawKey, name: impl Into<String>) -> Self {
563 self.field_key = Some(Arc::new((key, Arc::from(name.into()))));
564 self
565 }
566
567 #[doc(hidden)]
569 pub fn original_name(&self) -> &str {
570 self.field_key
571 .as_deref()
572 .map_or(&self.name, |(_, name)| name)
573 }
574
575 #[doc(hidden)]
577 pub fn field_key(&self) -> Option<&RawKey> {
578 self.field_key.as_deref().map(|(key, _)| key)
579 }
580
581 #[doc(hidden)]
583 pub fn with_source(self, source: DataSourceInfo) -> crate::Result<Self> {
584 self.with_repeated_source(source, 1)
585 }
586
587 #[doc(hidden)]
589 pub fn with_repeated_source(
590 mut self,
591 source: DataSourceInfo,
592 copies: usize,
593 ) -> crate::Result<Self> {
594 self.source = VectorSource::repeated(source, copies, self.len())?.with_original_validity(
595 self.len(),
596 self.has_nulls(),
597 |slot| self.validity.is_valid(slot),
598 )?;
599 Ok(self)
600 }
601
602 #[doc(hidden)]
604 pub fn with_source_slots(
605 mut self,
606 source: DataSourceInfo,
607 slots: Vec<usize>,
608 ) -> crate::Result<Self> {
609 self.source = VectorSource::gathered(source, slots, self.len())?.with_original_validity(
610 self.len(),
611 self.has_nulls(),
612 |slot| self.validity.is_valid(slot),
613 )?;
614 Ok(self)
615 }
616
617 #[doc(hidden)]
619 pub fn with_cycled_source(mut self, source: DataSourceInfo) -> crate::Result<Self> {
620 self.source = VectorSource::cycled(source, self.len())?.with_original_validity(
621 self.len(),
622 self.has_nulls(),
623 |slot| self.validity.is_valid(slot),
624 )?;
625 Ok(self)
626 }
627
628 #[doc(hidden)]
630 pub fn get_source(&self) -> &DataSourceInfo {
631 self.source.info()
632 }
633
634 pub(crate) fn with_row_scope(mut self) -> Self {
635 self.source.mark_rows();
636 self
637 }
638
639 pub(crate) fn vector_source(&self) -> &VectorSource {
640 &self.source
641 }
642
643 pub(crate) fn has_nulls(&self) -> bool {
644 !matches!(self.validity, Validity::All)
645 }
646
647 #[doc(hidden)]
649 pub fn source_slot(&self, index: usize) -> Option<usize> {
650 (index < self.len()).then(|| self.source.slot(index))
651 }
652
653 pub(crate) fn select(&self, indices: &[usize]) -> crate::Result<Self> {
654 let mut values = Self::from_optional(
655 copy_source_string(self.name())?,
656 indices.iter().map(|&index| {
657 let value = self.get(index).ok_or_else(|| {
658 Error::Data(format!("category selection index {index} is out of bounds").into())
659 })?;
660 value.map(copy_source_string).transpose()
661 }),
662 )?;
663 values.field_key.clone_from(&self.field_key);
664 values.source = self.source.select(indices)?;
665 values.original_keys = self
666 .original_keys
667 .as_ref()
668 .map(|keys| keys.select(indices).map(Arc::new))
669 .transpose()?;
670 Ok(values)
671 }
672
673 pub(crate) fn original_field(&self) -> crate::data::RawKey {
674 self.field_key.as_deref().map_or_else(
675 || crate::data::RawKey::from(self.name()),
676 |(key, _)| key.clone(),
677 )
678 }
679
680 #[doc(hidden)]
682 #[must_use]
683 pub fn with_field_name(mut self, name: &str) -> Self {
684 if self.name() != name {
685 self.name = Arc::from(name);
686 }
687 self.field_key = None;
688 self
689 }
690
691 #[must_use]
694 pub fn key_values(&self) -> crate::data::KeyValues {
695 if let Some(keys) = &self.original_keys {
696 return match keys.as_ref() {
697 crate::data::KeyValues::Strings(values) => {
698 let mut values = values.clone();
699 values.source = self.source.clone();
700 values.field_key.clone_from(&self.field_key);
701 crate::data::KeyValues::Strings(values)
702 }
703 crate::data::KeyValues::Raw(values) => crate::data::KeyValues::Raw(
704 values
705 .clone()
706 .with_name(self.original_name())
707 .with_vector_source(self.source.clone()),
708 ),
709 };
710 }
711 let mut values = self.clone();
712 if let Some((_, name)) = values.field_key.as_deref() {
713 values.name = Arc::clone(name);
714 }
715 crate::data::KeyValues::Strings(values)
716 }
717
718 pub(crate) fn key_for_level(&self, level: &str) -> Option<RawKey> {
720 if self.original_keys.is_none() {
721 return self
722 .levels()
723 .iter()
724 .any(|value| value == level)
725 .then(|| RawKey::from(level));
726 }
727 self.key_at(self.iter().position(|value| value == Some(level))?)
728 }
729
730 pub(crate) fn key_at(&self, slot: usize) -> Option<RawKey> {
731 match self.original_keys.as_deref() {
732 None => self.get(slot)?.map(RawKey::from),
733 Some(crate::data::KeyValues::Strings(values)) => values.get(slot)?.map(RawKey::from),
734 Some(crate::data::KeyValues::Raw(values)) => values.get(slot)?.cloned(),
735 }
736 }
737
738 pub(crate) fn raw_field(&self) -> crate::data::category::RawField {
739 crate::data::category::RawField::from_key(Arc::clone(&self.name), self.original_field())
740 }
741
742 pub(crate) fn same_level_key(&self, level: &str, other: &Self, other_level: &str) -> bool {
743 if self.original_keys.is_none() && other.original_keys.is_none() {
744 level == other_level
745 } else {
746 self.key_for_level(level) == other.key_for_level(other_level)
747 }
748 }
749
750 pub(crate) fn raw_category(
751 &self,
752 level: &str,
753 ) -> crate::Result<crate::data::category::RawCategory> {
754 use crate::data::category::{shared_text, RawCategory};
755 let key = self.key_for_level(level).ok_or_else(|| {
756 Error::Render(format!("guide level {level:?} has no original category").into())
757 })?;
758 if matches!(&key, RawKey::String(value) if value == level) {
759 RawCategory::new(level)
760 } else {
761 Ok(RawCategory::from_key(shared_text(level)?, key))
762 }
763 }
764}
765
766impl PartialEq for CategoryValues {
767 fn eq(&self, other: &Self) -> bool {
768 self.name == other.name
769 && self.levels == other.levels
770 && self.codes == other.codes
771 && self.validity == other.validity
772 && self.original_keys == other.original_keys
773 && self.field_key == other.field_key
774 }
775}
776
777impl Eq for CategoryValues {}
778
779#[derive(Debug, Clone)]
781pub struct BooleanValues {
782 name: Arc<str>,
783 values: Arc<[bool]>,
784 validity: Validity,
785 field_key: Option<Arc<(RawKey, Arc<str>)>>,
786 source: VectorSource,
787}
788
789impl BooleanValues {
790 pub(crate) fn with_value_name(mut self, name: &str) -> Self {
791 self.name = Arc::from(name);
792 self.field_key = None;
793 self
794 }
795 #[must_use]
797 pub fn new(name: impl Into<String>, values: impl IntoIterator<Item = bool>) -> Self {
798 let values = values.into_iter().collect::<Vec<_>>();
799 Self {
800 name: Arc::from(name.into()),
801 field_key: None,
802 source: VectorSource::direct(values.len()),
803 values: values.into(),
804 validity: Validity::All,
805 }
806 }
807
808 #[must_use]
810 pub fn nullable(
811 name: impl Into<String>,
812 values: impl IntoIterator<Item = Option<bool>>,
813 ) -> Self {
814 let values = values.into_iter().collect::<Vec<_>>();
815 let validity =
816 Validity::from_flags(&values.iter().map(Option::is_some).collect::<Vec<_>>());
817 let values = values
818 .into_iter()
819 .map(|value| value.unwrap_or(false))
820 .collect::<Vec<_>>();
821 Self {
822 name: Arc::from(name.into()),
823 field_key: None,
824 source: VectorSource::direct_nullable(
825 values.len(),
826 !matches!(&validity, Validity::All),
827 ),
828 values: values.into(),
829 validity,
830 }
831 }
832
833 #[must_use]
835 pub fn name(&self) -> &str {
836 self.field_key
837 .as_deref()
838 .map_or(&self.name, |(_, name)| name)
839 }
840
841 #[doc(hidden)]
843 #[must_use]
844 pub fn with_field_key(mut self, key: RawKey, name: impl Into<String>) -> Self {
845 self.field_key = Some(Arc::new((key, Arc::from(name.into()))));
846 self
847 }
848
849 #[doc(hidden)]
851 pub fn field_key(&self) -> Option<&RawKey> {
852 self.field_key.as_deref().map(|(key, _)| key)
853 }
854
855 #[doc(hidden)]
857 pub fn with_source(self, source: DataSourceInfo) -> crate::Result<Self> {
858 self.with_repeated_source(source, 1)
859 }
860
861 #[doc(hidden)]
863 pub fn with_repeated_source(
864 mut self,
865 source: DataSourceInfo,
866 copies: usize,
867 ) -> crate::Result<Self> {
868 self.source = VectorSource::repeated(source, copies, self.len())?.with_original_validity(
869 self.len(),
870 self.has_nulls(),
871 |slot| self.validity.is_valid(slot),
872 )?;
873 Ok(self)
874 }
875
876 #[doc(hidden)]
878 pub fn with_source_slots(
879 mut self,
880 source: DataSourceInfo,
881 slots: Vec<usize>,
882 ) -> crate::Result<Self> {
883 self.source = VectorSource::gathered(source, slots, self.len())?.with_original_validity(
884 self.len(),
885 self.has_nulls(),
886 |slot| self.validity.is_valid(slot),
887 )?;
888 Ok(self)
889 }
890
891 #[doc(hidden)]
893 pub fn get_source(&self) -> &DataSourceInfo {
894 self.source.info()
895 }
896
897 pub(crate) fn with_row_scope(mut self) -> Self {
898 self.source.mark_rows();
899 self
900 }
901
902 pub(crate) fn vector_source(&self) -> &VectorSource {
903 &self.source
904 }
905
906 pub(crate) fn has_nulls(&self) -> bool {
907 !matches!(self.validity, Validity::All)
908 }
909
910 #[doc(hidden)]
912 pub fn source_slot(&self, index: usize) -> Option<usize> {
913 (index < self.len()).then(|| self.source.slot(index))
914 }
915
916 #[must_use]
918 pub fn len(&self) -> usize {
919 self.values.len()
920 }
921
922 #[must_use]
924 pub fn is_empty(&self) -> bool {
925 self.values.is_empty()
926 }
927
928 #[must_use]
930 pub fn get(&self, row: usize) -> Option<Option<bool>> {
931 self.values
932 .get(row)
933 .map(|value| self.validity.is_valid(row).then_some(*value))
934 }
935
936 pub fn iter(&self) -> impl ExactSizeIterator<Item = Option<bool>> + '_ {
938 (0..self.len()).map(|row| self.get(row).expect("in-bounds source slot"))
939 }
940
941 #[must_use]
943 pub fn is_valid(&self, row: usize) -> Option<bool> {
944 (row < self.len()).then(|| self.validity.is_valid(row))
945 }
946}
947
948impl PartialEq for BooleanValues {
949 fn eq(&self, other: &Self) -> bool {
950 self.name == other.name && self.values == other.values && self.validity == other.validity
951 }
952}
953
954impl Eq for BooleanValues {}