You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.
I started my career as an embedded systems engineer which meant that I had a long opportunity to become an expert at bit-wise operations. I've created an algorithm that dramatically improves the performance of finding (or in this case calculating) the full roots for a flat-tree - the reference implementation (in Javascript) iterates over all nodes in the tree with an O(n) performance while the new algorithm is based on the number of bits needed to store the record count and has O(n * log2(1)) performance.
If you'd like a paragraph describing this optimization, feel free to assign this issue to me.
Possible Solution
Additional optimization is possible as the roots for a tree only change when records are appended. It's also possible to cache the root list for a tree of a given size after calculating it and then use that value for any tree of the same size.
Code Sample
Here's my Go code for calculating the list of full roots for a flat-tree. Note that the size of the underlying array is retrieved as len(t.Nodes) but it would be possible to create the same function with a signature of func Roots(size int) []int to match the signature of the Rust example given in the book.
/*Roots calculates the list of "full roots" for the FlatTree. */func (tFlatTree) Roots() []int {
recs:= (len(t.Nodes) +1) >>1roots:= []int{}
// If there are an odd number of leaf nodes (records) then the last// record is also a root and can be calculated as the value of the// other set bits times two.ifrecs&1!=0 {
recs^=1roots=append(roots, recs<<1)
}
// For the rest of the set bits, the root can be calculated as the// as the sum of the current bit minus one plus the value of the// other set bits times two. If the current bit is zero, the loop// short circuits.forbit:=2; recs>0; bit<<=1 {
ifrecs&bit==0 {
continue
}
recs^=bitroots=append([]int{(recs<<1) + (bit-1)}, roots...)
}
returnroots
}
The text was updated successfully, but these errors were encountered:
smoyer64
changed the title
Add optimization for full roots
Add optimization for full_roots function
Feb 7, 2021
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
**🙋 feature request
I started my career as an embedded systems engineer which meant that I had a long opportunity to become an expert at bit-wise operations. I've created an algorithm that dramatically improves the performance of finding (or in this case calculating) the full roots for a flat-tree - the reference implementation (in Javascript) iterates over all nodes in the tree with an O(n) performance while the new algorithm is based on the number of bits needed to store the record count and has O(n * log2(1)) performance.
If you'd like a paragraph describing this optimization, feel free to assign this issue to me.
Possible Solution
Additional optimization is possible as the roots for a tree only change when records are appended. It's also possible to cache the root list for a tree of a given size after calculating it and then use that value for any tree of the same size.
Code Sample
Here's my
Go
code for calculating the list of full roots for a flat-tree. Note that the size of the underlying array is retrieved aslen(t.Nodes)
but it would be possible to create the same function with a signature offunc Roots(size int) []int
to match the signature of the Rust example given in the book.The text was updated successfully, but these errors were encountered: