utility.cc

00001 /*
00002  * Copyright (c) 2007 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <string.h>
00031 #include "utility.h"
00032 
00033 DinArray::DinArray(int size) : obj_(0), count_(0)
00034 {
00035         if(size > 0)
00036         {
00037                 obj_ = new void*[size];
00038                 count_ = size;
00039         }
00040 }
00041 
00042 DinArray::~DinArray()
00043 {
00044         if(count_ > 0)
00045                 delete [] obj_;
00046 }
00047 
00048 int DinArray::count()
00049 {
00050         return count_;
00051 }
00052 
00053 void *DinArray::get(int i)
00054 {
00055         if(i < 0 || i >= count_)
00056                 return 0;
00057         return obj_[i];
00058 }
00059 
00060 void DinArray::set(int i, void *o)
00061 {
00062         if(i >= count_)
00063         {
00064                 void **tmp = new void*[i+1];
00065                 for(int j = 0; j < count_; j++)
00066                         tmp[j] = obj_[j];
00067                 if(count_ > 0)
00068                         delete [] obj_;
00069                 obj_ = tmp;
00070                 count_ = i + 1;
00071         }
00072         obj_[i] = o;
00073 
00074 }
00075 
00076 void DinArray::clear()
00077 {
00078         count_ = 0;
00079 }
00080 
00081 void DinArray::sort(Comparator *c)
00082 {
00083         if(c && count_ > 1)
00084                 mergeSort(0, count_ - 1, c);
00085 }
00086 
00087 void DinArray::mergeSort(int i, int f, Comparator *c)
00088 {
00089         if(f - i > 1)
00090         {
00091                 int m = (i + f) / 2;
00092                 mergeSort(i, m, c);
00093                 mergeSort(m+1, f, c);
00094                 DinArray tmp(i - f + 1);
00095                 int cur1 = i;
00096                 int cur2 = m + 1;
00097                 int cur = 0;
00098                 while(cur1 <= m || cur2 <= f)
00099                 {
00100                         if(cur1 > m)
00101                         {
00102                                 tmp.set(cur, obj_[cur2]);
00103                                 cur2++;
00104                         }
00105                         else if(cur2 > m)
00106                         {
00107                                 tmp.set(cur, obj_[cur1]);
00108                                 cur1++;
00109                         }
00110                         else if(c->isLess(obj_[cur1], obj_[cur2]))
00111                         {
00112                                 tmp.set(cur, obj_[cur1]);
00113                                 cur1++;
00114                         }
00115                         else
00116                         {
00117                                 tmp.set(cur, obj_[cur2]);
00118                                 cur2++;
00119                         }
00120                         cur++;
00121                 }
00122                 for(int j = 0; j < tmp.count(); j++)
00123                         obj_[i + j] = tmp.get(j);
00124         }
00125         else if(f - i == 1 && c->isLess(obj_[f], obj_[i]))
00126         {
00127                 void *tmp = obj_[i];
00128                 obj_[i] = obj_[f];
00129                 obj_[f] = tmp;
00130         }
00131 }
00132 
00133 IntegerDinArray::IntegerDinArray(int size) : DinArray(size)
00134 {
00135 }
00136 
00137 IntegerDinArray::~IntegerDinArray()
00138 {
00139 }
00140 
00141 int IntegerDinArray::get(int i)
00142 {
00143         return *((int *)(DinArray::get(i)));
00144 }
00145 
00146 void IntegerDinArray::set(int i, int value)
00147 {
00148         int *v = new int(value);
00149         DinArray::set(i,(void *)v);
00150 }
00151 
00152 class IntegerAscComp : public Comparator
00153 {
00154         virtual int isLess(void *a, void *b)
00155         {
00156                 return *((int *)a) < *((int *)b);
00157         }
00158 };
00159 
00160 class IntegerDescComp : public Comparator
00161 {
00162         virtual int isLess(void *a, void *b)
00163         {
00164                 return *((int *)a) > *((int *)b);
00165         }
00166 };
00167 
00168 void IntegerDinArray::sortAscending()
00169 {
00170         IntegerAscComp c;
00171         DinArray::sort(&c);
00172 }
00173 
00174 void IntegerDinArray::sortDescending()
00175 {
00176         IntegerDescComp c;
00177         DinArray::sort(&c);
00178 }
00179 
00180 void *realloc(void *array, int olddim, int newdim)
00181 {
00182         char *tmp = new char[newdim];
00183         if (olddim>0)
00184         {
00185                 memcpy(tmp, array, olddim);
00186                 delete [] array;
00187         }
00188         return tmp;
00189 }
00190 

Generated on Wed Nov 26 15:47:29 2008 for NS-MIRACLE library by  doxygen 1.5.2