Java examples for Language Basics:Bit
a perhaps curious method: b is a reference set, perhaps all atoms in a certain molecule a is the working set, perhaps representing all displayed atoms For each set bit in b: a) if a is also set, then clear a's bit UNLESS b) if a is not set, then add to a all set bits of b Thus, if a equals b --> clear all if a is a subset of b, then --> b if b is a subset of a, then --> a not b if a only intersects with b, then --> a or b if a does not intersect with b, then a or b In "toggle" mode, when you click on any atom of the molecule, you want either: (a) all the atoms in the molecule to be displayed if not all are already displayed, or (b) the whole molecule to be hidden if all the atoms of the molecule are already displayed.
/* $RCSfile$// ww w .ja v a 2s . c o m * $Author: egonw $ * $Date: 2005-11-10 09:52:44 -0600 (Thu, 10 Nov 2005) $ * $Revision: 4255 $ * * Copyright (C) 2003-2005 The Jmol Development Team * * Contact: jmol-developers@lists.sf.net * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ //package com.java2s; import java.util.BitSet; public class Main { /** * a perhaps curious method: * * b is a reference set, perhaps all atoms in a certain molecule a is the * working set, perhaps representing all displayed atoms * * For each set bit in b: a) if a is also set, then clear a's bit UNLESS b) if * a is not set, then add to a all set bits of b * * Thus, if a equals b --> clear all if a is a subset of b, then --> b if b is * a subset of a, then --> a not b if a only intersects with b, then --> a or * b if a does not intersect with b, then a or b * * In "toggle" mode, when you click on any atom of the molecule, you want * either: * * (a) all the atoms in the molecule to be displayed if not all are already * displayed, or * * (b) the whole molecule to be hidden if all the atoms of the molecule are * already displayed. * * @param a * @param b * @return a handy pointer to the working set, a */ public static BitSet toggleInPlace(BitSet a, BitSet b) { if (a.equals(b)) { // all on -- toggle all off a.clear(); } else if (andNot(copy(b), a).length() == 0) { // b is a subset of a -> remove all b bits from a andNot(a, b); } else { // may or may not be some overlap // combine a and b a.or(b); } return a; } public static BitSet andNot(BitSet a, BitSet b) { if (b != null) a.andNot(b); return a; } public static BitSet copy(BitSet bs) { return bs == null ? null : (BitSet) bs.clone(); } public static BitSet copy(BitSet a, BitSet b) { if (a == null || b == null) return null; b.clear(); b.or(a); return b; } }