-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.go
66 lines (57 loc) · 1.24 KB
/
insert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package golist
import (
"github.com/emylincon/golist/core"
)
// Insert :
// inserts an element at a given location. returns error if index is out of range
func (arr *List) Insert(element interface{}, index int) error {
if index > arr.Len() {
return ErrIndexOutOfRange
}
switch list := arr.list.(type) {
case []int:
item, ok := element.(int)
if !ok {
return ErrTypeNotSame
}
arr.list = *core.InsertInt(&list, item, index)
return nil
case []int32:
item, ok := element.(int32)
if !ok {
return ErrTypeNotSame
}
arr.list = *core.InsertInt32(&list, item, index)
return nil
case []int64:
item, ok := element.(int64)
if !ok {
return ErrTypeNotSame
}
arr.list = *core.InsertInt64(&list, item, index)
return nil
case []float32:
item, ok := element.(float32)
if !ok {
return ErrTypeNotSame
}
arr.list = *core.InsertFloat32(&list, item, index)
return nil
case []float64:
item, ok := element.(float64)
if !ok {
return ErrTypeNotSame
}
arr.list = *core.InsertFloat64(&list, item, index)
return nil
case []string:
item, ok := element.(string)
if !ok {
return ErrTypeNotSame
}
arr.list = *core.InsertString(&list, item, index)
return nil
default:
return ErrTypeNotsupported
}
}