Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PileDeBooleens.java
/*
* Copyright (C) 2018 by Julian Bruyat <bruyat dot ju at live dot fr>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
* granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* Une pile de booléens où il est rapide de savoir si tous les éléments de la
* pile sont vrais.
*
* @author Bruju
*/
public class PileDeBooleens {
private int pile = 0;
private int sommet = 1;
/**
* Empile le booléen donné
* @param booleen Le booléen à empiler
*/
public void empiler(boolean booleen) {
if (!booleen) {
pile = pile + sommet;
}
sommet = sommet * 2;
}
/**
* Permet de savoir si la pile est vide
* @return Vrai si la pile est vode
*/
public boolean estVide() {
return sommet == 1;
}
/**
* Dépile et renvoie l'élément au sommet
* @return Le booléen en sommet de pile
*/
public boolean depiler() {
sommet = sommet / 2;
if (pile >= sommet) {
pile -= sommet;
return false;
} else {
return true;
}
}
/**
* Permet de savoir si la pile ne contient que des éléments vrais
* @return Vrai si aucun élément de la pile est faux.
*/
public boolean toutAVrai() {
return pile == 0;
}
/**
* Inverse le sommet de la pile
*/
public void inverseSommet() {
empiler(!depiler());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment