Git Product home page Git Product logo

Comments (7)

seancfoley avatar seancfoley commented on June 3, 2024

Can you describe what you mean by "the range of the A address list"? Perhaps you might provide an example?

from ipaddress.

seancfoley avatar seancfoley commented on June 3, 2024

If the addresses in list A were CIDR subnets or individual addresses, and you wanted to know if an address in list B were contained by any of the subnets or addresses in list A, then you could use this example.

But I'm not entirely sure that's what you're asking.

If both lists were CIDR subnets or individual addresses, and you wanted to know if a subnet or address in list B intersection with any of the subnets or addresses in A, then you could use this example

from ipaddress.

Jump2nj avatar Jump2nj commented on June 3, 2024

List listA = new ArrayList<>();
listA.add("103.42.76.0/24");
listA.add("111.30.182.128/25");
listA.add("221.181.97.128/25");

List listB = new ArrayList<>();
listB.add("103.42.76.33/32");
listB.add("103.42.76.103/32");
listB.add("111.30.182.211/32");
listB.add("120.221.25.23/30");
listB.add("117.187.129.203/29");

For example, the above-mentioned address list A and list b. There is no intersection between all CIDR blocks and a single address in A, and there is no intersection between all CIDR blocks and a single address in B. I want to get the CIDR block and single address contained in list A in list B. The expected result of the above example is:
103.42.76.33/32, 103.42.76.103/32, 111.30.182.211/32

From your code example, I know it can be satisfied in this way:
List blocks = new ArrayList<>();
for (String str : listA) {
blocks.add(new IPAddressString(str).getAddress());
}

IPv4AddressTrie ipv4AddressTrie = new IPv4AddressTrie();
for (IPAddress block : blocks) {
ipv4AddressTrie.add(block.toIPv4());
}

listB.stream().forEach(item -> {
IPAddressString toFindAddrStr = new IPAddressString(item);
IPv4Address toFindBlockFor = toFindAddrStr.getAddress().toIPv4();
if (ipv4AddressTrie.elementContains(toFindBlockFor)) {
System.out.println(toFindBlockFor.toString());
}
});
But this method needs to loop through list B. I would like to get your help. Is there a more efficient way to satisfy it, such as creating a tree for both list A and list B, and comparing the two trees?

from ipaddress.

seancfoley avatar seancfoley commented on June 3, 2024

I am not aware of a way to compare two tries in a manner that is more efficient (ie scales better) than the code above. The code is O(m+n) where m and n are the sizes of the two lists. This is more efficient than comparing every element in the first list with every element in the other, which is O(m*n). However, I think it's unlikely you can do better than O(m+n), I'd guess that any algorithm would need to be at least proportional to the number of elements in the two lists.

I'd also note that your code is not quite right if you want to find all intersections, you need to check for both containing and contained blocks in the trie to find all intersections:

if (ipv4AddressTrie.elementContains(toFindBlockFor) || ipv4AddressTrie.elementsContainedBy(toFindBlockFor) != null) {
    System.out.println(toFindBlockFor.toString());
}

from ipaddress.

Jump2nj avatar Jump2nj commented on June 3, 2024

Thanks for your answer. I also want to ask a question。
if (ipv4AddressTrie.elementsContaining(toFindBlockFor) != null || ipv4AddressTrie.elementsContainedBy(toFindBlockFor) != null{ System.out.println(toFindBlockFor.toString()); }
Is this code also satisfying to find all intersections?

from ipaddress.

seancfoley avatar seancfoley commented on June 3, 2024

The answer to your question is yes.

from ipaddress.

Jump2nj avatar Jump2nj commented on June 3, 2024

thanks~

from ipaddress.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.