2016-10-20 2 views
0

3 개의 정수 값을 가진 객체가 있습니다. 3 개의 정수는 항상 고유합니다. 수천 중에서 특정 개체를 빠르게 찾을 수있는 방법이 필요합니다.3 개의 정수 값으로 사전 키를 만듭니다.

내 생각은 문자열에 3 개의 정수를 결합하여 1, 2533과 9가 고유 한 문자열이됩니다 : 1-2533-9. 그러나 이것이 가장 효율적인 방법입니까? 숫자는 2^16보다 클 수 없습니다. 따라서 비트 이동을 사용하고 문자열을 만드는 것보다 더 빠를 것입니다. 다른 옵션이 있습니까? 나는 어떻게해야합니까?

내가 원하는 목표는 수천 개의 개체 모음을 사용하여 개체를 빠르게 찾는 것입니다.

+0

'long' 접근 방식은 의미 – slawekwin

+0

중요 정수의 순서입니다 만드는 것, 또는 1-2533-9 당신이 그들을 정렬 할 수 있습니다 2533년 1월 9일 – Misiakw

+0

과 같은 의미? 이렇게하면 피보나치를 사용할 수 있습니다. O (log n) – Nic

답변

0
public class SomeClass 
{ 
    private readonly IDictionary<CompositeIntegralTriplet, object> _dictionary = new Dictionary<CompositeIntegralTriplet, object>(); 
} 

public sealed class CompositeIntegralTriplet : IEquatable<CompositeIntegralTriplet> 
{ 
    public CompositeIntegralTriplet(int first, int second, int third) 
    { 
     First = first; 
     Second = second; 
     Third = third; 
    } 

    public int First { get; } 
    public int Second { get; } 
    public int Third { get; } 

    public override bool Equals(object other) 
    { 
     var otherAsTriplet = other as CompositeIntegralTriplet; 
     return Equals(otherAsTriplet); 
    } 

    public override int GetHashCode() 
    { 
     unchecked 
     { 
      var hashCode = First; 
      hashCode = (hashCode*397)^Second; 
      hashCode = (hashCode*397)^Third; 
      return hashCode; 
     } 
    } 

    public bool Equals(CompositeIntegralTriplet other) => other != null && First == other.First && Second == other.Second && Third == other.Third; 
} 
관련 문제