OpenClonk
C4SortObjectValue Class Reference

#include <C4FindObject.h>

Inheritance diagram for C4SortObjectValue:
[legend]
Collaboration diagram for C4SortObjectValue:
[legend]

Public Member Functions

 C4SortObjectValue ()
 
int32_t Compare (C4Object *pObj1, C4Object *pObj2) override
 
bool PrepareCache (const C4ValueArray *pObjs) override
 
int32_t CompareCache (int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override
 
void SortObjects (C4ValueArray *pArray)
 

Static Public Member Functions

static C4SortObjectCreateByValue (const C4Value &Data, const C4Object *context=nullptr)
 
static C4SortObjectCreateByValue (int32_t iType, const C4ValueArray &Data, const C4Object *context=nullptr)
 

Protected Member Functions

int32_t CompareGetValue (C4Object *pFor) override
 

Detailed Description

Definition at line 532 of file C4FindObject.h.

Constructor & Destructor Documentation

◆ C4SortObjectValue()

C4SortObjectValue::C4SortObjectValue ( )
inline

Definition at line 535 of file C4FindObject.h.

Member Function Documentation

◆ Compare()

int32_t C4SortObjectByValue::Compare ( C4Object pObj1,
C4Object pObj2 
)
overridevirtualinherited

Implements C4SortObject.

Definition at line 971 of file C4FindObject.cpp.

References C4SortObjectByValue::CompareGetValue().

Referenced by C4SortObjectMultiple::Compare().

972 {
973  // this is rather slow, should only be called in special cases
974 
975  // make sure to hardcode the call order, as it might lead to desyncs otherwise [Icewing]
976  int32_t iValue1 = CompareGetValue(pObj1);
977  int32_t iValue2 = CompareGetValue(pObj2);
978  return iValue2 - iValue1;
979 }
virtual int32_t CompareGetValue(C4Object *pOf)=0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CompareCache()

int32_t C4SortObjectByValue::CompareCache ( int32_t  iObj1,
int32_t  iObj2,
C4Object pObj1,
C4Object pObj2 
)
overridevirtualinherited

Reimplemented from C4SortObject.

Definition at line 981 of file C4FindObject.cpp.

Referenced by C4SortObjectMultiple::CompareCache().

982 {
983  assert(pVals); assert(iObj1 >= 0 && iObj1 < iSize); assert(iObj2 >= 0 && iObj2 < iSize);
984  // Might overflow for large values...!
985  return pVals[iObj2] - pVals[iObj1];
986 }
Here is the caller graph for this function:

◆ CompareGetValue()

int32_t C4SortObjectValue::CompareGetValue ( C4Object pFor)
overrideprotectedvirtual

Implements C4SortObjectByValue.

Definition at line 1066 of file C4FindObject.cpp.

References C4Object::GetValue(), and NO_OWNER.

1067 {
1068  return pFor->GetValue(nullptr, NO_OWNER);
1069 }
int32_t GetValue(C4Object *pInBase, int32_t iForPlayer)
Definition: C4Object.cpp:1690
const int NO_OWNER
Definition: C4Constants.h:137
Here is the call graph for this function:

◆ CreateByValue() [1/2]

C4SortObject * C4SortObject::CreateByValue ( const C4Value Data,
const C4Object context = nullptr 
)
staticinherited

Definition at line 853 of file C4FindObject.cpp.

References C4FindObject::CreateByValue(), and C4Value::getArray().

Referenced by C4FindObject::CreateByValue(), and C4SortObject::CreateByValue().

854 {
855  // Must be an array
856  const C4ValueArray *pArray = C4Value(DataVal).getArray();
857  if (!pArray) return nullptr;
858  const C4ValueArray &Data = *pArray;
859  int32_t iType = Data[0].getInt();
860  return CreateByValue(iType, Data, context);
861 }
C4ValueArray * getArray() const
Definition: C4Value.h:118
static C4SortObject * CreateByValue(const C4Value &Data, const C4Object *context=nullptr)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateByValue() [2/2]

C4SortObject * C4SortObject::CreateByValue ( int32_t  iType,
const C4ValueArray Data,
const C4Object context = nullptr 
)
staticinherited

Definition at line 863 of file C4FindObject.cpp.

References C4SO_Distance, C4SO_Func, C4SO_Mass, C4SO_Multiple, C4SO_Random, C4SO_Reverse, C4SO_Speed, C4SO_Value, C4SortObject::CreateByValue(), C4ValueArray::GetSize(), C4Object::GetX(), C4Object::GetY(), iSize, and C4SortObjectFunc::SetPar().

864 {
865  switch (iType)
866  {
867  case C4SO_Reverse:
868  {
869  // create child sort
870  C4SortObject *pChildSort = C4SortObject::CreateByValue(Data[1], context);
871  if (!pChildSort) return nullptr;
872  // wrap
873  return new C4SortObjectReverse(pChildSort);
874  }
875 
876  case C4SO_Multiple:
877  {
878  // Trivial case (one sort)
879  if (Data.GetSize() == 2)
880  {
881  return C4SortObject::CreateByValue(Data[1], context);
882  }
883  // Create all children
884  int32_t i;
885  C4SortObject **ppSorts = new C4SortObject *[Data.GetSize() - 1];
886  for (i = 0; i < Data.GetSize() - 1; i++)
887  {
888  ppSorts[i] = C4SortObject::CreateByValue(Data[i+1], context);
889  }
890  // Count real entries, move them to start of list
891  int32_t iSize = 0;
892  for (i = 0; i < Data.GetSize() - 1; i++)
893  if (ppSorts[i])
894  if (iSize++ != i)
895  ppSorts[iSize-1] = ppSorts[i];
896  // Create
897  return new C4SortObjectMultiple(iSize, ppSorts);
898  }
899 
900  case C4SO_Distance:
901  {
902  int32_t x = Data[1].getInt();
903  int32_t y = Data[2].getInt();
904  if (context)
905  {
906  x += context->GetX();
907  y += context->GetY();
908  }
909  return new C4SortObjectDistance(x, y);
910  }
911 
912  case C4SO_Random:
913  return new C4SortObjectRandom();
914 
915  case C4SO_Speed:
916  return new C4SortObjectSpeed();
917 
918  case C4SO_Mass:
919  return new C4SortObjectMass();
920 
921  case C4SO_Value:
922  return new C4SortObjectValue();
923 
924  case C4SO_Func:
925  {
926  // Get function name
927  C4String *pStr = Data[1].getStr();
928  if (!pStr) return nullptr;
929  // Construct
930  C4SortObjectFunc *pSO = new C4SortObjectFunc(pStr);
931  // Add parameters
932  for (int i = 2; i < Data.GetSize(); i++)
933  pSO->SetPar(i - 2, Data[i]);
934  // Done
935  return pSO;
936  }
937 
938  }
939  return nullptr;
940 }
int32_t GetX() const
Definition: C4Object.h:287
int32_t GetSize() const
Definition: C4ValueArray.h:36
int32_t GetY() const
Definition: C4Object.h:288
static C4SortObject * CreateByValue(const C4Value &Data, const C4Object *context=nullptr)
void SetPar(int i, const C4Value &val)
int iSize
Definition: TstC4NetIO.cpp:32
Here is the call graph for this function:

◆ PrepareCache()

bool C4SortObjectByValue::PrepareCache ( const C4ValueArray pObjs)
overridevirtualinherited

Reimplemented from C4SortObject.

Definition at line 959 of file C4FindObject.cpp.

References C4Value::_getObj(), C4SortObjectByValue::CompareGetValue(), C4ValueArray::GetItem(), and C4ValueArray::GetSize().

Referenced by C4SortObjectMultiple::PrepareCache().

960 {
961  // Clear old cache
962  delete [] pVals; pVals = nullptr; iSize = 0;
963  // Create new cache
964  iSize = pObjs->GetSize(); pVals = new int32_t [iSize];
965  for (int32_t i = 0; i < iSize; i++)
966  pVals[i] = CompareGetValue(pObjs->GetItem(i)._getObj());
967  // Okay
968  return true;
969 }
C4Object * _getObj() const
Definition: C4Value.cpp:73
virtual int32_t CompareGetValue(C4Object *pOf)=0
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
int32_t GetSize() const
Definition: C4ValueArray.h:36
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SortObjects()

void C4SortObject::SortObjects ( C4ValueArray pArray)
inherited

Definition at line 942 of file C4FindObject.cpp.

References C4ValueArray::Sort().

Referenced by C4FindObject::FindMany().

943 {
944  pArray->Sort(*this);
945 }
void Sort(class C4SortObject &rSort)
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: