Pārlūkot izejas kodu

Leverage math bitx

Darien Raymond 8 gadi atpakaļ
vecāks
revīzija
c33d7f0e21
1 mainītis faili ar 2 papildinājumiem un 13 dzēšanām
  1. 2 13
      common/net/ipnet.go

+ 2 - 13
common/net/ipnet.go

@@ -1,13 +1,10 @@
 package net
 
 import (
+	"math/bits"
 	"net"
 )
 
-var (
-	onesCount = make(map[byte]byte)
-)
-
 type IPNet struct {
 	cache map[uint32]byte
 }
@@ -30,7 +27,7 @@ func ipToUint32(ip net.IP) uint32 {
 func ipMaskToByte(mask net.IPMask) byte {
 	value := byte(0)
 	for _, b := range []byte(mask) {
-		value += onesCount[b]
+		value += byte(bits.OnesCount8(b))
 	}
 	return value
 }
@@ -83,11 +80,3 @@ func (n *IPNet) Contains(ip net.IP) bool {
 func (n *IPNet) IsEmpty() bool {
 	return len(n.cache) == 0
 }
-
-func init() {
-	value := byte(0)
-	for mask := byte(1); mask <= 8; mask++ {
-		value += 1 << byte(8-mask)
-		onesCount[value] = mask
-	}
-}