further container hardening
This commit is contained in:
parent
065ccf19e5
commit
268be80ccc
1 changed files with 9 additions and 6 deletions
|
@ -98,9 +98,10 @@ func (c *Container) Scan(visit VisitFunc) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add adds an item to the end of the container, if not already present.
|
// Add adds an item to the end of the container, if not already present.
|
||||||
|
// Adding nil, or a component already present in the container, does nothing.
|
||||||
// Add is _not_ safe to call on a nil *Container.
|
// Add is _not_ safe to call on a nil *Container.
|
||||||
func (c *Container) Add(component interface{}) {
|
func (c *Container) Add(component interface{}) {
|
||||||
if c.Contains(component) {
|
if component == nil || c.Contains(component) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.reverse[component] = len(c.items)
|
c.reverse[component] = len(c.items)
|
||||||
|
@ -109,6 +110,7 @@ func (c *Container) Add(component interface{}) {
|
||||||
|
|
||||||
// Remove replaces an item with nil. If the number of nil items is greater than
|
// Remove replaces an item with nil. If the number of nil items is greater than
|
||||||
// half the slice, the slice is compacted (indexes of items will change).
|
// half the slice, the slice is compacted (indexes of items will change).
|
||||||
|
// Removing an item not in the Container does nothing.
|
||||||
// Remove is safe to call on a nil *Container.
|
// Remove is safe to call on a nil *Container.
|
||||||
func (c *Container) Remove(component interface{}) {
|
func (c *Container) Remove(component interface{}) {
|
||||||
if c == nil {
|
if c == nil {
|
||||||
|
@ -188,15 +190,16 @@ func (c *Container) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// compact moves all the items to the front of the items slice, removing any
|
// compact moves all the items to the front of the items slice, removing any
|
||||||
// free slots, and resets the free counter.
|
// free slots. The underlying array remains the same size.
|
||||||
func (c *Container) compact() {
|
func (c *Container) compact() {
|
||||||
i := 0
|
i := 0
|
||||||
for _, x := range c.items {
|
for _, x := range c.items {
|
||||||
if x != nil {
|
if x == nil {
|
||||||
c.items[i] = x
|
continue
|
||||||
c.reverse[x] = i
|
|
||||||
i++
|
|
||||||
}
|
}
|
||||||
|
c.items[i] = x
|
||||||
|
c.reverse[x] = i
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
c.items = c.items[:i]
|
c.items = c.items[:i]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue