00001 00002 /* 00003 | Author: Michael Schmidt 00004 | Gunnar Jehl 00005 | 00006 | ************************* SOFTWARE LICENSE AGREEMENT *********************** 00007 | This source code is published under the BSD License. 00008 | 00009 | See file 'LICENSE.txt' that comes with this distribution or 00010 | http://dbis.informatik.uni-freiburg.de/index.php?project=GCX/license.php 00011 | for the full license agreement. 00012 | 00013 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00014 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00015 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00016 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00017 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00018 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00019 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00020 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00021 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00022 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00023 | POSSIBILITY OF SUCH DAMAGE. 00024 | **************************************************************************** 00025 */ 00026 00035 #include "intmultiset.h" 00036 #include "outputstream.h" 00037 00038 IntMultiSet::IntMultiSet() { 00039 max = RoleList::getInstance()->getMaxRoleId(); 00040 elems = new unsigned[max + 1]; 00041 00042 for (unsigned i = 0; (int) i <= max; i++) { 00043 elems[i] = 0; 00044 } 00045 } 00046 00047 IntMultiSet::IntMultiSet(vector < unsigned >*_elems) { 00048 max = RoleList::getInstance()->getMaxRoleId(); 00049 elems = new unsigned[max + 1]; 00050 00051 for (unsigned i = 0; (int) i <= max; i++) { 00052 elems[i] = 0; 00053 } 00054 for (unsigned i = 0; i < _elems->size(); i++) { 00055 elems[(*_elems)[i]]++; 00056 } 00057 } 00058 00059 IntMultiSet::~IntMultiSet() { 00060 delete[]elems; 00061 } 00062 00063 void IntMultiSet::addElems(IntMultiSet & set) { 00064 vector < unsigned >_elems = set.getElemVec(); 00065 00066 for (unsigned i = 0; i < _elems.size(); i++) { 00067 addElem(_elems[i]); 00068 } 00069 } 00070 00071 void IntMultiSet::addElems(vector < unsigned >*_elems) { 00072 for (unsigned i = 0; i < _elems->size(); i++) { 00073 addElem((*_elems)[i]); 00074 } 00075 } 00076 00077 void IntMultiSet::print(OutputStream & dos) { 00078 bool printed = false; 00079 00080 for (unsigned i = 0; (int) i <= max; i++) { 00081 if (elems[i] > 0) { 00082 if (printed) 00083 dos << ","; 00084 dos << elems[i] << "xr" << i; 00085 printed = true; 00086 } 00087 } 00088 } 00089 00090 vector < unsigned >IntMultiSet::getElemVec() { 00091 vector < unsigned >retvec; 00092 00093 for (unsigned i = 0; (int) i <= max; i++) { 00094 for (unsigned j = 0; j < elems[i]; j++) { 00095 retvec.push_back(i); 00096 } 00097 } 00098 00099 return retvec; 00100 } 00101 00102 bool IntMultiSet::isEmpty() { 00103 for (unsigned i = 0; (int) i <= max; i++) { 00104 if (elems[i]) 00105 return false; 00106 } 00107 00108 return true; 00109 } 00110 00111 bool IntMultiSet::removeElem(unsigned i) { 00112 if (elems[i] == 0) 00113 return false; 00114 00115 elems[i]--; 00116 return true; 00117 }