Last active
January 20, 2023 16:37
-
-
Save mdwhatcott/39f9c0d1bbd825e7b4e0364e02d6cfa0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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