change growth factor to doube

pull/1/head
Emir Pasic 11 years ago
parent 48ad2d72e6
commit 133677039b

@ -151,7 +151,7 @@ type Interface interface {
#####ArrayList #####ArrayList
This structure implements the List interface and is backed by a dynamic array that grows and shrinks implicitly (by 50% when capacity is reached). This structure implements the List interface and is backed by a dynamic array that grows and shrinks implicitly (by 100% when capacity is reached).
Direct access method _Get(index)_ is guaranteed a constant time performance. Remove is of linear time performance. Checking with _Contains()_ is of quadratic complexity. Direct access method _Get(index)_ is guaranteed a constant time performance. Remove is of linear time performance. Checking with _Contains()_ is of quadratic complexity.

@ -47,8 +47,8 @@ type List struct {
} }
const ( const (
GROWTH_FACTOR = float32(1.5) // growth by 50% GROWTH_FACTOR = float32(2.0) // growth by 100%
SHRINK_FACTOR = float32(0.5) // shrink when size is 50% of capacity (0 means never shrink) SHRINK_FACTOR = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
) )
// Instantiates a new empty list // Instantiates a new empty list
@ -165,7 +165,7 @@ func (list *List) resize(cap int) {
// Expand the array if necessary, i.e. capacity will be reached if we add n elements // Expand the array if necessary, i.e. capacity will be reached if we add n elements
func (list *List) growBy(n int) { func (list *List) growBy(n int) {
// Grow when capacity is reached by a factor of 1.5 and add number of elements // When capacity is reached, grow by a factor of GROWTH_FACTOR and add number of elements
currentCapacity := cap(list.elements) currentCapacity := cap(list.elements)
if list.size+n >= currentCapacity { if list.size+n >= currentCapacity {
newCapacity := int(GROWTH_FACTOR * float32(currentCapacity+n)) newCapacity := int(GROWTH_FACTOR * float32(currentCapacity+n))

Loading…
Cancel
Save