1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| void TableauDynamique_int_ctor0(struct tableauDynamique_int *pThis)
{
pThis->tab = NULL;
pThis->taille = 0;
pThis->capacite = 0;
}
int TableauDynamique_int_ctor1(struct tableauDynamique_int *pThis, size_t capaciteInitiale)
{
int ret = -1;
pThis->tab = malloc(capaciteInitiale * sizeof *pThis->tab);
if(pThis->tab != NULL)
{
pThis->capacite = capaciteInitiale;
ret = 0;
}
else
pThis->capacite = 0;
pThis->taille = 0;
return ret;
}
void TableauDynamique_int_dtor(struct tableauDynamique_int *pThis)
{
free(pThis->tab);
pThis->tab = NULL;
pThis->taille = 0;
pThis->capacite = 0;
}
void TableauDynamique_int_delete(struct tableauDynamique_int *pThis)
{
if(pThis != NULL)
{
TableauDynamique_int_dtor(pThis);
free(pThis);
}
}
struct tableauDynamique_int *TableauDynamique_int_new0(void)
{
struct tableauDynamique_int *pThis = malloc(sizeof *pThis);
if(pThis != NULL)
TableauDynamique_int_ctor0(pThis);
return pThis;
}
struct tableauDynamique_int *TableauDynamique_int_new1(size_t capaciteInitiale)
{
struct tableauDynamique_int *pThis = malloc(sizeof *pThis);
if(pThis != NULL)
{
int ctorRet = TableauDynamique_int_ctor1(pThis, capaciteInitiale);
if(ctorRet < 0)
{
TableauDynamique_int_delete(pThis);
pThis = NULL;
}
}
return pThis;
}
int realloc_int(int **pp, size_t cElemNewSize)
{
int ret = -1;
size_t cbNewSize = cElemNewSize * sizeof **pp;
int *tmp = realloc(*pp, cbNewSize);
if(tmp != NULL)
{
*pp = tmp;
ret = 0;
}
return ret;
}
int TableauDynamique_int_ajouter(struct tableauDynamique_int *pThis, int nouvelleValeur)
{
assert(pThis->taille <= pThis->capacite);
/* Agrandir le tableau si besoin est. */
if(pThis->taille == pThis->capacite)
{
size_t nouvelleCapacite = (pThis->capacite < 4 ? 4 : (size_t)(pThis->capacite*1.5));
if(realloc_int(&pThis->tab, nouvelleCapacite) >= 0)
{
pThis->capacite = nouvelleCapacite;
}
else
return -1;
}
/* ajouter */
pThis->tab[ pThis->taille++ ] = nouvelleValeur;
return 0;
} |
Partager