Comments (2)
Addressed by #1 (comment)
from ipaddress-go.
The following code works identically and produces identical output to your python code, except for the fact I am hard-coding the command-line args rather than using go's "flags" package:
package main
import (
"fmt"
"github.com/seancfoley/ipaddress-go/ipaddr"
)
func main() {
taken := []*ipaddr.IPAddress{addr("10.255.255.192/26"), addr("10.255.255.128/30")}
main := addr("10.255.255.0/24")
available := get_available_subnets(main, taken)
fmt.Println("\nAvailable IP space:")
for _, avl := range available {
fmt.Println(avl)
}
}
func get_available_subnets(mainSubnet *ipaddr.IPAddress, taken []*ipaddr.IPAddress) []*ipaddr.IPAddress {
var available, q []*ipaddr.IPAddress
q = append(q, mainSubnet)
matched := false
for len(q) > 0 {
subnet := q[0]
q = q[1:]
for _, taken_subnet := range taken {
if matched = subnet.Equal(taken_subnet); matched {
// found matching subnet in taken, stop expanding
fmt.Printf("similar: %s and %s\n", subnet, taken_subnet)
break
}
if matched = subnet.Contains(taken_subnet); matched {
// still has overlaps somewhere in children, keep expanding
fmt.Printf("overlaps: %s and %s\n", subnet, taken_subnet)
iter := subnet.AdjustPrefixLen(1).PrefixBlockIterator()
for iter.HasNext() {
sub_subnet := iter.Next()
q = append(q, sub_subnet)
}
break
}
}
if !matched {
// no overlaps with taken - this subnet is entirely available
available = append(available, subnet)
}
}
return available
}
func addr(addrStr string) *ipaddr.IPAddress {
return ipaddr.NewIPAddressString(addrStr).GetAddress()
}
from ipaddress-go.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ipaddress-go.