OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4SortObjectMultiple Class Reference

#include <C4FindObject.h>

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

Public Member Functions

 C4SortObjectMultiple (int32_t iCnt, C4SortObject **ppSorts, bool fFreeArray=true)
 
 ~C4SortObjectMultiple () 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 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
 

Detailed Description

Definition at line 471 of file C4FindObject.h.

Constructor & Destructor Documentation

C4SortObjectMultiple::C4SortObjectMultiple ( int32_t  iCnt,
C4SortObject **  ppSorts,
bool  fFreeArray = true 
)
inline

Definition at line 474 of file C4FindObject.h.

475  : C4SortObject(), fFreeArray(fFreeArray), iCnt(iCnt), ppSorts(ppSorts) {}
C4SortObject()=default
int iCnt
Definition: TstC4NetIO.cpp:32
C4SortObjectMultiple::~C4SortObjectMultiple ( )
override

Definition at line 1001 of file C4FindObject.cpp.

1002 {
1003  for (int32_t i=0; i<iCnt; ++i) delete ppSorts[i];
1004  if (fFreeArray) delete [] ppSorts;
1005 }
int iCnt
Definition: TstC4NetIO.cpp:32

Member Function Documentation

int32_t C4SortObjectMultiple::Compare ( C4Object pObj1,
C4Object pObj2 
)
overrideprotectedvirtual

Implements C4SortObject.

Definition at line 1007 of file C4FindObject.cpp.

1008 {
1009  // return first comparison that's nonzero
1010  int32_t iCmp;
1011  for (int32_t i=0; i<iCnt; ++i)
1012  if ((iCmp = ppSorts[i]->Compare(pObj1, pObj2)))
1013  return iCmp;
1014  // all comparisons equal
1015  return 0;
1016 }
int32_t Compare(C4Object *pObj1, C4Object *pObj2) override
int iCnt
Definition: TstC4NetIO.cpp:32
int32_t C4SortObjectMultiple::CompareCache ( int32_t  iObj1,
int32_t  iObj2,
C4Object pObj1,
C4Object pObj2 
)
overrideprotectedvirtual

Reimplemented from C4SortObject.

Definition at line 1027 of file C4FindObject.cpp.

1028 {
1029  // return first comparison that's nonzero
1030  int32_t iCmp;
1031  for (int32_t i=0; i<iCnt; ++i)
1032  if ((iCmp = ppSorts[i]->CompareCache(iObj1, iObj2, pObj1, pObj2)))
1033  return iCmp;
1034  // all comparisons equal
1035  return 0;
1036 }
int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override
int iCnt
Definition: TstC4NetIO.cpp:32
C4SortObject * C4SortObject::CreateByValue ( const C4Value Data,
const C4Object context = nullptr 
)
staticinherited

Definition at line 846 of file C4FindObject.cpp.

References C4Value::getArray().

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

847 {
848  // Must be an array
849  const C4ValueArray *pArray = C4Value(DataVal).getArray();
850  if (!pArray) return nullptr;
851  const C4ValueArray &Data = *pArray;
852  int32_t iType = Data[0].getInt();
853  return CreateByValue(iType, Data, context);
854 }
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:

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

Definition at line 856 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().

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

bool C4SortObjectMultiple::PrepareCache ( const C4ValueArray pObjs)
overrideprotectedvirtual

Reimplemented from C4SortObject.

Definition at line 1018 of file C4FindObject.cpp.

1019 {
1020  bool fCaches = false;
1021  for (int32_t i=0; i<iCnt; ++i)
1022  fCaches |= ppSorts[i]->PrepareCache(pObjs);
1023  // return wether a sort citerion uses a cache
1024  return fCaches;
1025 }
int iCnt
Definition: TstC4NetIO.cpp:32
bool PrepareCache(const C4ValueArray *pObjs) override
void C4SortObject::SortObjects ( C4ValueArray pArray)
inherited

Definition at line 935 of file C4FindObject.cpp.

References C4ValueArray::Sort().

Referenced by C4FindObject::FindMany().

936 {
937  pArray->Sort(*this);
938 }
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: