Comments (3)
Hey, joeandraverde,
What we're doing here is making space to insert an item into our array. Given an array of:
[a, b, c, d, e]
We want to add an item at position i=2. So, we:
- grow the array so it can fit one more element, by appending a nil element to the end
Line 203 in be84af9
[a, b, c, d e, nil]
- move the elements [2:4] over one, to make space, by copying copy(s[2+1:], s[2:]) which copies from s[2:] into s[2+1:], with
Line 205 in be84af9
[a, b, c, c, d, e]
- overwrite the element at [2] with our new value 'x', with
Line 207 in be84af9
[a, b, x, c, d, e]
The one caveat to all of this is that, in cases where we're "inserting" at the end of the list, step 2 isn't necessary and in fact causes panics. If, for example, len(s) == 3
and we want to insert at 3
(also known as just appending to the end, steps 1 and 3 above work fine, but step 2 fails because it tries to copy(s[4:], s[3:])
, and s[4:]
is out of range for a len(s)==3
array.
Your comment "The check whether the index is less than the length seems like it would prevent an unnecessary copy" is correct, but it's not the API we need to provide. This implementation needs to be able to insert anywhere in the list, including at the end (where len(s) == i
).
Hope that helps!
from btree.
Let me clarify by example:
package main
type node struct {}
type children []*node
func (s *children) insertAt(index int, n *node) {
*s = append(*s, nil)
if index < len(*s) {
copy((*s)[index+1:], (*s)[index:])
}
(*s)[index] = n
}
func main() {
example := make(children, 10)
// this would insert at the end, expanding the array. OK
// length == 10, max index == 9, new index == 10 (expanding the array)
//example.insertAt(10, &node{})
// bounds ERROR
// length == 10, max index == 9, new index == 11 (overshooting the array that gets expanded by 1)
example.insertAt(11, &node{})
}
from btree.
The if
statement you've highlighted actually assumes that the passed-in index is correctly <= len(example)
, and it differentiates between:
func main() {
example := make(children, 10)
example.insertAt(10, &node{}) // if is false, no need to copy stuff
example.insertAt(5, &node{}) // if is true, we're writing in the middle of the list so need to move the stuff after the index
}
from btree.
Related Issues (20)
- Can it be done if I want to insert pair instead of int? HOT 1
- Q: how do I delete a range? HOT 6
- deleteMin appears to be O(n); not O(log(n)) as expected from a balanced binary tree. HOT 2
- Read traversal and COW HOT 1
- only one goroutine put and some other goroutines get , Is it safe ? HOT 1
- Tagged releases
- panic: runtime error: index out of range using Ascend iterator HOT 3
- Marshaling btree on disk
- Question on iteration HOT 8
- "t2" declared but "out" returned
- Go modules giving issues while importing kubernetes/cli-runtime
- Worked unexpected in go rountine? HOT 1
- What is the meaning of `copyOnWriteContext`? HOT 1
- Generic approach HOT 4
- should call Less() fewer times when iterating HOT 1
- BTree Iterators
- is it b-tree or b+tree? HOT 1
- is it b plus tree?? HOT 1
- stack overflow while deleting a key from the tree
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from btree.