FilterList
Introduction
This script provides an API to search objects using filters.
Example
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Wonderland;
public class FilterListTest : MonoBehaviour {
class Location : System.IEquatable<Location> {
public string City = "Nantes";
public string Region = "Loire-Atlantique";
public string Country = "France";
public bool Equals(Location loc) {
return Country == loc.Country && Region == loc.Region && City == loc.City;
}
}
void Start() {
// my items
int sword = 1;
int armor = 2;
int helmet = 3;
int shield = 4;
int potion = 5;
///////////////
// simple filterlist myItemId + user name
var myList = new FilterList<int, string>();
myList.Set("Antoine", sword, armor, potion);
myList.Set("Thomas", sword, armor, helmet);
myList.Set("Thibault", sword, helmet, shield, potion);
// tests (optimized get)
Log("myList.Get(sword)", myList.Get(sword)); // {Antoine, Thomas, Thibault}
Log("myList.Get(shield)", myList.Get(shield)); // {Thibault}
Log("myList.Get(armor, helmet)", myList.Get(armor, helmet)); // {Antoine, Thomas, Thibault}
///////////////
// Using IEquatable
var myList2 = new FilterList<Location, string>();
// my country database
var city1 = new Location() {
City = "Nantes",
Region = "Loire-Atlantique",
Country = "France"
};
var city2 = new Location() {
City = "Nice",
Region = "Alpes-Maritimes",
Country = "France"
};
myList2.Set("Antoine", city1);
myList2.Set("Thomas", city1);
myList2.Set("Thibault", city2);
Log("myList2.Get(city1)", myList2.Get(city1)); // {Antoine, Thomas}
Log("myList2.Get(city2)", myList2.Get(city2)); // {Thibault}
// create a new location with same values of the first
var city3 = new Location() {
City = "Nantes",
Region = "Loire-Atlantique",
Country = "France"
};
myList2.Set("Pierre", city3);
// Use match to select with equals
Log("myList2.Get(city1)", myList2.Get(city1)); // {Antoine, Thomas}
Log("myList2.Match(city1)", myList2.Match(city1)); // {Antoine, Thomas, Pierre}
}
void Log(string title, IEnumerable<string> names) {
print(title + " : {" + names.Aggregate((current, next) => current+", "+next) + "}");
}
}
myList.Get(sword) : {Antoine, Thomas, Thibault}
myList.Get(shield) : {Thibault}
myList.Get(armor, helmet) : {Antoine, Thomas, Thibault}
myList2.Get(city1) : {Antoine, Thomas}
myList2.Get(city2) : {Thibault}
myList2.Get(city1) : {Antoine, Thomas}
myList2.Match(city1) : {Antoine, Thomas, Pierre}