You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sparse.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2017 Couchbase, Inc.
  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 regexp
  15. type sparseSet struct {
  16. dense []uint
  17. sparse []uint
  18. size uint
  19. }
  20. func newSparseSet(size uint) *sparseSet {
  21. return &sparseSet{
  22. dense: make([]uint, size),
  23. sparse: make([]uint, size),
  24. size: 0,
  25. }
  26. }
  27. func (s *sparseSet) Len() int {
  28. return int(s.size)
  29. }
  30. func (s *sparseSet) Add(ip uint) uint {
  31. i := s.size
  32. s.dense[i] = ip
  33. s.sparse[ip] = i
  34. s.size++
  35. return i
  36. }
  37. func (s *sparseSet) Get(i uint) uint {
  38. return s.dense[i]
  39. }
  40. func (s *sparseSet) Contains(ip uint) bool {
  41. i := s.sparse[ip]
  42. return i < s.size && s.dense[i] == ip
  43. }
  44. func (s *sparseSet) Clear() {
  45. s.size = 0
  46. }