Created
March 17, 2016 12:20
-
-
Save jesuslpm/3e638fc33370f2f74f41 to your computer and use it in GitHub Desktop.
Ayende csv user data searcher 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class UserData | |
| { | |
| public int Id { get; set; } | |
| public string Email { get; set; } | |
| } | |
| public class UserDataSearcher | |
| { | |
| private readonly List<UserData> userDataList; | |
| private class UserDataComparer : IComparer<UserData> | |
| { | |
| public int Compare(UserData x, UserData y) | |
| { | |
| return StringComparer.InvariantCultureIgnoreCase.Compare(x.Email, y.Email); | |
| } | |
| } | |
| private static readonly UserDataComparer comparer = new UserDataComparer(); | |
| public UserDataSearcher(IEnumerable<UserData> usersData) | |
| { | |
| userDataList = usersData.ToList(); | |
| userDataList.Sort(comparer); | |
| } | |
| public IEnumerable<UserData> GetUsersByEmail(string email) | |
| { | |
| int pos = this.userDataList.BinarySearch(new UserData { Email = email }, comparer); | |
| if (pos < 0) yield break; | |
| yield return userDataList[pos]; | |
| for (int i = pos + 1; i < userDataList.Count; i++) | |
| { | |
| var userData = userDataList[i]; | |
| if (string.Equals(userData.Email, email, StringComparison.InvariantCultureIgnoreCase)) | |
| { | |
| yield return userData; | |
| } | |
| else break; | |
| } | |
| for (int i = pos - 1; i >= 0; i--) | |
| { | |
| var userData = userDataList[i]; | |
| if (string.Equals(userData.Email, email, StringComparison.InvariantCultureIgnoreCase)) | |
| { | |
| yield return userData; | |
| } | |
| else break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment