यदि आपकी दो सूचियों को क्रमबद्ध किया गया है, तो आप आसानी से उनके माध्यम से चल सकते हैं। यह एक ओ (एम + एन) ऑपरेशन है। निम्नलिखित कोड मदद कर सकता है:
class Program
{
static void Main()
{
List<string> left = new List<string> { "Alice", "Charles", "Derek" };
List<string> right = new List<string> { "Bob", "Charles", "Ernie" };
EnumerableExtensions.CompareSortedCollections(left, right, StringComparer.CurrentCultureIgnoreCase,
s => Console.WriteLine("Left: " + s), s => Console.WriteLine("Right: " + s), (x,y) => Console.WriteLine("Both: " + x + y));
}
}
static class EnumerableExtensions
{
public static void CompareSortedCollections<T>(IEnumerable<T> source, IEnumerable<T> destination, IComparer<T> comparer, Action<T> onLeftOnly, Action<T> onRightOnly, Action<T, T> onBoth)
{
EnumerableIterator<T> sourceIterator = new EnumerableIterator<T>(source);
EnumerableIterator<T> destinationIterator = new EnumerableIterator<T>(destination);
while (sourceIterator.HasCurrent && destinationIterator.HasCurrent)
{
// While LHS < RHS, the items in LHS aren't in RHS
while (sourceIterator.HasCurrent && (comparer.Compare(sourceIterator.Current, destinationIterator.Current) < 0))
{
onLeftOnly(sourceIterator.Current);
sourceIterator.MoveNext();
}
// While RHS < LHS, the items in RHS aren't in LHS
while (sourceIterator.HasCurrent && destinationIterator.HasCurrent && (comparer.Compare(sourceIterator.Current, destinationIterator.Current) > 0))
{
onRightOnly(destinationIterator.Current);
destinationIterator.MoveNext();
}
// While LHS==RHS, the items are in both
while (sourceIterator.HasCurrent && destinationIterator.HasCurrent && (comparer.Compare(sourceIterator.Current, destinationIterator.Current) == 0))
{
onBoth(sourceIterator.Current, destinationIterator.Current);
sourceIterator.MoveNext();
destinationIterator.MoveNext();
}
}
// Mop up.
while (sourceIterator.HasCurrent)
{
onLeftOnly(sourceIterator.Current);
sourceIterator.MoveNext();
}
while (destinationIterator.HasCurrent)
{
onRightOnly(destinationIterator.Current);
destinationIterator.MoveNext();
}
}
}
internal class EnumerableIterator<T>
{
private readonly IEnumerator<T> _enumerator;
public EnumerableIterator(IEnumerable<T> enumerable)
{
_enumerator = enumerable.GetEnumerator();
MoveNext();
}
public bool HasCurrent { get; private set; }
public T Current
{
get { return _enumerator.Current; }
}
public void MoveNext()
{
HasCurrent = _enumerator.MoveNext();
}
}
आप हालांकि, उन पर पुनरावृत्ति जबकि संग्रह संशोधित करने के बारे में सावधान रहना होगा।
यदि वे क्रमबद्ध नहीं होते हैं, तो प्रत्येक तत्व को प्रत्येक तत्व में एक दूसरे के साथ तुलना करना ओ (एमएन) है, जो वास्तव में दर्दनाक हो जाता है।
यदि आप प्रत्येक संग्रह से मुख्य मूल्यों को एक शब्दकोश या इसी तरह की प्रतिलिपि बना सकते हैं (यानी।"एक्स एक्स मौजूद है" पूछे जाने पर स्वीकार्य प्रदर्शन के साथ एक संग्रह?), तो आप कुछ उचित के साथ आ सकते हैं।
स्रोत
2008-10-02 09:43:55
क्या कोई अच्छा कारण है कि आप केवल संदर्भ सूची की प्रतिलिपि नहीं बना सकते? एल 2 = एल 1; आपके द्वारा दिए गए मानदंडों के आधार पर आपको जो चाहिए उसे लगता है। –
हां, एल 2 सूची एक बाध्यकारी सूची है जो ग्रिड के लिए डेटासोर्स के रूप में उपयोग की जाती है। एल 1 और एल 2 ऑब्जेक्ट्स एक ही प्रकार के नहीं हैं। एल 2 में प्रेजेंटेशन ऑब्जेक्ट्स शामिल हैं जिन्हें प्रदर्शन और ग्रिड ब्लिंकिंग व्यवहार के बारे में अपडेट किया जाना चाहिए। –