MCPcopy Create free account
hub / github.com/deathkiller/jazz2-native / Sort

Method Sort

Sources/Jazz2/Scripting/RegisterArray.cpp:1312–1450  ·  view source on GitHub ↗

Internal

Source from the content-addressed store, hash-verified

1310
1311 // Internal
1312 void CScriptArray::Sort(std::uint32_t startAt, std::uint32_t count, bool asc)
1313 {
1314 // Subtype isn't primitive and doesn't have opCmp
1315 SArrayCache* cache = static_cast<SArrayCache*>(objType->GetUserData(ARRAY_CACHE));
1316 if (subTypeId & ~asTYPEID_MASK_SEQNBR) {
1317 if (cache == nullptr || cache->cmpFunc == 0) {
1318 asIScriptContext* ctx = asGetActiveContext();
1319 asITypeInfo* subType = objType->GetEngine()->GetTypeInfoById(subTypeId);
1320
1321 // Throw an exception
1322 if (ctx != nullptr) {
1323 char tmp[256];
1324
1325 if (cache && cache->cmpFuncReturnCode == asMULTIPLE_FUNCTIONS) {
1326#if defined(DEATH_TARGET_MSVC)
1327 sprintf_s(tmp, arraySize(tmp), "Type '%s' has multiple matching opCmp methods", subType->GetName());
1328#else
1329 sprintf(tmp, "Type '%s' has multiple matching opCmp methods", subType->GetName());
1330#endif
1331 } else {
1332#if defined(DEATH_TARGET_MSVC)
1333 sprintf_s(tmp, arraySize(tmp), "Type '%s' does not have a matching opCmp method", subType->GetName());
1334#else
1335 sprintf(tmp, "Type '%s' does not have a matching opCmp method", subType->GetName());
1336#endif
1337 }
1338 ctx->SetException(tmp);
1339 }
1340
1341 return;
1342 }
1343 }
1344
1345 // No need to sort
1346 if (count < 2) {
1347 return;
1348 }
1349
1350 std::int32_t start = startAt;
1351 std::int32_t end = startAt + count;
1352
1353 // Check if we could access invalid item while sorting
1354 if (start >= (std::int32_t)buffer->numElements || end > (std::int32_t)buffer->numElements) {
1355 asIScriptContext* ctx = asGetActiveContext();
1356
1357 // Throw an exception
1358 if (ctx) {
1359 ctx->SetException("Index out of bounds");
1360 }
1361
1362 return;
1363 }
1364
1365 if (subTypeId & ~asTYPEID_MASK_SEQNBR) {
1366 asIScriptContext* cmpContext = 0;
1367 bool isNested = false;
1368
1369 // Try to reuse the active context

Callers

nothing calls this directly

Calls 9

sortFunction · 0.85
GetEngineMethod · 0.80
AbortMethod · 0.80
ExecuteMethod · 0.80
arraySizeFunction · 0.50
LessClass · 0.50
GetUserDataMethod · 0.45
GetNameMethod · 0.45
GetStateMethod · 0.45

Tested by

no test coverage detected