Add MapSort
This commit is contained in:
parent
bb37025026
commit
71f37d0c42
|
@ -24,15 +24,17 @@ static ArrayId stackArrayId = 1;
|
||||||
#define ARRAY_ACTION_REVERSE (-4)
|
#define ARRAY_ACTION_REVERSE (-4)
|
||||||
#define ARRAY_ACTION_SHUFFLE (-5)
|
#define ARRAY_ACTION_SHUFFLE (-5)
|
||||||
|
|
||||||
template <class T>
|
template <class T, typename Compare>
|
||||||
static void ListSort(std::vector<T>& arr, int type)
|
static void ListSort(std::vector<T>& arr, int type, Compare cmp)
|
||||||
{
|
{
|
||||||
|
auto kek = std::less<T>();
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ARRAY_ACTION_SORT: // sort ascending
|
case ARRAY_ACTION_SORT: // sort ascending
|
||||||
std::sort(arr.begin(), arr.end());
|
std::sort(arr.begin(), arr.end(), cmp);
|
||||||
break;
|
break;
|
||||||
case ARRAY_ACTION_RSORT: // sort descending
|
case ARRAY_ACTION_RSORT: // sort descending
|
||||||
std::sort(arr.rbegin(), arr.rend());
|
std::sort(arr.rbegin(), arr.rend(), cmp);
|
||||||
break;
|
break;
|
||||||
case ARRAY_ACTION_REVERSE: // reverse elements
|
case ARRAY_ACTION_REVERSE: // reverse elements
|
||||||
std::reverse(arr.rbegin(), arr.rend());
|
std::reverse(arr.rbegin(), arr.rend());
|
||||||
|
@ -123,7 +125,7 @@ public:
|
||||||
};
|
};
|
||||||
values = newValues;
|
values = newValues;
|
||||||
} else if (newLen >= ARRAY_ACTION_SHUFFLE) {
|
} else if (newLen >= ARRAY_ACTION_SHUFFLE) {
|
||||||
ListSort(values, newLen);
|
ListSort(values, newLen, std::less<SFallScriptValue>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -134,6 +136,8 @@ private:
|
||||||
std::vector<SFallScriptValue> keys;
|
std::vector<SFallScriptValue> keys;
|
||||||
std::map<SFallScriptValue, SFallScriptValue> map;
|
std::map<SFallScriptValue, SFallScriptValue> map;
|
||||||
|
|
||||||
|
void MapSort(int newLen);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SFallArrayAssoc() = delete;
|
SFallArrayAssoc() = delete;
|
||||||
|
|
||||||
|
@ -226,12 +230,28 @@ public:
|
||||||
keys = newKeys;
|
keys = newKeys;
|
||||||
} else if (newLen < 0) {
|
} else if (newLen < 0) {
|
||||||
if (newLen < (ARRAY_ACTION_SHUFFLE - 2)) return;
|
if (newLen < (ARRAY_ACTION_SHUFFLE - 2)) return;
|
||||||
// TODO
|
MapSort(newLen);
|
||||||
// MapSort(arr, newlen);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void SFallArrayAssoc::MapSort(int type)
|
||||||
|
{
|
||||||
|
bool sortByValue = false;
|
||||||
|
if (type < ARRAY_ACTION_SHUFFLE) {
|
||||||
|
type += 4;
|
||||||
|
sortByValue = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sortByValue) {
|
||||||
|
ListSort(keys, type, [this](const SFallScriptValue& a, const SFallScriptValue& b) -> bool {
|
||||||
|
return this->map[a] < this->map[b];
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
ListSort(keys, type, std::less<SFallScriptValue>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
using ArraysMap
|
using ArraysMap
|
||||||
= std::unordered_map<ArrayId, std::unique_ptr<SFallArray>>;
|
= std::unordered_map<ArrayId, std::unique_ptr<SFallArray>>;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue