Algo LowerBound

¾ð¸®¾óÀÇ Algo·Î ÀÌÁø °Ë»ö ÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ­ ¾Ë¾Æº»´Ù.

³ªÁß¿¡ ¿¹Á¦¿¡ »ç¿ëµÉ ±¸Á¶Ã¼ÀÌ´Ù.
//Çì´õ ÆÄÀÏ
USTRUCT(BlueprintType)
struct FMyStruct
{
    GENERATED_BODY()
public:
    FMyStruct();
    ~FMyStruct();
    UPROPERTY()
    FString Name;
    UPROPERTY()
    int id = 0;
};

//CPP ÆÄÀÏ
FMyStruct::FMyStruct()
{
    //UE_LOG(LogTemp, Warning, TEXT("%p, %s (%d)"), this, *FString(__FUNCTION__), __LINE__);
}

FMyStruct::~FMyStruct()
{
    //UE_LOG(LogTemp, Warning, TEXT("%p, %s (%d)"), this, *FString(__FUNCTION__), __LINE__);
}

ÀÌÁø °Ë»öÀ¸·Î À妽º ã±â

Algo::BinarySearch´Â ¿À¸§Â÷¼øÀ϶§ ÀÌÁø °Ë»öÀ¸·Î ãÀ¸¸é À妽º¸¦ ¹ÝȯÇÑ´Ù.
    TArray<int> IntList;
    for (int n = 0; n < 10; n++)
        IntList.Add(n * 10);

    int Index = Algo::BinarySearch(IntList, 0);  //°á°ú 0
    int Index1 = Algo::BinarySearch(IntList, 10); //°á°ú 1
    int Index2 = Algo::BinarySearch(IntList, 80);  //°á°ú 8

±¸Á¶Ã¼¸¦ ÀÌÁø °Ë»öÀ¸·Î À妽º ã±â

    TArray<FMyStruct> MyList;
    FMyStruct mySt;

    for (int n = 0; n < 10; ++n)
    {
        mySt.id = n;
        mySt.Name = FString::FromInt(n * 100);
        MyList.Add(mySt);
    }

    int MyListIndex3 = Algo::BinarySearchBy(MyList, 3, &FMyStruct::id);  //°á°ú 3
    int MyListIndex6 = Algo::BinarySearchBy(MyList, 6, &FMyStruct::id);  //°á°ú 6
    int MyListIndex9 = Algo::BinarySearchBy(MyList, 9, &FMyStruct::id);  //°á°ú 9
    int MyListIndex20 = Algo::BinarySearchBy(MyList, 20, &FMyStruct::id); //°á°ú -1

¿¬»êÀÚ ÀçÁ¤ÀǸ¦ ÇÏÁö ¾Ê¾Æµµ Àß Ã£´Â´Ù.
´ÙÀ½°ú °°ÀÌ ±¸Á¶Ã¼ÀÇ ¿¬»êÀÚ ÀçÁ¤ÀǸ¦ ÇÏ¿©µµ ½ÇÇàÀÌ ¾ÈµÈ´Ù.
¾ð¸®¾óÀ̶ó ±×·±°¡ ¹æ¹ýÀ» ¸ð¸£°Ú´Ù.
    bool operator==(const FMyStruct& input)
    {
        return (id == input.id);
    }

    FMyStruct& operator=(const FMyStruct& input)
    {
        id = input.id;
        Name = input.Name;
        return *this;
    }

ÇØ´ç °ªÀÌ ¾î´À À妽º »çÀÌ¿¡ ÀÖ´ÂÁö °Ë»ö

¹è¿­¿¡ Ãß°¡ÇÏÁö ¾Ê°í ¾î´À À妽º°¡ µÉÁö ¾Ë¼ö ÀÖ´Ù.
¾Æ·¡ LoweBound´Â ã°íÀÚ ÇÏ´Â À妽º°¡ ¾î´À À妽º »çÀÌ¿¡ ÀÖ´ÂÁö °Ë»öÇÑ´Ù.
3¹ø° ÀÎÀÚ°¡ ¾øÀ¸¸é ¿À¸§Â÷¼ø¿¡¼­ TLess·Î °Ë»öÇÑ´Ù.
    TArray<int> IntList;
    for (int n = 0; n < 10; n++)
        IntList.Add(n * 10);

    int Lindex = Algo::LowerBound(IntList, 5);  //Result 1
    int Lindex2 = Algo::LowerBound(IntList, 85); //Result 9
    int Lindex3 = Algo::LowerBound(IntList, -1); //Result 0
    int Lindex4 = Algo::LowerBound(IntList, 100); //Result 10

    //¹è¿­¿¡ Ãß°¡ÇÏÁö ¾Ê°í ¿À¸§Â÷¼ø ¼øÀ§¸¦ ±¸ÇÑ´Ù. ¼øÀ§¸¦ ÇÒ·Á¸é +1À» ´õÇÑ´Ù.
    TArray<int> IntIncList;
    for (int n = 0; n < 10; ++n)
        IntIncList.Add(n * 10);

    int IncLindex = Algo::LowerBound(IntIncList, 100, TLess<int>()); //10
    int IncLindex1 = Algo::LowerBound(IntIncList, -100, TLess<int>()); //0
    int IncLindex2 = Algo::LowerBound(IntIncList, 85); //9
    int IncLindex3 = Algo::LowerBound(IntIncList, 90); //9
    int IncLindex4 = Algo::LowerBound(IntIncList, 90); //9

³»¸²Â÷¼øÀ¸·Î ¾î´À À妽º »çÀÌ¿¡ ÀÖ´ÂÁö °Ë»ö

³»¸²Â÷¼øÀ¸·Î Á¤·Ä µÇ¾î ÀÖÀ» ¶§´Â ´ÙÀ½°ú °°ÀÌ ¶÷´Ù ÇÔ¼ö¸¦ ¸¸µé°Å³ª TGreater<int>() ÇÔ¼ö¸¦ ÀÌ¿ëÇØ °Ë»öÇÑ´Ù.

    TArray<int> IntList;
    for (int n = 9; n >= 0; --n)
        IntList.Add(n * 10);

    auto IntGreater = [](int A, int B)
    {
        return A > B;
    };

    int Lindex = Algo::LowerBound(IntList, 100, TGreater<int>()); //0

    int Lindex1 = Algo::LowerBound(IntList, -100, TGreater<int>()); //10
    int Lindex2 = Algo::LowerBound(IntList, 85, TGreater<int>()); //1
    int Lindex3 = Algo::LowerBound(IntList, 90, IntGreater); //0
    int Lindex4 = Algo::LowerBound(IntList, 25, IntGreater); //7
    int Lindex5 = Algo::LowerBound(IntList, 20, IntGreater); //7
    int Lindex6 = Algo::LowerBound(IntList, 90, IntGreater); //9

±¸Á¶Ã¼·Î ¾î´À À妽º »çÀÌ¿¡ ÀÖ´ÂÁö °Ë»ö

±¸Á¶Ã¼·Î °Ë»öÇÒ¶§´Â ¾Æ·¡¿Í °°ÀÌ ÀÛ¼ºÇÑ´Ù.

    TArray<FMyStruct> TestList;
    for (int k = 0; k < 10; ++k)
    {
        FMyStruct Item;
        Item.id = k * 10;
        Item.Name = FString::FromInt(Item.id) + TEXT("AAA");
        TestList.Add(Item);
    }

    auto MyStructLess = [](const FMyStruct& A, int B)
    {
        return A.id < B;
    };

    int StLindex = Algo::LowerBound(TestList, 100, MyStructLess); //10
    int StLindex1 = Algo::LowerBound(TestList, -100, MyStructLess); //0
    int StLindex2 = Algo::LowerBound(TestList, 85, MyStructLess); //9
    int StLindex3 = Algo::LowerBound(TestList, 90, MyStructLess); //9
    int StLindex4 = Algo::LowerBound(TestList, 90, MyStructLess); //9