/** * See {@link #getBitAtPosition(long, int)}. Equivalent to {@code getBitAtPosition(ip.asLong(), * position)} */ public static boolean getBitAtPosition(Ip ip, int position) { return getBitAtPosition(ip.asLong(), position); }
public boolean containsPathFromPrefix(long bits, int prefixLength, int depth) { if (prefixLength == depth) { if (depth == 0 && _prefix == null) { return false; } else { return true; } } else { boolean currentBit = Ip.getBitAtPosition(bits, depth); if (currentBit) { if (_right == null) { return false; } else { return _right.containsPathFromPrefix(bits, prefixLength, depth + 1); } } else { if (_left == null) { return false; } else { return _left.containsPathFromPrefix(bits, prefixLength, depth + 1); } } } }
@Test public void testGetBitAtPosition() { assertThat(getBitAtPosition(0L, 0), is(false)); assertThat(getBitAtPosition(0L, 31), is(false)); assertThat(getBitAtPosition(1L, 31), is(true)); assertThat(getBitAtPosition(0xFF000000L, 7), is(true)); assertThat(getBitAtPosition(0xFF000000L, 8), is(false)); for (int i = 0; i < 32; i++) { assertThat(getBitAtPosition(Ip.MAX.asLong(), i), equalTo(true)); } }
private void addPrefix( Prefix prefix, Set<String> devices, long bits, int prefixLength, int depth) { if (prefixLength == depth) { _prefix = prefix; if (_devices == null) { _devices = devices; } else { _devices.addAll(devices); } } else { boolean currentBit = Ip.getBitAtPosition(bits, depth); if (_devices != null) { devices.addAll(_devices); } if (currentBit) { if (_right == null) { _right = new ByteTrieNode(); } _right.addPrefix(prefix, devices, bits, prefixLength, depth + 1); } else { if (_left == null) { _left = new ByteTrieNode(); } _left.addPrefix(prefix, devices, bits, prefixLength, depth + 1); } } }
public Prefix getLongestPrefixMatch(Ip address, long bits, int index) { Prefix longestPrefixMatch = getLongestPrefixMatch(address); if (index == Prefix.MAX_PREFIX_LENGTH) { return longestPrefixMatch; } boolean currentBit = Ip.getBitAtPosition(bits, index); Prefix longerMatch = null; if (currentBit) { if (_right != null) { longerMatch = _right.getLongestPrefixMatch(address, bits, index + 1); } } else { if (_left != null) { longerMatch = _left.getLongestPrefixMatch(address, bits, index + 1); } } if (longerMatch == null) { return longestPrefixMatch; } else { return longerMatch; } } }
boolean currentBit = Ip.getBitAtPosition(bits, index);
public void addPrefix(Prefix prefix, long bits, int prefixLength, int depth) { if (prefixLength == depth) { _prefix = prefix; return; } else { boolean currentBit = Ip.getBitAtPosition(bits, depth); if (currentBit) { if (_right == null) { _right = new ByteTrieNode(); } _right.addPrefix(prefix, bits, prefixLength, depth + 1); } else { if (_left == null) { _left = new ByteTrieNode(); } _left.addPrefix(prefix, bits, prefixLength, depth + 1); } } }
@Nullable private RibTreeNode<R> findRouteNode(long bits, int prefixLength, int firstUnmatchedBitIndex) { // If prefix lengths match, this is the node where such route would be stored. if (prefixLength == _prefix.getPrefixLength()) { return this; } boolean currentBit = Ip.getBitAtPosition(bits, firstUnmatchedBitIndex); /* * If prefixes don't match exactly, look at the current bit. That determines whether we look * left or right. As long as the child is not null, recurse. * * Note that: * 1) routes are stored in the nodes where lengths of the node prefix and the route prefix * match exactly; and * 2) prefix matches only get more specific (longer) the deeper we go in the tree * * Therefore, we can fast-forward the firstUnmatchedBitIndex to the prefix length of the * child node */ if (currentBit) { return (_right != null) ? _right.findRouteNode(bits, prefixLength, _right._prefix.getPrefixLength()) : null; } else { return (_left != null) ? _left.findRouteNode(bits, prefixLength, _left._prefix.getPrefixLength()) : null; } }
public BDD toBDD(IpWildcard ipWildcard) { long ip = ipWildcard.getIp().asLong(); long wildcard = ipWildcard.getWildcard().asLong(); BDD acc = _factory.one(); BDD[] bitBDDs = _bddInteger.getBitvec(); for (int i = 0; i < Prefix.MAX_PREFIX_LENGTH; i++) { boolean significant = !Ip.getBitAtPosition(wildcard, i); if (significant) { boolean bitValue = Ip.getBitAtPosition(ip, i); if (bitValue) { acc = acc.and(bitBDDs[i]); } else { acc = acc.and(bitBDDs[i].not()); } } } return acc; }
public static BDD firstBitsEqual(BDD[] bits, Prefix p, int length) { long b = p.getStartIp().asLong(); BDD acc = factory.one(); for (int i = 0; i < length; i++) { boolean res = Ip.getBitAtPosition(b, i); if (res) { acc = acc.and(bits[i]); } else { acc = acc.and(bits[i].not()); } } return acc; }
private BDD firstBitsEqual(Ip ip, int length) { long b = ip.asLong(); BDD acc = _factory.one(); BDD[] bitBDDs = _bddInteger.getBitvec(); for (int i = 0; i < length; i++) { boolean bitValue = Ip.getBitAtPosition(b, i); if (bitValue) { acc = acc.and(bitBDDs[i]); } else { acc = acc.and(bitBDDs[i].not()); } } return acc; }
boolean currentBit = Ip.getBitAtPosition(routeBits, firstUnmatchedBitIndex); return mergeHelper( this, route, routeBits, routePrefixLength, firstUnmatchedBitIndex, currentBit);
public BDD restrict(BDD bdd, Prefix pfx) { int len = pfx.getPrefixLength(); long bits = pfx.getStartIp().asLong(); int[] vars = new int[len]; BDD[] vals = new BDD[len]; _pairing.reset(); for (int i = 0; i < len; i++) { int var = _dstIp.getBitvec()[i].var(); // dstIpIndex + i; BDD subst = Ip.getBitAtPosition(bits, i) ? _factory.one() : _factory.zero(); vars[i] = var; vals[i] = subst; } _pairing.set(vars, vals); return bdd.veccompose(_pairing); }
nextUnmatchedBit < nodePrefixLength && nextUnmatchedBit < prefixLength; nextUnmatchedBit++) { currentAddressBit = Ip.getBitAtPosition(routeBits, nextUnmatchedBit); currentNodeAddressBit = Ip.getBitAtPosition(nodeAddressBits, nextUnmatchedBit); if (currentNodeAddressBit != currentAddressBit) { break; currentNodeAddressBit = Ip.getBitAtPosition(nodeAddressBits, nextUnmatchedBit); RibTreeNode<R> oldNode = node; node = new RibTreeNode<>(route.getNetwork(), _owner);
for (int i = 0; i < len; i++) { int var = _prefix.getBitvec()[i].var(); // prefixIndex + i; BDD subst = Ip.getBitAtPosition(bits, i) ? factory.one() : factory.zero(); vars[i] = var; vals[i] = subst;