/// <summary> /// Attempts to find the country code corresponding to /// a given IP address. /// </summary> /// <param name="address">A <see cref="String"/> value /// representing the </param> /// <returns>The two letter country code corresponding to /// the IP address, or <strong>"??"</strong> if it was not /// found.</returns> public String GetCountry(String address) { String [] parts = address.Split('.'); // The first IndexLength bits form the key into the // array of root nodes. Int32 indexBase = ((Int32.Parse(parts[0]) << 8) + Int32.Parse(parts[1])); Int32 index = indexBase >> (_indexOffset - 16); BinaryTrieNode root = base.Roots[index]; // If we don't have a root, we don't have a value. if (null == root) { return(null); } // Calculate the full key... Int32 key = (indexBase << 16) + (Int32.Parse(parts[2]) << 8) + Int32.Parse(parts[3]); // ...and look it up. return((String)root.FindBestMatch(key).UserData); }
/// <summary> /// Looks up a key value in the trie. /// </summary> /// <param name="key">The key to look up.</param> /// <returns>The best matching <see cref="BinaryTrieNode"/> /// in the trie.</returns> public BinaryTrieNode FindBestMatch(Int32 key) { // Pick the child to investigate. if ((key & _bit[_keyLength + 1]) == 0) { // If the key matches the child's key, pass on the request. if (null != _zero) { if ((key ^ _zero._key) < _bit[_zero._keyLength]) { return(_zero.FindBestMatch(key)); } } } else { // If the key matches the child's key, pass on the request. if (null != _one) { if ((key ^ _one._key) < _bit[_one._keyLength]) { return(_one.FindBestMatch(key)); } } } // If we got here, neither child was a match, so the current // node is the best match. return(this); }
protected Object FindBestMatchInternal(Int32 index, Int32 key) { BinaryTrieNode root = _roots[index]; if (null == root) { return(null); } return(root.FindBestMatch(key).UserData); }