Created
May 22, 2019 21:36
-
-
Save BruJu/bc48052eb673066b0720c4b4a54d1c67 to your computer and use it in GitHub Desktop.
PileDeBooleens.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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