seancfoley / IPAddress

Java library for handling IP addresses and subnets, both IPv4 and IPv6
https://seancfoley.github.io/IPAddress/
Apache License 2.0
469 stars 63 forks source link

Returns the subset of arguments in the Trie #101

Closed anan-2019 closed 1 year ago

anan-2019 commented 1 year ago

i have a Trie is:

○ 0.0.0.0/0 (4) └─○ 127.9.23.0/29 (4) ├─○ 127.9.23.0/30 (3) │ ├─● 127.9.23.1 = 23 (1) │ └─○ 127.9.23.2/31 (2) │ ├─● 127.9.23.2 = 21 (1) │ └─● 127.9.23.3 = 22 (1) └─● 127.9.23.4 = 24 (1)

now i want to add 127.9.23.0/30=11 to the Trie and i want get the subset of 127.9.23.0/30=11 in Trie │ ├─● 127.9.23.1 = 23 (1) │ └─○ 127.9.23.2/31 (2) │ ├─● 127.9.23.2 = 21 (1) │ └─● 127.9.23.3 = 22 (1) └─● 127.9.23.4 = 24 (1)

before real added it,cause i want to check if i can add it

seancfoley commented 1 year ago

I'm not sure this answers your question.

Create that trie:

static void add(IPv4AddressAssociativeTrie<Integer> trie, String addrStr, int num) {
    trie.put(getAddress(addrStr), num);
}

static IPv4Address getAddress(String addrStr) {
    return new IPAddressString(addrStr).getAddress().toIPv4();
}

IPv4AddressAssociativeTrie<Integer> trie = new IPv4AddressAssociativeTrie<Integer>();
add(trie, "127.9.23.1", 23);
add(trie, "127.9.23.2", 21);
add(trie, "127.9.23.3", 22);
add(trie, "127.9.23.4", 24);
System.out.println(trie);
System.out.println();

IPv4Address addr = getAddress("127.9.23.0/30");

You can either use a subtrie or submap to get a subset.

static void subMap(IPv4AddressAssociativeTrie<Integer> trie, IPv4Address addr) {
    AddressTrieMap<IPv4Address, Integer> map = trie.asMap();
    AddressTrieMap<IPv4Address, Integer> subMap = map.subMapFromKeysContainedBy(addr);
    System.out.println("sub map is " + subMap);
    System.out.println("sub map contains " + addr + ": " + subMap.containsKey(addr));
    System.out.println();
}

static void subTrie(IPv4AddressAssociativeTrie<Integer> trie, IPv4Address addr) {
    IPv4AssociativeTrieNode<Integer> subTrie = trie.elementsContainedBy(addr);
    //IPv4AddressAssociativeTrie<Integer> x = subTrie.asNewTrie(); // duplicates nodes to produce a new trie
    System.out.print("sub trie is " + subTrie.toTreeString(true, true));
    System.out.println("sub trie contains " + addr + ": " + trie.contains(addr));
    System.out.println();
}

subMap(trie, addr);
subTrie(trie, addr);

// add the address to the trie and repeat   
System.out.println();
System.out.println("adding " + addr);
trie.put(addr, 11);

subMap(trie, addr);
subTrie(trie, addr);

Output:

○ 0.0.0.0/0 (4)
└─○ 127.9.23.0/29 (4)
  ├─○ 127.9.23.0/30 (3)
  │ ├─● 127.9.23.1 = 23 (1)
  │ └─○ 127.9.23.2/31 (2)
  │   ├─● 127.9.23.2 = 21 (1)
  │   └─● 127.9.23.3 = 22 (1)
  └─● 127.9.23.4 = 24 (1)

sub map is {127.9.23.1=23, 127.9.23.2=21, 127.9.23.3=22}
sub map contains 127.9.23.0/30: false

sub trie is 
○ 127.9.23.0/30 (3)
├─● 127.9.23.1 = 23 (1)
└─○ 127.9.23.2/31 (2)
  ├─● 127.9.23.2 = 21 (1)
  └─● 127.9.23.3 = 22 (1)
sub trie contains 127.9.23.0/30: false

adding 127.9.23.0/30
sub map is {127.9.23.1=23, 127.9.23.0/30=11, 127.9.23.2=21, 127.9.23.3=22}
sub map contains 127.9.23.0/30: true

That's how you can get subsets and check for containment.

anan-2019 commented 1 year ago

yeah, you are right, thank you very much