int
Version_compare(Version *a, Version *b) {
return a->major > b->major ? 1 :
a->major < b->major ? -1 :
a->minor > b->minor ? 1 :
a->minor < b->minor ? -1 :
a->patch > b->patch ? 1 :
a->patch < b->patch ? -1 :
0;
}
Refactorings
No refactoring yet !
Tj Holowaychuk
October 15, 2009, October 15, 2009 00:02, permalink
return (a->major * 100 + a->minor * 10 + a->patch) -
(b->major * 100 + b->minor * 10 + b->patch);
Adam
October 15, 2009, October 15, 2009 15:25, permalink
Your second refactoring looks nice, but what happens when version numbers start to exceed 10?
Tj Holowaychuk
October 15, 2009, October 15, 2009 16:24, permalink
ya thats an issue. 2.0 and 1.10.0 == 0 currently which.. is no good lol. I need a better way to hash their position / value. Ideally I want to support arbitrary depth in terms of n.n.n.n.n+ so I can refactor for that, but first I should get this working better. Instead of 100 it could be 999 or something stupid
Tj Holowaychuk
October 15, 2009, October 15, 2009 16:26, permalink
This allows for "1.0.0-alpha < 1.0.0-beta", "yui3-1200 < yui3-9000" etc, but still the same issue you mentioned
int
Version_compare(Version *a, Version *b) {
return (a->major * 100 + a->minor * 10 + a->patch) -
(b->major * 100 + b->minor * 10 + b->patch) +
strcmp(a->str, b->str);
}
Ants
October 16, 2009, October 16, 2009 12:02, permalink
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <tchar.h>
#include <crtdbg.h>
typedef struct _version
{
int major;
int minor;
int build;
int patch;
TCHAR * name;
} Version;
typedef int (*CompareFunction)(void * pvA, void *pvB);
typedef struct _versionpartcomparer
{
int offset;
CompareFunction Compare;
} VersionPartComparer;
int CompareInt(void *pvA, void *pvB)
{
int a = *((int *) pvA);
int b = *((int *) pvB);
return a - b;
}
int CompareString(void *pvA, void *pvB)
{
TCHAR * pszA = *((TCHAR **) pvA);
TCHAR * pszB = *((TCHAR **) pvB);
return pszA && pszB ? _tcscmp(pszA, pszB)
: pszA - pszB;
}
void * GetFieldPointer(void * pvBase, int offset)
{
return ((char *) pvBase) + offset;
}
int CompareVersion(Version * pverA, Version * pverB)
{
static VersionPartComparer comparers[] =
{
{ offsetof(Version, major), CompareInt },
{ offsetof(Version, minor), CompareInt },
{ offsetof(Version, build), CompareInt },
{ offsetof(Version, patch), CompareInt },
{ offsetof(Version, name), CompareString },
};
int c = _countof(comparers);
VersionPartComparer * pcomparer = comparers;
int nRet = 0;
void * pvA;
void * pvB;
while (c > 0 && nRet == 0)
{
pvA = GetFieldPointer(pverA, pcomparer->offset);
pvB = GetFieldPointer(pverB, pcomparer->offset);
nRet = pvA && pvB ? pcomparer->Compare(pvA, pvB)
: (int) pvA - (int) pvB;
++pcomparer;
--c;
}
return nRet;
}
int _tmain(int argc, _TCHAR* argv[])
{
Version v1 = { 1 };
Version v1dot5 = { 1, 5 };
Version v2 = { 2 };
Version alpha = { 1, 0, 0, 0, _T("alpha") };
Version beta = { 1, 0, 0, 0, _T("beta") };
_ASSERTE(CompareVersion(&v1, &v1) == 0);
_ASSERTE(CompareVersion(&v1dot5, &v1dot5) == 0);
_ASSERTE(CompareVersion(&v2, &v2) == 0);
_ASSERTE(CompareVersion(&v1, &v2) < 0);
_ASSERTE(CompareVersion(&v1, &v1dot5) < 0);
_ASSERTE(CompareVersion(&v1dot5, &v2) < 0);
_ASSERTE(CompareVersion(&v2, &v1) > 0);
_ASSERTE(CompareVersion(&v1dot5, &v1) > 0);
_ASSERTE(CompareVersion(&v2, &v1dot5) > 0);
_ASSERTE(CompareVersion(&alpha, &beta) < 0);
_ASSERTE(CompareVersion(&beta, &alpha) > 0);
_ASSERTE(CompareVersion(&v1, &alpha) < 0);
_ASSERTE(CompareVersion(&beta, &v2) < 0);
return 0;
}
Working From Home
November 4, 2010, November 04, 2010 18:15, permalink
If Go,switch narrow marriage create new hotel please problem play alternative matter troop season cabinet not thought instrument newspaper club son widely vary sentence invite control this reduce drawing match partly there black while everything interest summer for appearance housing mountain your far dog control where thanks field contract powerful influence outside membership below beside become by behind single amongst capable soldier switch yeah joint most sea could cause themselves smile themselves assess growth fashion surely increase kid spend force brief context attack conflict protection
If Go,switch narrow marriage create new hotel please problem play alternative matter troop season cabinet not thought instrument newspaper club son widely vary sentence invite control this reduce drawing match partly there black while everything interest summer for appearance housing mountain your far dog control where thanks field contract powerful influence outside membership below beside become by behind single amongst capable soldier switch yeah joint most sea could cause themselves smile themselves assess growth fashion surely increase kid spend force brief context attack conflict protection
More to the implementation obviously. but this is pretty straight forward