123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- package array
- import (
- "bytes"
- "fmt"
- "reflect"
- )
- type Array struct {
- data []interface{}
- size int
- }
- // 构造函数,传入数组的容量capacity构造Array
- func New(capacity int) *Array {
- return &Array{
- data: make([]interface{}, capacity),
- }
- }
- // 获取数组的容量
- func (a *Array) GetCapacity() int {
- return len(a.data)
- }
- // 获得数组中的元素个数
- func (a *Array) GetSize() int {
- return a.size
- }
- // 返回数组是否为空
- func (a *Array) IsEmpty() bool {
- return a.size == 0
- }
- // 在第 index 个位置插入一个新元素 e
- func (a *Array) Add(index int, e interface{}) {
- if index < 0 || index > a.size {
- panic("add failed, index out of range")
- }
- if a.size == len(a.data) {
- a.resize(2 * a.size)
- }
- for i := a.size - 1; i >= index; i-- {
- a.data[i+1] = a.data[i]
- }
- a.data[index] = e
- a.size++
- }
- // 向所有元素后添加一个新元素
- func (a *Array) AddLast(e interface{}) {
- a.Add(a.size, e)
- }
- // 向所有元素前添加一个新元素
- func (a *Array) AddFirst(e interface{}) {
- a.Add(0, e)
- }
- // 获取 index 索引位置的元素
- func (a *Array) Get(index int) interface{} {
- a.checkIndex(index)
- return a.data[index]
- }
- // 修改 index 索引位置的元素
- func (a *Array) Set(index int, e interface{}) {
- a.checkIndex(index)
- a.data[index] = e
- }
- // 查找数组中是否有元素 e
- func (a *Array) Contains(e interface{}) bool {
- for i := 0; i < a.size; i++ {
- if Compare(a.data[i], e) == 0 {
- return true
- }
- }
- return false
- }
- // 查找数组中元素 e 所在的索引,不存在则返回 -1
- func (a *Array) Find(e interface{}) int {
- for i := 0; i < a.size; i++ {
- if Compare(a.data[i], e) == 0 {
- return i
- }
- }
- return -1
- }
- // 查找数组中元素 e 所有的索引组成的切片,不存在则返回 -1
- func (a *Array) FindAll(e interface{}) (indexes []int) {
- for i := 0; i < a.size; i++ {
- if Compare(a.data[i], e) == 0 {
- indexes = append(indexes, i)
- }
- }
- return
- }
- // 从数组中删除 index 位置的元素,返回删除的元素
- func (a *Array) Remove(index int) interface{} {
- a.checkIndex(index)
- e := a.data[index]
- for i := index + 1; i < a.size; i++ {
- a.data[i-1] = a.data[i]
- }
- a.size--
- a.data[a.size] = nil //loitering object != memory leak
- // 考虑边界条件,避免长度为 1 时,resize 为 0
- if a.size == len(a.data)/4 && len(a.data)/2 != 0 {
- a.resize(len(a.data) / 2)
- }
- return e
- }
- // 从数组中删除第一个元素,返回删除的元素
- func (a *Array) RemoveFirst() interface{} {
- return a.Remove(0)
- }
- // 从数组中删除最后一个元素,返回删除的元素
- func (a *Array) RemoveLast() interface{} {
- return a.Remove(a.size - 1)
- }
- // 从数组中删除一个元素 e
- func (a *Array) RemoveElement(e interface{}) bool {
- index := a.Find(e)
- if index == -1 {
- return false
- }
- a.Remove(index)
- return true
- }
- // 从数组中删除所有元素 e
- func (a *Array) RemoveAllElement(e interface{}) bool {
- if a.Find(e) == -1 {
- return false
- }
- for i := 0; i < a.size; i++ {
- if Compare(a.data[i], e) == 0 {
- a.Remove(i)
- }
- }
- return true
- }
- // 为数组扩容
- func (a *Array) resize(newCapacity int) {
- newData := make([]interface{}, newCapacity)
- for i := 0; i < a.size; i++ {
- newData[i] = a.data[i]
- }
- a.data = newData
- }
- func (a *Array) checkIndex(index int) {
- if index < 0 || index >= a.size {
- panic(fmt.Sprintf("index is out of range, required range: [0, %d) but get %d", a.size, index))
- }
- }
- // 重写 Array 的 string 方法
- func (a *Array) String() string {
- var buffer bytes.Buffer
- buffer.WriteString(fmt.Sprintf("Array: size = %d, capacity = %d\n", a.size, len(a.data)))
- buffer.WriteString("[")
- for i := 0; i < a.size; i++ {
- // fmt.Sprint 将 interface{} 类型转换为字符串
- buffer.WriteString(fmt.Sprint(a.data[i]))
- if i != (a.size - 1) {
- buffer.WriteString(", ")
- }
- }
- buffer.WriteString("]")
- return buffer.String()
- }
- func Compare(a interface{}, b interface{}) int {
- aType := reflect.TypeOf(a).String()
- bType := reflect.TypeOf(b).String()
- if aType != bType {
- panic("cannot compare different type params")
- }
- switch a.(type) {
- case int:
- if a.(int) > b.(int) {
- return 1
- } else if a.(int) < b.(int) {
- return -1
- } else {
- return 0
- }
- case string:
- if a.(string) > b.(string) {
- return 1
- } else if a.(string) < b.(string) {
- return -1
- } else {
- return 0
- }
- case float64:
- if a.(float64) > b.(float64) {
- return 1
- } else if a.(float64) < b.(float64) {
- return -1
- } else {
- return 0
- }
- default:
- panic("unsupported type params")
- }
- }
- func (a *Array) Swap(i int, j int) {
- if i < 0 || i >= a.size || j < 0 || j >= a.size {
- panic("index is out of range")
- }
- a.data[i], a.data[j] = a.data[j], a.data[i]
- }
|