Skip to content

Instantly share code, notes, and snippets.

@nnorm
Created February 26, 2017 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnorm/eac114f40f02afc328b25faf63285325 to your computer and use it in GitHub Desktop.
Save nnorm/eac114f40f02afc328b25faf63285325 to your computer and use it in GitHub Desktop.
Dynamic generic container, working as a pile (add at the bottom, remove at the top).
#include "pioche.hpp"
#include <string.h>
#include <iostream>
//#define NDEBUG //uncomment before compiling so the assert macros are disabled for release version
#include <cassert>
void pioche_init(Pioche& pioche, int taille_elt) {
pioche.elementSize_ = taille_elt;
pioche.capacity_ = 1;
pioche.startMemPtr_ = (char*)malloc(pioche.elementSize_ * pioche.capacity_);
pioche.endMemPtr_ = pioche.startMemPtr_;
pioche.elementCount_ = 0;
}
//Pas demandé par le sujet, voir pioche.hpp
/*void pioche_print(const Pioche& pioche)
{
std::cout << "State = (count: " << pioche.elementCount_ << ", capacity: " << pioche.capacity_ << ")" << std::endl;
std::cout << std::endl;
}*/
void pioche_defausse(Pioche& pioche, const void* elt) {
assert(pioche.startMemPtr_);
assert(elt);
if(pioche.capacity_ <= pioche.elementCount_ + 1)
{
pioche.capacity_ *= 2;
int offset = pioche.elementCount_ * pioche.elementSize_;
assert(offset >= 0);
int sizeToRealloc = pioche.elementSize_ * pioche.capacity_;
assert(sizeToRealloc > 0);
//type safety, force bytewise arithmetics, force to be compiled as C++
pioche.startMemPtr_ = (char*)realloc(pioche.startMemPtr_, sizeToRealloc);
assert(pioche.startMemPtr_);
pioche.endMemPtr_ = pioche.startMemPtr_ + offset;
}
else
{ /* do nothing */}
memcpy(pioche.endMemPtr_, elt, pioche.elementSize_);
pioche.elementCount_++;
pioche.endMemPtr_ += pioche.elementSize_;
}
void pioche_pioche(Pioche& pioche, void* target) {
assert(pioche.startMemPtr_);
if(pioche.elementCount_ > 0)
{
memcpy(target, pioche.startMemPtr_, pioche.elementSize_);
pioche.elementCount_--;
memcpy(pioche.startMemPtr_, pioche.startMemPtr_ + pioche.elementSize_, pioche.elementSize_ * pioche.elementCount_);
pioche.endMemPtr_ -= pioche.elementSize_;
}
else
{ /* do nothing */ }
}
void swapElements(void* a, void* b)
{
void* tmp = a;
a = b;
b = tmp;
}
void pioche_melange(Pioche& pioche) {
assert(pioche.startMemPtr_);
//Source: https://fr.wikipedia.org/wiki/M%C3%A9lange_de_Fisher-Yates
for(unsigned int i = pioche.elementCount_ - 1; i >= 1; --i)
{
int j = rand() % i;
swapElements(pioche.startMemPtr_ + i * pioche.elementSize_,
pioche.startMemPtr_ + j * pioche.elementSize_);
}
}
void pioche_suppr(Pioche& pioche) {
if(pioche.startMemPtr_) // safe memory freeing
free(pioche.startMemPtr_);
else
{ /* do nothing, assert in debug */ assert(false); }
}
#ifndef ARAIL_PIOCHE_H
#define ARAIL_PIOCHE_H
#include <stdlib.h>
//structure de données
struct Pioche
{
char* startMemPtr_;
char* endMemPtr_;
size_t elementSize_;
unsigned int capacity_;
unsigned int elementCount_;
} ;
//initialisation de la pioche.
// - taille_elt est le nombre d'octets necessaires pour stocker une carte
void pioche_init(Pioche& pioche, int taille_elt) ;
//Pas demandé par le sujet, permet de connaitre l'etat actuel de la structure de données
//void pioche_print(const Pioche& pioche);
//pioche d'une carte
// - cible est l'adresse d'une zone memoire ou la carte piochee est inscrite
void pioche_pioche(Pioche& pioche, void* cible) ;
//defausse d'une carte
// - carte est l'adresse d'une zone memoire ou la carte defaussee est inscrite
void pioche_defausse(Pioche& pioche, const void* carte) ;
//melange de la pioche. Toutes les cartes (pioche et defausse) sont melangees
//pour former une nouvelle pioche.
//échanger deux pointeurs
void swapElements(void* a, void* b);
void pioche_melange(Pioche& pioche) ;
//liberation de la memoire occupee par la pioche. Apres cette fonction il est
//necessaire d'appeler a nouveau init pour que la pioche puisse etre utilisee.
void pioche_suppr(Pioche& pioche) ;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment