Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Last active January 20, 2023 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwhatcott/39f9c0d1bbd825e7b4e0364e02d6cfa0 to your computer and use it in GitHub Desktop.
Save mdwhatcott/39f9c0d1bbd825e7b4e0364e02d6cfa0 to your computer and use it in GitHub Desktop.
type Slice struct {
Data []int
Length int
Capacity int
}
func Insert(record *Slice, position int, value int) {
capacity := record.Length + 1
if position > capacity {
capacity = position
}
CheckLength(record, capacity)
record.Length++
for i := record.Length; i >= position; i-- {
record.Data[i] = record.Data[i-1]
}
record.Data[position] = value
}
func CheckLength(record *Slice, length int) {
if length <= record.Capacity {
return
}
capacity := record.Capacity << 1
if capacity < length {
capacity = length
}
data := make([]int, capacity)
for i := 0; i < record.Length; i++ {
data[i] = record.Data[i]
}
record.Data = data
record.Capacity = capacity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment