array.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2021 gorse Project Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package base
  15. import "reflect"
  16. const batchSize = 1024 * 1024
  17. type Array[T any] struct {
  18. Data [][]T
  19. }
  20. func (a *Array[T]) Len() int {
  21. if len(a.Data) == 0 {
  22. return 0
  23. }
  24. return len(a.Data)*batchSize - batchSize + len(a.Data[len(a.Data)-1])
  25. }
  26. func (a *Array[T]) Get(index int) T {
  27. return a.Data[index/batchSize][index%batchSize]
  28. }
  29. func (a *Array[T]) Append(val T) {
  30. if len(a.Data) == 0 || len(a.Data[len(a.Data)-1]) == batchSize {
  31. a.Data = append(a.Data, make([]T, 0, batchSize))
  32. }
  33. a.Data[len(a.Data)-1] = append(a.Data[len(a.Data)-1], val)
  34. }
  35. func (a *Array[T]) Bytes() int {
  36. // The memory usage of Array[T] consists of:
  37. // 1. struct
  38. // 2. slices in s.Data[*]
  39. // 3. elements in s.Data[*][*]
  40. bytes := reflect.TypeOf(a).Elem().Size()
  41. if len(a.Data) > 0 {
  42. bytes += reflect.TypeOf(a.Data).Elem().Size() * uintptr(cap(a.Data))
  43. bytes += reflect.TypeOf(a.Data).Elem().Elem().Size() * uintptr(len(a.Data)) * batchSize
  44. }
  45. return int(bytes)
  46. }