11/*
22Technitium Bit Chat
3- Copyright (C) 2015 Shreyas Zare (shreyas@technitium.com)
3+ Copyright (C) 2017 Shreyas Zare (shreyas@technitium.com)
44
55This program is free software: you can redistribute it and/or modify
66it under the terms of the GNU General Public License as published by
@@ -24,7 +24,7 @@ You should have received a copy of the GNU General Public License
2424
2525namespace BitChatCore . Network
2626{
27- public class BinaryID : IWriteStream , IEquatable < BinaryID >
27+ public class BinaryID : IWriteStream , IEquatable < BinaryID > , IComparable < BinaryID >
2828 {
2929 #region variables
3030
@@ -132,6 +132,23 @@ public override int GetHashCode()
132132 return BitConverter . ToInt32 ( _id , 0 ) ;
133133 }
134134
135+ public int CompareTo ( BinaryID other )
136+ {
137+ if ( this . _id . Length != other . _id . Length )
138+ throw new ArgumentException ( "Operand id length not equal." ) ;
139+
140+ for ( int i = 0 ; i < this . _id . Length ; i ++ )
141+ {
142+ if ( this . _id [ i ] > other . _id [ i ] )
143+ return 1 ;
144+
145+ if ( this . _id [ i ] < other . _id [ i ] )
146+ return - 1 ;
147+ }
148+
149+ return 0 ;
150+ }
151+
135152 public override string ToString ( )
136153 {
137154 return BitConverter . ToString ( _id ) . Replace ( "-" , "" ) . ToLower ( ) ;
@@ -257,16 +274,60 @@ public void WriteTo(Stream s)
257274 if ( b1 . _id . Length != b2 . _id . Length )
258275 throw new ArgumentException ( "Operand id length not equal." ) ;
259276
277+ bool eq = true ;
278+
260279 for ( int i = 0 ; i < b1 . _id . Length ; i ++ )
261280 {
262281 if ( b1 . _id [ i ] > b2 . _id [ i ] )
263282 return false ;
283+
284+ if ( b1 . _id [ i ] != b2 . _id [ i ] )
285+ eq = false ;
264286 }
265287
288+ if ( eq )
289+ return false ;
290+
266291 return true ;
267292 }
268293
269294 public static bool operator > ( BinaryID b1 , BinaryID b2 )
295+ {
296+ if ( b1 . _id . Length != b2 . _id . Length )
297+ throw new ArgumentException ( "Operand id length not equal." ) ;
298+
299+ bool eq = true ;
300+
301+ for ( int i = 0 ; i < b1 . _id . Length ; i ++ )
302+ {
303+ if ( b1 . _id [ i ] < b2 . _id [ i ] )
304+ return false ;
305+
306+ if ( b1 . _id [ i ] != b2 . _id [ i ] )
307+ eq = false ;
308+ }
309+
310+ if ( eq )
311+ return false ;
312+
313+ return true ;
314+ }
315+
316+ public static bool operator <= ( BinaryID b1 , BinaryID b2 )
317+ {
318+ if ( b1 . _id . Length != b2 . _id . Length )
319+ throw new ArgumentException ( "Operand id length not equal." ) ;
320+
321+ for ( int i = 0 ; i < b1 . _id . Length ; i ++ )
322+ {
323+ if ( b1 . _id [ i ] > b2 . _id [ i ] )
324+ return false ;
325+ }
326+
327+ return true ;
328+ }
329+
330+ public static bool operator >= ( BinaryID b1 , BinaryID b2 )
270331 {
271332 if ( b1 . _id . Length != b2 . _id . Length )
272333 throw new ArgumentException ( "Operand id length not equal." ) ;
0 commit comments