Skip to content
Snippets Groups Projects
Commit c3058663 authored by Ansgar Burchardt's avatar Ansgar Burchardt
Browse files

[cleanup] pack.cc, unpack.cc: use `std::sort`

This also eliminates the global variable `currentObjectMem`.
parent 3ad36f48
No related branches found
No related tags found
1 merge request!96DDD: move global state to a context object
......@@ -35,6 +35,8 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include "dddi.h"
#include "xfer.h"
......@@ -91,53 +93,15 @@ START_UGDIM_NAMESPACE
/****************************************************************************/
static int sort_SymTabEntries (const void *e1, const void *e2)
static bool sort_SymTabEntries (const SYMTAB_ENTRY& a, const SYMTAB_ENTRY& b)
{
SYMTAB_ENTRY *ci1, *ci2;
ci1 = (SYMTAB_ENTRY *)e1;
ci2 = (SYMTAB_ENTRY *)e2;
if (ci1->gid < ci2->gid) return(-1);
if (ci1->gid == ci2->gid) return(0);
return(1);
return a.gid < b.gid;
}
static char *currentObjectMem;
static int sort_ObjTabEntries (const void *e1, const void *e2)
static bool sort_MsgSize (const XFERMSG* a, const XFERMSG* b)
{
DDD_GID g1, g2;
g1 = OTE_GID(currentObjectMem, (OBJTAB_ENTRY *)e1);
g2 = OTE_GID(currentObjectMem, (OBJTAB_ENTRY *)e2);
/* sort with ascending gid */
if (g1 < g2) return(-1);
if (g1 > g2) return(1);
return(0);
}
static int sort_MsgSize (const void *e1, const void *e2)
{
XFERMSG *xm1, *xm2;
size_t s1, s2;
xm1 = *((XFERMSG **)e1);
xm2 = *((XFERMSG **)e2);
s1 = LC_GetBufferSize(xm1->msg_h);
s2 = LC_GetBufferSize(xm2->msg_h);
/* sort with descending msg-size */
if (s1 < s2) return(1);
if (s1 > s2) return(-1);
return(0);
return LC_GetBufferSize(a->msg_h) > LC_GetBufferSize(b->msg_h);
}
......@@ -528,12 +492,15 @@ static void XferPackSingleMsg (DDD::DDDContext& context, XFERMSG *msg)
/* sort SymTab, ObjTab and CplTab */
/* sort SymTab according to the global ids stored there */
qsort(theSymTab, actSym, sizeof(SYMTAB_ENTRY), sort_SymTabEntries);
std::sort(theSymTab, theSymTab + actSym, sort_SymTabEntries);
/* sort ObjTab according to their global ids */
/* sorting of objtab is necessary!! (see AcceptObjFromMsg) KB 960812 */
currentObjectMem = theObjects;
qsort(theObjTab, msg->nObjects, sizeof(OBJTAB_ENTRY), sort_ObjTabEntries);
const auto sort_ObjTabEntries = [=](const OBJTAB_ENTRY& a, const OBJTAB_ENTRY& b) {
/* sort with ascending gid */
return OTE_GID(theObjects, &a) < OTE_GID(theObjects, &b);
};
std::sort(theObjTab, theObjTab + msg->nObjects, sort_ObjTabEntries);
/* substitute all pointers by index into SymTab */
......@@ -606,7 +573,7 @@ RETCODE XferPackMsgs (DDD::DDDContext& context, XFERMSG *theMsgs)
for(i=0, xm=theMsgs; i<n; xm=xm->next, i++) xm_array[i] = xm;
/* sort array and relink list */
qsort(xm_array, n, sizeof(XFERMSG *), sort_MsgSize);
std::sort(xm_array, xm_array + n, sort_MsgSize);
theMsgs = xm_array[0];
for(i=0; i<n-1; i++) xm_array[i]->next = xm_array[i+1];
if (n>1) xm_array[n-1]->next = NULL;
......
......@@ -35,6 +35,9 @@
#include <cstring>
#include <cassert>
#include <algorithm>
#include <tuple>
#include "dddi.h"
#include "xfer.h"
......@@ -72,49 +75,28 @@ static void NEW_AddCpl(DDD_PROC destproc, DDD_GID objgid, DDD_PROC cplproc, DDD_
}
static int sort_TENewCpl (const void *e1, const void *e2)
static bool sort_TENewCpl (const TENewCpl& a, const TENewCpl& b)
{
TENewCpl *ci1, *ci2;
ci1 = (TENewCpl *)e1;
ci2 = (TENewCpl *)e2;
if (ci1->_gid < ci2->_gid) return(-1);
if (ci1->_gid > ci2->_gid) return(1);
if (ci1->_dest < ci2->_dest) return(-1);
if (ci1->_dest > ci2->_dest) return(1);
/* sorting according to priority is not necessary anymore,
equal items with different priorities will be sorted
out according to PriorityMerge(). KB 970326
if (ci1->prio < ci2->prio) return(-1);
if (ci1->prio > ci2->prio) return(1);
*/
return(0);
return std::tie(a._gid, a._dest) < std::tie(b._gid, b._dest);
}
static int sort_ObjTabPtrs (const void *e1, const void *e2)
static bool sort_ObjTabPtrs (const OBJTAB_ENTRY* a, const OBJTAB_ENTRY* b)
{
DDD_HDR ci1, ci2;
ci1 = (*(OBJTAB_ENTRY **)e1)->hdr;
ci2 = (*(OBJTAB_ENTRY **)e2)->hdr;
/* sort with ascending gid */
if (OBJ_GID(ci1) < OBJ_GID(ci2)) return(-1);
if (OBJ_GID(ci1) > OBJ_GID(ci2)) return(1);
/* sort with decreasing priority */
/* not necessary anymore. see first phase of
AcceptReceivedObjects() for details. KB 970128
if (OBJ_PRIO(ci1) < OBJ_PRIO(ci2)) return(1);
if (OBJ_PRIO(ci1) > OBJ_PRIO(ci2)) return(-1);
*/
return(0);
return OBJ_GID(a->hdr) < OBJ_GID(b->hdr);
}
......@@ -1440,7 +1422,7 @@ static int CompressNewCpl (TENewCpl *tabNC, int nNC)
int nNCnew;
int iNC;
qsort(tabNC, nNC, sizeof(TENewCpl), sort_TENewCpl);
std::sort(tabNC, tabNC + nNC, sort_TENewCpl);
nNCnew = iNC = 0;
while (iNC<nNC)
......@@ -1583,7 +1565,7 @@ void XferUnpack (DDD::DDDContext& context, LC_MSGHANDLE *theMsgs, int nRecvMsgs,
if (lenObjTab>0)
{
qsort(unionObjTab, lenObjTab, sizeof(OBJTAB_ENTRY *), sort_ObjTabPtrs);
std::sort(unionObjTab, unionObjTab + lenObjTab, sort_ObjTabPtrs);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment