Comments (7)
Can you describe what you mean by "the range of the A address list"? Perhaps you might provide an example?
from ipaddress.
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.
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.
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.
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.
The answer to your question is yes.
from ipaddress.
thanks~
from ipaddress.
Related Issues (20)
- How to quickly find all objects that may be associated with a specific IP address using IPAddressTrie? HOT 1
- Some ideas about trie trees HOT 2
- [Help]How to check the single IPv6 address valid. HOT 1
- Issue with isZero and isMax HOT 4
- Improve OpenSSF Scorecard Score HOT 2
- Loop through IPAddress list that contains both v4 and v6 HOT 5
- "192.168.1" being parsed to "192.168.0.1" HOT 1
- Questions about CRID format HOT 2
- How to quickly find all objects that may be associated with a specific IP address using IPAddressTrie by longest match? HOT 8
- bug? HOT 4
- isSubnetContainsOtherSubnet HOT 2
- new IPAddressBitsDivision leads to infinite loop HOT 12
- IPv6, Net Mask, contains() HOT 1
- AddressDivisionBase dependent on jdk 9 HOT 3
- Too short IP range when bits are set in first host address HOT 4
- Is there a method for parse string from IPAddress::toFullString back to IPAddress HOT 2
- IPAddressString Support Format startIP-endIP for IP range HOT 1
- isValid() method returns True for non-standard IP address notation HOT 3
- After upgrade to spring boot 2.7 i'm getting two ip match HOT 1
- Can `AddressValueException(String message)` be made public? HOT 2
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.