public static void Main(string[] args) { // check that arguments have been supplied if (args.Length != 2) { Console.WriteLine("missing args"); System.Environment.Exit(1); } // attempt to open data file InFile data = new InFile(args[0]); if (data.OpenError()) { Console.WriteLine("cannot open " + args[0]); System.Environment.Exit(1); } // attempt to open results file OutFile results = new OutFile(args[1]); if (results.OpenError()) { Console.WriteLine("cannot open " + args[1]); System.Environment.Exit(1); } // various initializations int total = 0; IntSet mySet = new IntSet(); IntSet smallSet = new IntSet(1, 2, 3, 4, 5); string smallSetStr = smallSet.ToString(); // read and process data file int item = data.ReadInt(); while (!data.NoMoreData()) { total = total + item; if (item > 0) { mySet.Incl(item); } item = data.ReadInt(); } // write various results to output file results.Write("total = "); results.WriteLine(total, 5); results.WriteLine("unique positive numbers " + mySet.ToString()); results.WriteLine("union with " + smallSetStr + " = " + mySet.Union(smallSet).ToString()); results.WriteLine("intersection with " + smallSetStr + " = " + mySet.Intersection(smallSet).ToString()); /* or simply * results.WriteLine("union with " + smallSetStr + " = " + mySet.Union(smallSet)); * results.WriteLine("intersection with " + smallSetStr + " = " + mySet.Intersection(smallSet)); */ results.Close(); } // Main
//---------------------------------------------------------------------- protected static ResultSet SearchByProfile <ProfileType>( Timer t, Dictionary <string, string> consumerProperties, IntSet <ProfileId> profileSpecificProfiles) where ProfileType : ISearch, new() { ISearch profile = new ProfileType(); IntSet <ProfileId> moreOptionsMatches; if (!profile.ExpressionTree.TryEvaluate( consumerProperties, out moreOptionsMatches, profile.ProfileTemplateLabel)) { moreOptionsMatches = IntSet <ProfileId> .Universe; } return(ResultSet.From( IntSet <ProfileId> .Intersection( profileSpecificProfiles, moreOptionsMatches))); }
//---------------------------------------------------------------------- private static ResultSet SearchByParty <ProfileType>( Timer t, IEnumerable <string> nameSearchNoiseWords, AddressParserResult searchLocale, int desiredResultCount, int maxResultCount, IntSet <PartyId> externalPartyMembershipTypeMatches, IntSet <PartyId> profileSpecificParties, double nonGeoMatchAbsDensity, ref Comparison <Result> sortOrder) where ProfileType : ISearch, new() { IntSet <PartyId> nameMatches = null; IntSet <PartyId> locationMatches = null; IntSet <PartyId> geoOrNameMatches = null; Location centroid = null; double? maxRadiusMi = null; ResultSet rs = null; if (StringX.IsNotEmpty(searchLocale.PartyName)) { nameMatches = SearchByName(t, searchLocale, nameSearchNoiseWords); } if (searchLocale.Type == Snap.Data.Controller.SearchType.PartyName) { geoOrNameMatches = nameMatches; sortOrder = Result.NameAscending; } else { switch (new ProfileType().GeoSearch) { case GeoSearchType.Point: locationMatches = SearchForLocations( t, nameSearchNoiseWords, searchLocale, desiredResultCount, maxResultCount, nonGeoMatchAbsDensity, out centroid, out maxRadiusMi); break; case GeoSearchType.ServiceArea: locationMatches = SearchForServiceArea(t, searchLocale); break; case GeoSearchType.All: var pointMatches = SearchForLocations( t, nameSearchNoiseWords, searchLocale, desiredResultCount, maxResultCount, nonGeoMatchAbsDensity, out centroid, out maxRadiusMi); var svcAreaMatches = SearchForServiceArea(t, searchLocale); locationMatches = IntSet <PartyId> .Union(pointMatches, svcAreaMatches); sortOrder = Result.ShowServiceAreaMatchesFirst(svcAreaMatches, sortOrder); break; default: throw new ArgumentException(String.Format( "Unexpected value of 'ISearch.GeoSearch': {0}", new ProfileType().GeoSearch.ToString())); } geoOrNameMatches = StringX.IsNotEmpty(searchLocale.PartyName) ? IntSet <PartyId> .Intersection(nameMatches, locationMatches) : locationMatches; } rs = ResultSet.From( centroid, maxRadiusMi, IntSet <PartyId> .IntersectionOfMany( geoOrNameMatches, externalPartyMembershipTypeMatches, profileSpecificParties)); if (Snap.Data.Controller.SearchType.Membership == searchLocale.Type) { rs.UnfilteredMatchCount = externalPartyMembershipTypeMatches.Count; } else if (IntSet <PartyId> .IsEmpty(geoOrNameMatches)) { rs.UnfilteredMatchCount = 0; } else { rs.UnfilteredMatchCount = geoOrNameMatches.Count; } return(rs); }
//---------------------------------------------------------------------- private static ResultSet InnerSearch <ProfileType>( Timer t, IEnumerable <string> nameSearchNoiseWords, Dictionary <string, string> consumerProperties, Comparison <Result> sortOrder, AddressParserResult searchLocale, int desiredResultCount, int maxResultCount, List <int> externalPartyMembershipTypeIds, IntSet <PartyId> profileSpecificParties, IntSet <ProfileId> profileSpecificProfiles, MembershipSearch membershipSearch, PartySearch partySearch, ProfileSearch <ProfileType> profileSearch, string featuredProfileTemplateLabel) where ProfileType : ISearch, new() { if (null == profileSpecificProfiles) { profileSpecificProfiles = IntSet <ProfileId> .Universe; } if (null == profileSpecificParties) { profileSpecificParties = IntSet <PartyId> .Universe; } //Console.WriteLine(Snap.Cache.Cache.ProfileTemplate.Count); ISearch profileType = new ProfileType(); IntSet <PartyId> matchesByProfileType = null; try { if (String.IsNullOrEmpty(profileType.ProfileTemplateLabel)) { matchesByProfileType = IntSet <PartyId> .Universe; } else { matchesByProfileType = Snap.Cache.Cache.ProfileTemplate[profileType.ProfileTemplateLabel]; } } catch (KeyNotFoundException exc) { throw new ApplicationException( "Unknown ProfileTemplateLabel: " + profileType.ProfileTemplateLabel, exc); } IntSet <PartyId> matchesByMembership = IntSet <PartyId> .Universe; t.Measure("Search by membership(s)", delegate() { if (!ListX.IsEmpty(externalPartyMembershipTypeIds)) { matchesByMembership = membershipSearch(externalPartyMembershipTypeIds); } }); matchesByMembership = IntSet <PartyId> .Intersection(matchesByProfileType, matchesByMembership); ResultSet matchesByProfile = null; t.Measure("Search by Profile ('advanced' search)", delegate() { matchesByProfile = profileSearch( t, consumerProperties, profileSpecificProfiles); }); double nonGeoMatchAbsDensity = NonGeoMatchAbsDensity( matchesByMembership, matchesByProfile); ResultSet matchesByParty = ResultSet.Universe; t.Measure("Search by Party (geography,name)", delegate() { if (searchLocale == null) { matchesByParty = ResultSet.From(null, null, matchesByMembership); } else { matchesByParty = partySearch( t, nameSearchNoiseWords, searchLocale, desiredResultCount, maxResultCount, matchesByMembership, profileSpecificParties, nonGeoMatchAbsDensity, ref sortOrder); } }); ResultSet matches = null; t.Measure("Combine matches-by-party and matches-by-profile", delegate() { matches = ResultSet.Intersection(matchesByParty, matchesByProfile); }); return(FinalizeResultSet( t, sortOrder, desiredResultCount, matches, featuredProfileTemplateLabel)); }