Biomechanical Joint Model
 Author: Anderson Maciel

PairData.cpp

Go to the documentation of this file.
00001 /************************************************************************\
00002 
00003   Copyright 1997 The University of North Carolina at Chapel Hill.
00004   All Rights Reserved.
00005 
00006   Permission to use, copy, modify and distribute this software
00007   and its documentation for educational, research and non-profit
00008   purposes, without fee, and without a written agreement is
00009   hereby granted, provided that the above copyright notice and
00010   the following three paragraphs appear in all copies.
00011 
00012   IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL
00013   HILL BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
00014   INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
00015   ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
00016   EVEN IF THE UNIVERSITY OF NORTH CAROLINA HAVE BEEN ADVISED OF
00017   THE POSSIBILITY OF SUCH DAMAGES.
00018 
00019 
00020   Permission to use, copy, modify and distribute this software
00021   and its documentation for educational, research and non-profit
00022   purposes, without fee, and without a written agreement is
00023   hereby granted, provided that the above copyright notice and
00024   the following three paragraphs appear in all copies.
00025 
00026   THE UNIVERSITY OF NORTH CAROLINA SPECIFICALLY DISCLAIM ANY
00027   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00028   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029   PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
00030   BASIS, AND THE UNIVERSITY OF NORTH CAROLINA HAS NO OBLIGATION
00031   TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
00032   MODIFICATIONS.
00033 
00034 
00035    --------------------------------- 
00036   |Please send all BUG REPORTS to:  |
00037   |                                 |
00038   |   geom@cs.unc.edu               |
00039   |                                 |
00040    ---------------------------------
00041   
00042      
00043   The authors may be contacted via:
00044 
00045   US Mail:  A. Pattekar/J. Cohen/T. Hudson/S. Gottschalk/M. Lin/D. Manocha
00046             Department of Computer Science
00047             Sitterson Hall, CB #3175
00048             University of N. Carolina
00049             Chapel Hill, NC 27599-3175
00050             
00051   Phone:    (919)962-1749
00052             
00053   EMail:    geom@cs.unc.edu
00054 
00055 \************************************************************************/
00056 
00057 
00058 /************************************************************************\
00059 Filename: PairData.C
00060 --
00061 Description: This file defines the member functions of the
00062              class PairData, declared in file PairData.H
00063 
00064 \************************************************************************/
00065 
00066 
00067 #ifdef _WIN32
00068 #include <iostream>
00069 #else
00070 #include #include <iostream.h>
00071 #endif
00072 
00073 #include <stdio.h>
00074 #include "PairData.H"
00075 
00076 
00077 const int PAIRDATA_DEFAULT_SIZE=10;
00078 
00079 
00080 PairData::PairData()
00081 {
00082   size = PAIRDATA_DEFAULT_SIZE;
00083   arr = new Elem*[PAIRDATA_DEFAULT_SIZE];
00084   int i;
00085   for (i=0;i<PAIRDATA_DEFAULT_SIZE;i++)
00086     arr[i] = NULL;
00087 }
00088 
00089 PairData::~PairData()
00090 {
00091   int i;
00092   for (i=0; i<size; i++)
00093     {
00094       Elem *current = arr[i];
00095       while (current != NULL)
00096         {
00097           Elem *temp = current;
00098           current = current->next;
00099           delete temp;
00100         }
00101     }
00102 }
00103 
00104 
00105 void PairData::AddPair(int id1, int id2) //add a pair to the set.
00106 {
00107   
00108   OrderIds(id1, id2);  //order the ids
00109   
00110   if (id1 >= size)     //increase the size of "arr", if necessary.
00111     {
00112       int newsize = (id1 >= 2*size) ? (id1+1) : 2*size;
00113       
00114       Elem **temp = new Elem*[newsize];
00115       int i;
00116       for (i=0; i<size; i++)
00117         temp[i] = arr[i];
00118       for (i=size; i<newsize; i++)
00119         temp[i] = NULL;
00120       delete [] arr;
00121       arr = temp;
00122       size = newsize;
00123     }
00124   
00125   Elem *current = arr[id1]; //select the right list from "arr".
00126   
00127   if (current == NULL)      //if the list is empty, insert the
00128     {                       //element in the front.
00129       current = new Elem;
00130       current->id = id2;
00131       current->next = NULL;
00132       arr[id1] = current;
00133     }
00134   else if (current->id > id2) //if the list is not empty but all
00135     {                         //elements are greater than id2, then
00136       current = new Elem;     //insert id2 in the front.
00137       current->id = id2;
00138       current->next = arr[id1];
00139       arr[id1] = current;
00140     }
00141   else
00142     {
00143       while (current->next != NULL)    //otherwise, find the correct location
00144         {                              //in the sorted list (ascending order) 
00145           if (current->next->id > id2) //and insert id2 there.
00146             break;
00147           current = current->next;
00148         }
00149       if (current->id == id2)
00150         {
00151           return;
00152         }
00153       else
00154         {
00155           Elem *temp = new Elem;
00156           temp->id = id2;
00157           temp->next = current->next;
00158           current->next = temp;
00159         }
00160     }
00161   
00162 }
00163   
00164 void PairData::DelPair(int id1, int id2) //delete a pair from the set.
00165 {
00166   OrderIds(id1, id2); //order the ids.
00167   
00168   if (id1 >= size)    //the pair doesnot exist in the set. So, do nothing
00169     return;           //but return.
00170   
00171   Elem *current = arr[id1]; //otherwise, select the correct list.
00172   
00173   if (current == NULL) //if this list is empty, the pair doesn't exist.
00174     {                  //so, return. 
00175       return;
00176     }
00177   else if (current->id == id2)   //otherwise, if id2 is the first element, 
00178     {                            //delete it.
00179       arr[id1] = current->next;
00180       delete current;
00181       return;
00182     }
00183   else
00184     {
00185       while (current->next != NULL)     //if id2 is not the first element,
00186         {                               //start traversing the sorted list.
00187           
00188           if (current->next->id > id2)  //if you have moved too far away
00189             {                           //without hitting id2, then the pair
00190               return;                   //pair doesn't exist. So, return.
00191             }
00192           else if (current->next->id == id2)  //otherwise, delete id2.
00193             {
00194               Elem *temp = current->next;
00195               current->next = current->next->next;
00196               delete temp;
00197               return;
00198             }
00199           current = current->next;
00200         }
00201     }
00202 }
00203 
00204 void PairData:: DelPairsInvolvingId(int id)  //delete all pairs containing id.
00205 {
00206   if (id < size)
00207     {
00208       Elem *temp = arr[id];
00209       while (temp != NULL)
00210         {
00211           Elem *t = temp;
00212           temp = temp->next;
00213           delete t;
00214         }
00215       arr[id] = NULL;
00216       
00217       int i;
00218       for (i=0; i<id; i++)
00219         DelPair(i, id);
00220     }
00221   else
00222     {
00223       int i;
00224       for (i=0;i<size; i++)
00225         DelPair(i, id);
00226     }
00227 }
00228 
00229 
00230 void PairData::Clear(void)     //delete all pairs from the set.
00231 {
00232   int i;
00233   for (i=0; i<size; i++)
00234     {
00235       while (arr[i] != NULL)
00236         {
00237           Elem *current = arr[i];
00238           arr[i] = current->next;
00239           delete current;
00240         }
00241     }
00242 };
00243 
00244 int PairData::ExistsPair(int id1, int id2)  //check if a pair exists in the
00245 {                                           //set.
00246   OrderIds(id1, id2);      //order the ids.
00247   
00248   if (id1 >=size)    //if id1 >= size, then the pair cannot exist.
00249     return 0;
00250   
00251   Elem *current = arr[id1];  //otherwise, find the correct list and traverse
00252   while (current != NULL)    //it, looking for id2.
00253     {
00254       if (current->id == id2)
00255         return 1;
00256       if (current->id > id2)
00257         return 0;
00258       
00259       current = current->next;
00260     }
00261   return 0;
00262 }

Generated on Thu Dec 1 10:13:39 2005 for COME - Biomechanical Joint Model by  doxygen 1.4.5