/** Utility method to determine if this address is between start and end * from the right, i.e. its satisfies the following constraints: * 1. Is to the right of start, and * 2. Is to the left of end * @return 1 in case its within * @return -1 in case it is not */ public bool IsBetweenFromRight(AHAddress start, AHAddress end){ int se_comp = start.CompareTo(end); //simple case of no wrap around where "within" is lesser if (se_comp > 0) { return start.CompareTo(this) > 0 && this.CompareTo(end) > 0; } else if( se_comp == 0 ) { //When start == end, nothing is between them return false; } else { //in case there is a wrap around //"within" has become greater than "this" return start.CompareTo(this) > 0 || this.CompareTo(end) > 0; } }
public void Test() { byte[] buf1 = new byte[20]; for (int i = 0; i <= 18; i++) { buf1[i] = 0x00; } buf1[19] = 0x0A; AHAddress test_address_1 = new AHAddress( MemBlock.Reference(buf1, 0, buf1.Length) ); byte[] buf2 = new byte[20]; for (int i = 0; i <= 18; i++) { buf2[i] = 0xFF; } buf2[19] = 0xFE; AHAddress test_address_2 = new AHAddress( MemBlock.Reference(buf2, 0, buf2.Length) ); //test_address_1 is to the left of test_address_2 //because it only a few steps in the clockwise direction: Assert.IsTrue( test_address_1.IsLeftOf( test_address_2 ), "IsLeftOf"); Assert.IsTrue( test_address_2.IsRightOf( test_address_1 ), "IsRightOf"); //This distance is twelve: Assert.AreEqual( test_address_2.DistanceTo( test_address_1), new BigInteger(12), "DistanceTo"); Assert.IsTrue( test_address_1.CompareTo(test_address_2) < 0, "CompareTo"); Assert.IsTrue( test_address_2.CompareTo(test_address_1) > 0, "CompareTo"); byte[] buf3 = new byte[Address.MemSize]; test_address_2.CopyTo(buf3); AHAddress test3 = new AHAddress(MemBlock.Reference(buf3,0,buf3.Length)); Assert.IsTrue( test3.CompareTo( test_address_2 ) == 0 , "CompareTo"); Assert.IsTrue( test3.CompareTo( test3 ) == 0, "CompareTo"); //As long as the address does not wrap around, adding should increase it: AHAddress a4 = new AHAddress( test_address_1.ToBigInteger() + 100 ); Assert.IsTrue( a4.CompareTo( test_address_1 ) > 0, "adding increases"); Assert.IsTrue( a4.CompareTo( test_address_2 ) < 0, "smaller than biggest"); //Here are some consistency tests: for( int i = 0; i < 100; i++) { System.Random r = new Random(); byte[] b1 = new byte[Address.MemSize]; r.NextBytes(b1); //Make sure it is class 0: Address.SetClass(b1, 0); byte[] b2 = new byte[Address.MemSize]; r.NextBytes(b2); //Make sure it is class 0: Address.SetClass(b2, 0); byte[] b3 = new byte[Address.MemSize]; r.NextBytes(b3); //Make sure it is class 0: Address.SetClass(b3, 0); AHAddress a5 = new AHAddress( MemBlock.Reference(b1,0,b1.Length) ); AHAddress a6 = new AHAddress( MemBlock.Reference(b2,0,b2.Length) ); AHAddress a7 = new AHAddress( MemBlock.Reference(b3,0,b3.Length) ); Assert.IsTrue( a5.CompareTo(a6) == -1 * a6.CompareTo(a5), "consistency"); //Nothing is between the same address: Assert.IsFalse( a5.IsBetweenFromLeft(a6, a6), "Empty Between Left"); Assert.IsFalse( a5.IsBetweenFromRight(a7, a7), "Empty Between Right"); //Endpoints are not between: Assert.IsFalse( a6.IsBetweenFromLeft(a6, a7), "End point Between Left"); Assert.IsFalse( a6.IsBetweenFromRight(a6, a7), "End point Between Right"); Assert.IsFalse( a7.IsBetweenFromLeft(a6, a7), "End point Between Left"); Assert.IsFalse( a7.IsBetweenFromRight(a6, a7), "End point Between Right"); if ( a5.IsBetweenFromLeft(a6, a7) ) { //Then the following must be true: Assert.IsTrue( a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7), "BetweenLeft true"); } else { //Then the following must be false: Assert.IsFalse( a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7), "BetweenLeft false"); } if ( a5.IsBetweenFromRight(a6, a7) ) { //Then the following must be true: Assert.IsTrue( a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7), "BetweenRight true"); } else { //Then the following must be false: Assert.IsFalse( a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7), "BetweenRight false"); } Assert.IsFalse(a5.IsBetweenFromLeft(a6, a7) == a5.IsBetweenFromRight(a6, a7), "can't be between left and between right"); } }
/** * @return the number of Structured Connections in the interval * (a1, a2) (not including the end points) when we move to the right. */ public int RightInclusiveCount(AHAddress a1, AHAddress a2) { if( a1.Equals(a2) ) { return 0; } int dist; int count = _con_list.Count; int a2_idx = _con_list.BinarySearch(a2); int a1_idx = _con_list.BinarySearch(a1); /* * There are four cases, we deal with each separately: * 0) neither a1 nor a2 are in the table * 1) a1 is not, but a2 is * 2) a1 is, but a2 is not * 3) a1 and a2 are. */ bool a2_present = true; bool a1_present = true; if( a2_idx < 0 ) { a2_present = false; a2_idx = ~a2_idx; } if( a1_idx < 0 ) { a1_present = false; a1_idx = ~a1_idx; } if( a1_idx == a2_idx ) { //This is an easy case: int max_dist = count; if( a2_present ) { max_dist--; } if( a1_present ) { max_dist--; } if( a2.CompareTo( a1 ) < 0 ) { dist = 0; } else { dist = max_dist; } } else { //These two indices are different: dist = a1_idx - a2_idx; if( dist < 0 ) { //Wrap around. dist += count; } if( a2_present ) { /* * In thie case, our calculations are too much by one, in both * cases (dist > 0, dist < 0), so we decrease by one: */ dist = dist - 1; } } return dist; }