github.com/ethersphere/bee/v2@v2.2.0/pkg/swarm/utilities.go (about) 1 // Copyright 2022 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package swarm 6 7 import "bytes" 8 9 // ContainsAddress reports whether a is present in addrs. 10 func ContainsAddress(addrs []Address, a Address) bool { 11 return IndexOfAddress(addrs, a) != -1 12 } 13 14 // RemoveAddress removes first occurrence of a in addrs, returning the modified slice. 15 func RemoveAddress(addrs []Address, a Address) []Address { 16 i := IndexOfAddress(addrs, a) 17 if i == -1 { 18 return addrs 19 } 20 21 return append(addrs[:i], addrs[i+1:]...) 22 } 23 24 // IndexOfAddress returns the index of the first occurrence of a in addrs, 25 // or -1 if not present. 26 func IndexOfAddress(addrs []Address, a Address) int { 27 for i, v := range addrs { 28 if v.Equal(a) { 29 return i 30 } 31 } 32 return -1 33 } 34 35 // IndexOfChunkWithAddress returns the index of the first occurrence of 36 // Chunk with Address a in chunks, or -1 if not present. 37 func IndexOfChunkWithAddress(chunks []Chunk, a Address) int { 38 for i, c := range chunks { 39 if c != nil && a.Equal(c.Address()) { 40 return i 41 } 42 } 43 return -1 44 } 45 46 // ContainsChunkWithAddress reports whether Chunk with Address a is present in chunks. 47 func ContainsChunkWithAddress(chunks []Chunk, a Address) bool { 48 return IndexOfChunkWithAddress(chunks, a) != -1 49 } 50 51 // ContainsChunkWithAddress reports whether Chunk with data d is present in chunks. 52 func ContainsChunkWithData(chunks []Chunk, d []byte) bool { 53 for _, c := range chunks { 54 if c != nil && bytes.Equal(c.Data(), d) { 55 return true 56 } 57 } 58 return false 59 } 60 61 // FindStampWithBatchID returns the first occurrence of Stamp having the same batchID. 62 func FindStampWithBatchID(stamps []Stamp, batchID []byte) (Stamp, bool) { 63 for _, s := range stamps { 64 if s != nil && bytes.Equal(s.BatchID(), batchID) { 65 return s, true 66 } 67 } 68 return nil, false 69 }