protected OrderedSet(IComparer <TKey> comparer, IEqualityComparer <TKey> equalityComparer, SortedSet <KeyValuePair <TKey, TValue> > set) { this.comparer = comparer; this.equalityComparer = equalityComparer; kvComparer = new KeyValuePairComparer <TKey, TValue>(comparer); this.set = set; }
public void CompareGreater() { var comparer = new KeyValuePairComparer <int, string>(); var a = new KeyValuePair <int, string>(2, null); var b = new KeyValuePair <int, string>(1, "asdf"); Assert.AreEqual(1, comparer.Compare(a, b)); }
public static void Main() { KeyValuePair <int, int> pair = new KeyValuePair <int, int> (3, 89); KeyValuePairComparer <int, int> comparer = new KeyValuePairComparer <int, int> (); TreeBag <KeyValuePair <int, int> > bag = new TreeBag <KeyValuePair <int, int> > (comparer, pair); bag.Find(); }
protected OrderedSet(IComparer <TKey> comparer, IEqualityComparer <TKey> equalityComparer, List <KeyValuePair <TKey, TValue> > list) { this.comparer = comparer; this.equalityComparer = equalityComparer; kvComparer = new KeyValuePairComparer <TKey, TValue>(comparer); this.list = list; }
public void CompareNullBothKeys() { var comparer = new KeyValuePairComparer <string, string>(); var a = new KeyValuePair <string, string>(null, null); var b = new KeyValuePair <string, string>(null, "asdf"); Assert.AreEqual(0, comparer.Compare(a, b)); }
public void CompareNullFirstKey() { // All instances are greater than null (MSDN) var comparer = new KeyValuePairComparer <string, string>(); var a = new KeyValuePair <string, string>(null, null); var b = new KeyValuePair <string, string>("a", "asdf"); Assert.AreEqual(-1, comparer.Compare(a, b)); }
public void CompareUsingCustomKeyComparer() { // Test that the custom comparer for the keys is used if supplied var keyComparer = new ReverseIntComparer(); var comparer = new KeyValuePairComparer <int, string>(keyComparer); var a = new KeyValuePair <int, string>(2, null); var b = new KeyValuePair <int, string>(1, "asdf"); Assert.AreEqual(-1, comparer.Compare(a, b)); }
/// <summary> /// Initializes a new instance of the <see cref="OrderedListDictionary{TK,TV}"/> class. /// </summary> /// <param name="keyComparer">The key comparer.</param> public OrderedListDictionary(IComparer <TK> keyComparer) { _itemList = new List <KeyValuePair <TK, TV> >(); _itemComparer = new KeyValuePairComparer(keyComparer, false); var range = new BoundRange <TK>(null, null, keyComparer); _keys = new OrderedListDictionaryKeys <TK, TV>(this, range); _values = new OrderedListDictionaryValues <TK, TV>(this, range); }
/// <summary> /// Initializes a new instance of the <see cref="OrderedListDictionary{TK,TV}" /> class. /// </summary> /// <param name="itemList">The item list.</param> /// <param name="comparer">The comparer.</param> internal OrderedListDictionary( List <KeyValuePair <TK, TV> > itemList, KeyValuePairComparer comparer) { _itemList = itemList; _itemComparer = comparer; var range = new BoundRange <TK>(null, null, comparer.KeyComparer); _keys = new OrderedListDictionaryKeys <TK, TV>(this, range); _values = new OrderedListDictionaryValues <TK, TV>(this, range); }
public SortedDictionary(IDictionary <TKey, TValue> dictionary, IComparer <TKey>?comparer) { ArgumentNullException.ThrowIfNull(dictionary); var keyValuePairComparer = new KeyValuePairComparer(comparer); if (dictionary is SortedDictionary <TKey, TValue> sortedDictionary && sortedDictionary._set.Comparer is KeyValuePairComparer kv && kv.keyComparer.Equals(keyValuePairComparer.keyComparer)) { _set = new TreeSet <KeyValuePair <TKey, TValue> >(sortedDictionary._set, keyValuePairComparer); }
/// <summary> /// Initializes a new instance of the <see cref="OrderedListDictionary{TK,TV}"/> class. /// </summary> public OrderedListDictionary() { var comparer = Comparers.Default <TK>(); //var comparer = Comparer<TK>.Default; _itemList = new List <KeyValuePair <TK, TV> >(); _itemComparer = new KeyValuePairComparer(comparer, false); var range = new BoundRange <TK>(null, null, comparer); _keys = new OrderedListDictionaryKeys <TK, TV>(this, range); _values = new OrderedListDictionaryValues <TK, TV>(this, range); }
public static bool EqualsDictionary <TKey, TValue>(this IReadOnlyDictionary <TKey, TValue> dictionary, IReadOnlyDictionary <TKey, TValue> other, IEqualityComparer <TKey> keyComparer, IEqualityComparer <TValue> valueComparer) where TKey : notnull { if (other == null) { return(false); } if (dictionary.Count != other.Count) { return(false); } var comparer = new KeyValuePairComparer <TKey, TValue>(keyComparer, valueComparer); return(!dictionary.Except(other, comparer).Any()); }
public void CustomComparer() { var associationKeyComparer = new KeyValuePairComparer <int, string>(Comparer <int> .Default); var pair1 = new KeyValuePair <int, string>(5, "5"); var pair2 = new KeyValuePair <int, string>(5, "6"); var pair3 = new KeyValuePair <int, string>(3, "5"); var pair4 = new KeyValuePair <int, string>(5, "5"); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair2), 0); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair3), 1); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair4), 0); Assert.AreEqual(associationKeyComparer.Compare(pair2, pair1), 0); Assert.AreEqual(associationKeyComparer.Compare(pair3, pair1), -1); Assert.AreEqual(associationKeyComparer.Compare(pair4, pair1), 0); }
public void DefaultComparer() { var associationKeyComparer = new KeyValuePairComparer<int, string>(); var pair1 = new KeyValuePair<int, string>(5, "5"); var pair2 = new KeyValuePair<int, string>(5, "6"); var pair3 = new KeyValuePair<int, string>(3, "5"); var pair4 = new KeyValuePair<int, string>(5, "5"); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair2), 0); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair3), 1); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair4), 0); Assert.AreEqual(associationKeyComparer.Compare(pair2, pair1), 0); Assert.AreEqual(associationKeyComparer.Compare(pair3, pair1), -1); Assert.AreEqual(associationKeyComparer.Compare(pair4, pair1), 0); }
public void CustomComparison() { var associationKeyComparer = new KeyValuePairComparer <int, string>((x, y) => x.CompareTo(y) * -1); var pair1 = new KeyValuePair <int, string>(5, "5"); var pair2 = new KeyValuePair <int, string>(5, "6"); var pair3 = new KeyValuePair <int, string>(3, "5"); var pair4 = new KeyValuePair <int, string>(5, "5"); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair2), 0); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair3), -1); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair4), 0); Assert.AreEqual(associationKeyComparer.Compare(pair2, pair1), 0); Assert.AreEqual(associationKeyComparer.Compare(pair3, pair1), +1); Assert.AreEqual(associationKeyComparer.Compare(pair4, pair1), 0); Assert.AreEqual(associationKeyComparer.Compare(pair2.Key, pair1.Key), 0); Assert.AreEqual(associationKeyComparer.Compare(pair3.Key, pair1.Key), +1); Assert.AreEqual(associationKeyComparer.Compare(pair4.Key, pair1.Key), 0); }
public void CustomComparison() { var associationKeyComparer = new KeyValuePairComparer<int, string>((x, y) => x.CompareTo(y) * -1); var pair1 = new KeyValuePair<int, string>(5, "5"); var pair2 = new KeyValuePair<int, string>(5, "6"); var pair3 = new KeyValuePair<int, string>(3, "5"); var pair4 = new KeyValuePair<int, string>(5, "5"); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair2), 0); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair3), -1); Assert.AreEqual(associationKeyComparer.Compare(pair1, pair4), 0); Assert.AreEqual(associationKeyComparer.Compare(pair2, pair1), 0); Assert.AreEqual(associationKeyComparer.Compare(pair3, pair1), +1); Assert.AreEqual(associationKeyComparer.Compare(pair4, pair1), 0); Assert.AreEqual(associationKeyComparer.Compare(pair2.Key, pair1.Key), 0); Assert.AreEqual(associationKeyComparer.Compare(pair3.Key, pair1.Key), +1); Assert.AreEqual(associationKeyComparer.Compare(pair4.Key, pair1.Key), 0); }
/// <summary> /// Initializes a new instance of the <see cref="OrderedDictionary{TK,TV}" /> class. /// </summary> /// <param name="itemList">The item list.</param> /// <param name="comparer">The comparer.</param> internal OrderedDictionary(List <KeyValuePair <TK, TV> > itemList, KeyValuePairComparer comparer) { _itemList = itemList; _itemComparer = comparer; }
/// <summary> /// Initializes a new instance of the <see cref="OrderedDictionary{TK,TV}"/> class. /// </summary> public OrderedDictionary() { _itemList = new List <KeyValuePair <TK, TV> >(); _itemComparer = new KeyValuePairComparer(new DefaultComparer(), false); }
internal OrderedListMultiDictionary( List <KeyValuePair <TK, ICollection <TV> > > itemList, KeyValuePairComparer comparer) : base(itemList, comparer) { }
/// <summary> /// Initializes a new instance of the <see cref="OrderedDictionary{TK,TV}"/> class. /// </summary> /// <param name="keyComparer">The key comparer.</param> public OrderedDictionary(IComparer <TK> keyComparer) { _itemList = new List <KeyValuePair <TK, TV> >(); _itemComparer = new KeyValuePairComparer(keyComparer, false); }
private IEnumerable <Url> GetSortedUrls(TabInfo tab, int portalId, Lazy <Dictionary <string, Locale> > locales, int sortColumn, bool sortOrder, bool isSystem) { var friendlyUrlSettings = new FriendlyUrlSettings(tab.PortalID); var tabs = new List <Url>(); if (isSystem) { //Add generated urls foreach (var alias in PortalAliasController.Instance.GetPortalAliasesByPortalId(portalId)) { var urlLocale = locales.Value.Values.FirstOrDefault(local => local.Code == alias.CultureCode); /*var isRedirected = tab.TabUrls.Any(u => u.HttpStatus == "200" * && u.CultureCode == ((urlLocale != null) ? urlLocale.Code : String.Empty)) || alias.PortalAliasID != PrimaryAliasId;*/ bool isRedirected = false; var isCustom200Urls = tab.TabUrls.Any(u => u.HttpStatus == "200");//are there any custom Urls for this tab? var baseUrl = Globals.AddHTTP(alias.HTTPAlias) + "/Default.aspx?TabId=" + tab.TabID; if (urlLocale != null) { baseUrl += "&language=" + urlLocale.Code; } string customPath = null; if (isCustom200Urls) { //get the friendlyUrl, including custom Urls customPath = AdvancedFriendlyUrlProvider.ImprovedFriendlyUrl(tab, baseUrl, Globals.glbDefaultPage, alias.HTTPAlias, false, friendlyUrlSettings, Guid.Empty); customPath = customPath.Replace(Globals.AddHTTP(alias.HTTPAlias), ""); } //get the friendlyUrl and ignore and custom Urls var path = AdvancedFriendlyUrlProvider.ImprovedFriendlyUrl(tab, baseUrl, Globals.glbDefaultPage, alias.HTTPAlias, true, friendlyUrlSettings, Guid.Empty); path = path.Replace(Globals.AddHTTP(alias.HTTPAlias), ""); int status = 200; if (customPath != null && (string.Compare(customPath, path, StringComparison.InvariantCultureIgnoreCase) != 0)) { //difference in custom/standard URL, so standard is 301 status = 301; isRedirected = true; } //AddUrlToList(tabs, -1, alias, urlLocale, path, String.Empty, (isRedirected) ? 301 : 200); //27139 : only show primary aliases in the tab grid (gets too confusing otherwise) if (alias.IsPrimary) //alias was provided to FriendlyUrlCall, so will always get the correct canonical Url back { AddUrlToList(tabs, portalId, -1, alias, urlLocale, path, String.Empty, status, isSystem, friendlyUrlSettings, null); } //Add url with diacritics isRedirected = friendlyUrlSettings.RedirectUnfriendly; bool replacedDiacritic; string asciiTabPath = TabPathHelper.ReplaceDiacritics(tab.TabPath, out replacedDiacritic).Replace("//", "/"); if (replacedDiacritic) { if (friendlyUrlSettings.AutoAsciiConvert) { if (friendlyUrlSettings.ReplaceSpaceWith != FriendlyUrlSettings.ReplaceSpaceWithNothing) { path = path.Replace(friendlyUrlSettings.ReplaceSpaceWith, String.Empty); } path = path.Replace(asciiTabPath, tab.TabPath.Replace("//", "/")); AddUrlToList(tabs, portalId, -1, alias, urlLocale, path, String.Empty, (isRedirected) ? 301 : 200, isSystem, friendlyUrlSettings, null); } } else { //Add url with space if (tab.TabName.Contains(" ") && friendlyUrlSettings.ReplaceSpaceWith != FriendlyUrlSettings.ReplaceSpaceWithNothing) { path = path.Replace(friendlyUrlSettings.ReplaceSpaceWith, String.Empty); if (customPath != null && string.Compare(customPath, path, StringComparison.InvariantCultureIgnoreCase) != 0) { AddUrlToList(tabs, portalId, -1, alias, urlLocale, path, String.Empty, (isRedirected) ? 301 : 200, isSystem, friendlyUrlSettings, null); } } } } } foreach (var url in tab.TabUrls.Where(u => u.IsSystem == isSystem).OrderBy(u => u.SeqNum)) { int statusCode; int.TryParse(url.HttpStatus, out statusCode); //27133 : Only show a custom URL if (url.PortalAliasUsage == PortalAliasUsageType.Default) { var aliases = PortalAliasController.Instance.GetPortalAliasesByPortalId(portalId); var alias = aliases.FirstOrDefault(primary => primary.IsPrimary == true); if (alias == null) { //if no primary alias just get first in list, need to use something alias = aliases.FirstOrDefault(a => a.PortalID == portalId); } if (alias != null) { var urlLocale = locales.Value.Values.FirstOrDefault(local => local.Code == alias.CultureCode); AddUrlToList(tabs, portalId, url.SeqNum, alias, urlLocale, url.Url, url.QueryString, statusCode, isSystem, friendlyUrlSettings, url.LastModifiedByUserId); } } else { var urlLocale = locales.Value.Values.FirstOrDefault(local => local.Code == url.CultureCode); var alias = PortalAliasController.Instance.GetPortalAliasesByPortalId(portalId) .SingleOrDefault(p => p.PortalAliasID == url.PortalAliasId); AddUrlToList(tabs, portalId, url.SeqNum, alias, urlLocale, url.Url, url.QueryString, statusCode, isSystem, friendlyUrlSettings, url.LastModifiedByUserId); } } var pairComparer = new KeyValuePairComparer(); switch ((SortingFields)sortColumn) { case SortingFields.Url: case SortingFields.None: return(sortOrder ? tabs.OrderBy(url => url.SiteAlias, pairComparer).ThenBy(url => url.Path) : tabs.OrderByDescending(url => url.SiteAlias, pairComparer).ThenByDescending(url => url.Path)); case SortingFields.Locale: return(sortOrder ? tabs.OrderBy(url => url.Locale, pairComparer) : tabs.OrderByDescending(url => url.Locale, pairComparer)); case SortingFields.Status: return(sortOrder ? tabs.OrderBy(url => url.StatusCode, pairComparer) : tabs.OrderByDescending(url => url.StatusCode, pairComparer)); default: return(sortOrder ? tabs.OrderBy(url => url.SiteAlias, pairComparer).ThenBy(url => url.Path) : tabs.OrderByDescending(url => url.SiteAlias, pairComparer).ThenByDescending(url => url.Path)); } }
public static bool EqualsDictionary <TKey, TValue>(this IReadOnlyDictionary <TKey, TValue> dictionary, IReadOnlyDictionary <TKey, TValue> other, IEqualityComparer <TKey> keyComparer, IEqualityComparer <TValue> valueComparer) where TKey : notnull { var comparer = new KeyValuePairComparer <TKey, TValue>(keyComparer, valueComparer); return(other != null && dictionary.Count == other.Count && !dictionary.Except(other, comparer).Any()); }