pivots #1

Merged
SethTrowbridge merged 17 commits from pivots into master 2021-05-15 12:05:52 -04:00
Showing only changes of commit 11dd9f0569 - Show all commits

View File

@ -4,98 +4,152 @@
import { h, Component, render } from 'https://unpkg.com/preact?module'; import { h, Component, render } from 'https://unpkg.com/preact?module';
var N = { var N = {
ID:{
Walk:0,
Instance:0
},
Create:(inMeta, ...inChildren) => Create:(inMeta, ...inChildren) =>
{ {
var output = { var output = {
Meta:inMeta, Meta:inMeta,
Link:{
Children:[], Hierarchy: {Get:[], Set:[]},
Parents:[], ModifyUp: {Get:[], Set:[]},
/* ModifyDown:{Get:[], Set:[]},
WalkID:0, ModifyOut: {Get:[], Set:[]},
},
GetUpward:[], ID:{
GetDownward:[], Walk:0,
GetOutside:[], Instance:N.ID.Instance++
}
SetUpward:[],
SetDownward:[],
SetOutside:[]
*/
}; };
inChildren.forEach( inChild => N.Connect(output, "Children", inChild, "Parents") ); inChildren.forEach( inChild => N.Connect(output, inChild, "Hierarchy") );
return output; return output;
}, },
Connect:(inParent, inParentRefs, inChild, inChildRefs) => Flow:(inNode, inType, inForward) => (inForward === undefined || inForward === true) ? inNode.Link[inType].Set : inNode.Link[inType].Get,
Connect:(inNodeMajor, inNodeMinor, inKey) =>
{ {
inParent[inParentRefs].push(inChild); inNodeMajor.Link[inKey].Set.push(inNodeMinor);
inChild[inChildRefs].push(inParent); inNodeMinor.Link[inKey].Get.push(inNodeMajor);
}, },
Disconnect:(inParent, inParentRefs, inChild, inChildRefs) => Disconnect:(inNodeMajor, inNodeMinor, inKey) =>
{ {
let checkRemove = (inArray, inMember) => let checkRemove = (inArray, inMember) => inArray.findIndex( (inMember, inIndex, inArray) => (inMember === match) ? inArray.splice(inIndex, 1) : false );
{
inArray.findIndex( (inMember, inIndex, inArray) => (inMember === match) ? inArray.splice(inIndex, 1) : false ); // if no minor node was passed, cut all of the major node's "Set" connections under inKey (removes all "children")
}; if(inNodeMinor == null){
checkRemove(inParent[inParentRefs], inChild); let array = inNodeMajor[inKey].Set
checkRemove(inChild[inChildRefs], inParent); array.forEach( nodeMinor =>
{
checkRemove(nodeMinor.Link[inKey].Get, nodeMinor);
});
array = [];
return;
}
// if no major node was passed, cut all of the minor node's "Get" connections under inKey (removes all "parents")
if(inNodeMajor == null){
let array = inNodeMinor[inKey].Get
array.forEach( nodeMajor =>
{
checkRemove(nodeMajor.Link[inKey].Set, nodeMajor);
});
array = [];
return;
}
checkRemove(inNodeMajor.Link[inKey].Set, inNodeMinor);
checkRemove(inNodeMinor.Link[inKey].Get, inNodeMajor);
}, },
Walk:(inNode, inKey, inIterator, inWalkID) => Walk:(inIterator, inNode, inKey, inForwards) =>
{ {
let array = inNode[inKey]; let array = N.Flow(inNode, inKey, inForwards);
for(let i=0; i<array.length; i++) for(let i=0; i<array.length; i++)
{ {
let next = array[i]; let next = array[i];
if(next.WalkID !== inWalkID) if(next.ID.Walk !== N.ID.Walk)
{ {
next.WalkID = inWalkID; next.ID.Walk = N.ID.Walk;
let results = inIterator(next); let results = inIterator(next);
if(results !== false) if(results !== false)
{ {
N.Walk(next, inKey, inIterator, inWalkID); N.Walk(inIterator, next, inKey, inForwards);
} }
} }
} }
}, },
Modify(inNode) Modify(inNode)
{ {
inNode.WalkID = "tweak-"+Math.random(); N.ID.Walk++;
let leaves = []; let leaves = [];
let gatherUp = n => N.Connect(inNode, "SetUpward", n, "GetUpward"); let gatherUp = n => N.Connect(inNode, n, "ModifyUp");
let gatherDown = n => let gatherDown = n =>
{ {
N.Connect(inNode, "SetDownward", n, "GetDownward"); N.Connect(inNode, n, "ModifyDown");
n.Children.length == 0 ? leaves.push(n) : null; N.Flow(n, "Hierarchy").length == 0 ? leaves.push(n) : null;
}; };
let gatherOut = n => N.Connect(inNode, "SetOutside", n, "GetOutside"); let gatherOut = n => N.Connect(inNode, n, "ModifyOut");
N.Walk(inNode, "Parents", gatherUp, inNode.WalkID); N.Walk(gatherUp, inNode, "Hierarchy", false);
N.Connect(inNode, "SetDownward", inNode, "GetDownward"); N.Connect(inNode, inNode, "ModifyDown");
N.Walk(inNode, "Children", gatherDown, inNode.WalkID); N.Walk(gatherDown, inNode, "Hierarchy");
leaves.forEach(leaf=>N.Walk(leaf, "Parents", gatherOut, inNode.WalkID)); leaves.forEach(leaf=>N.Walk(gatherOut, leaf, "Hierarchy", false));
} }
// delete modify
// create pivot
// delete pivot
}; };
let tree1 = N.Create("root1",
N.Create("branch1",
N.Create("leaf1"),
N.Create("leaf2"),
N.Create("leaf3"),
),
N.Create("branch2",
N.Create("leaft3"),
N.Create("leaft4")
)
);
let leaves = [];
let leavesCollect = n =>
{
if(n.Children.length == 0)
{
leaves.push(n);
}
};
N.Walk(leavesCollect, tree1, "Hierarchy");
let tree2 = N.Create("root2",
N.Create("branch3",
N.Create("leaf5"),
N.Create("leaf6")
),
N.Create("branch4", ...leaves)
);
let orchard = N.Create("orchard", tree1, tree2);
N.Modify(tree1);
let Leafify = inRows => inRows.map(r => N.Create({Row:r})); let Leafify = inRows => inRows.map(r => N.Create({Row:r}));
let Pivot = (inParent, inColumnIndicies, inSumIndicies, inDepth) => let Pivot = (inParent, inColumnIndicies, inSumIndicies, inDepth) =>
{ {
/*
arguments: //arguments:
- a Node with leaf Nodes temporarily stored in its Meta.Leaves // - a Node with leaf Nodes temporarily stored in its Meta.Leaves
- where each leaf Node has a row of table data in it's Meta.Row // - where each leaf Node has a row of table data in it's Meta.Row
- a list of columns to pivot on // - a list of columns to pivot on
- a list of columns to sum // - a list of columns to sum
- optional traversal depth, defaults to 0 // - optional traversal depth, defaults to 0
*/
let depth = inDepth||0; let depth = inDepth||0;
let uniques = {}; let uniques = {};
inParent.Meta.Leaves.forEach((inLeaf)=> inParent.Meta.Leaves.forEach((inLeaf)=>
{ {
let row = inLeaf.Meta.Row; // get the raw "CSV" row out of the leaf Node's Meta let row = inLeaf.Meta.Row; // shorthand for the raw "CSV" row in the leaf Node's Meta
let value = row[inColumnIndicies[depth]]; // get the pivot column let value = row[inColumnIndicies[depth]]; // get the pivot column
let match = uniques[value]; // check in the uniques list if this pivot column exists let match = uniques[value]; // check in the uniques list if this pivot column exists
if(!match) if(!match)
@ -107,7 +161,7 @@ let Pivot = (inParent, inColumnIndicies, inSumIndicies, inDepth) =>
Leaves:[] Leaves:[]
}; };
// grow a child off of the parent using the meta object // grow a child off of the parent using the meta object
N.Connect(inParent, "Children", N.Create(match), "Parents"); N.Connect(inParent, N.Create(match), "Hierarchy");
} }
else else
{ {
@ -119,19 +173,20 @@ let Pivot = (inParent, inColumnIndicies, inSumIndicies, inDepth) =>
}); });
delete inParent.Meta.Leaves; delete inParent.Meta.Leaves;
if(depth == inColumnIndicies.length-1) var children = N.Flow(inParent, "Hierarchy");
if(depth >= inColumnIndicies.length-1)
{ {
// cant go any deeper children.forEach( inChild =>
inParent.Children.forEach( inChild =>
{ {
inChild.Meta.Leaves.forEach( inLeaf => N.Connect(inChild, "Children", inLeaf, "Parents") ); inChild.Meta.Leaves.forEach( inLeaf => N.Connect(inChild, inLeaf, "Hierarchy") );
delete inChild.Meta.Leaves; delete inChild.Meta.Leaves;
}); });
} }
else else
{ {
inParent.Children.forEach( child => Pivot(child, inColumnIndicies, inSumIndicies, depth+1) ); children.forEach( child => Pivot(child, inColumnIndicies, inSumIndicies, depth+1) );
} }
}; };
let csv = Leafify([ let csv = Leafify([
["#1", "a", "long", 1], ["#1", "a", "long", 1],
@ -151,25 +206,24 @@ let pivots = [pivotRoot1, pivotRoot2];
let ElNode = ({node}) => let ElNode = ({node}) =>
{ {
var nodeChildren = N.Flow(node, "Hierarchy");
var children = []; var children = [];
var table = []; var table = [];
if(node.Children.length)
if(node.Meta.Row)
{ {
if(node.Meta.Row) table = node.Meta.Row.map( cell => h("span", {style:{padding:"10px"}}, cell));
{
table = node.Meta.Row.map( cell => h("span", {style:{padding:"10px"}}, cell));
}
children = [
h("strong", null, node.Meta.Label||"a node"),
...table,
...node.Children.map( inChild => h(ElNode, {node:inChild}))
];
} }
else children = [
h("strong", null, node.Meta.Label||"a node"),
...table
];
if(nodeChildren.length)
{ {
children = [ children = [
h("strong", null, node.Meta.Label||"a node"), ...children,
...node.Meta.Row.map( cell => h("span", {style:{padding:"10px"}}, cell)) ...nodeChildren.map( inChild => h(ElNode, {node:inChild}))
]; ];
} }
return h("div", {style:{padding:"10px"}}, children); return h("div", {style:{padding:"10px"}}, children);
@ -191,45 +245,3 @@ let ElRoot = ({pivots}) =>
render(h(ElRoot, {pivots}, null), document.querySelector("#app")); render(h(ElRoot, {pivots}, null), document.querySelector("#app"));
</script> </script>
<!--=
<script>
let tree1 = N.Create("root1",
N.Create("branch1",
N.Create("leaf1"),
N.Create("leaf2"),
N.Create("leaf3"),
),
N.Create("branch2",
N.Create("leaft3"),
N.Create("leaft4")
)
);
let leaves = [];
let leavesCollect = n =>
{
if(n.Children.length == 0)
{
leaves.push(n);
}
};
N.Walk(tree1, "Children", leavesCollect, 1);
let tree2 = N.Create("root2",
N.Create("branch3",
N.Create("leaf5"),
N.Create("leaf6")
),
N.Create("branch4", ...leaves)
);
let orchard = N.Create("orchard", tree1, tree2);
/*************************************************************/
N.Modify(tree1);
console.log(tree1);
</script>
-->