/// subcategories.
pub root: Arc<Group>,
- /// All of the leaves reachable via the root.
- ///
- /// The indexing for `data_leaves` is that used for `idx` in [Cell], thus
- /// `data_leaves[i]->data_index == i`. This might differ from what an
- /// in-order traversal of `root` would yield, if the user reordered
- /// categories.
- data_leaves: Vec<Arc<Leaf>>,
-
/// Ordering of leaves for presentation.
///
/// This is a permutation of `0..n` where `n` is the number of leaves.
}
pub fn len(&self) -> usize {
- self.data_leaves.len()
+ self.root.len()
+ }
+
+ pub fn nth_leaf(&self, index: usize) -> Option<&Leaf> {
+ self.root.nth_leaf(index)
}
pub fn builder(axis: Axis3, root: GroupBuilder) -> DimensionBuilder {
#[derive(Clone, Debug)]
pub struct Group {
parent: Option<Weak<Group>>,
+ len: usize,
name: Box<Value>,
/// The child categories.
pub fn parent(&self) -> Option<Arc<Group>> {
self.parent.as_ref().map(|parent| parent.upgrade().unwrap())
}
+
+ pub fn nth_leaf(&self, mut index: usize) -> Option<&Leaf> {
+ for child in &self.children {
+ let len = child.len();
+ if index < len {
+ return child.nth_leaf(index);
+ }
+ index -= len;
+ }
+ None
+ }
+
+ pub fn len(&self) -> usize {
+ self.len
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
}
#[derive(Clone)]
impl DimensionBuilder {
pub fn new(axis: Axis3, root: GroupBuilder) -> Self {
- let mut leaves = Vec::with_capacity(root.len());
- let root = root.build(None, &mut leaves);
+ let root = root.build(None);
Self {
axis,
dimension: Dimension {
+ presentation_order: (0..root.len()).collect(),
root,
- presentation_order: (0..leaves.len()).collect(),
- data_leaves: leaves,
hide_all_labels: false,
},
}
fn len(&self) -> usize {
self.children.iter().map(|category| category.len()).sum()
}
- fn build(self, parent: Option<Weak<Group>>, leaves: &mut Vec<Arc<Leaf>>) -> Arc<Group> {
+ fn build(self, parent: Option<Weak<Group>>) -> Arc<Group> {
Arc::new_cyclic(|weak| Group {
+ len: self.children.iter().map(|c| c.len()).sum(),
name: self.name,
children: self
.children
.into_iter()
- .map(|c| c.build(weak.clone(), leaves))
+ .map(|c| c.build(weak.clone()))
.collect(),
show_label: {
// By default, nested group labels are shown, but not dimension root labels.
CategoryBuilder::Leaf { .. } => 1,
}
}
- fn build(self, parent: Weak<Group>, leaves: &mut Vec<Arc<Leaf>>) -> Category {
+ fn build(self, parent: Weak<Group>) -> Category {
match self {
- Self::Group(group) => Category::Group(group.build(Some(parent), leaves)),
+ Self::Group(group) => Category::Group(group.build(Some(parent))),
Self::Leaf { name, class } => {
let leaf = Arc::new(Leaf {
parent,
name,
class,
});
- leaves.push(leaf.clone());
Category::Leaf(leaf)
}
}
}
impl Category {
- fn is_leaf(&self) -> bool {
+ fn len(&self) -> usize {
match self {
- Category::Group(_) => false,
- Category::Leaf(_) => true,
+ Category::Group(group) => group.len,
+ Category::Leaf(leaf) => 1,
}
}
- fn is_group(&self) -> bool {
+ fn nth_leaf(&self, index: usize) -> Option<&Leaf> {
match self {
- Category::Group(_) => true,
- Category::Leaf(_) => false,
+ Category::Group(group) => group.nth_leaf(index),
+ Category::Leaf(leaf) => {
+ if index == 0 {
+ Some(&leaf)
+ } else {
+ None
+ }
+ }
}
}
Category::Leaf(_) => true,
}
}
-
- fn ptr_eq(&self, other: &Self) -> bool {
- match (self, other) {
- (Category::Group(a), Category::Group(b)) => Arc::ptr_eq(a, b),
- (Category::Leaf(a), Category::Leaf(b)) => Arc::ptr_eq(a, b),
- _ => false,
- }
- }
}
trait CategoryTrait {