TrajectoryList.cpp
Go to the documentation of this file.
00001 /***
00002  Basic structure for list of streamlines
00003 ***/
00004 #include "tensor_field_nav_core/TrajectoryList.h"
00005 
00006 // The similar list operations
00007 TrajectoryList::TrajectoryList(int initsize = 1000) //construction
00008 {
00009         trajs = (Trajectory **)malloc(sizeof(Trajectory *)*initsize);
00010         curMaxNumTrajs = initsize;
00011         ntrajs = 0;
00012 
00013         if(trajs == NULL)
00014         {
00015                 exit(-1);
00016         }
00017 
00018         for(int i = 0; i < initsize; i++)
00019                 trajs[i] = NULL;
00020         curMaxNumTrajs = initsize;
00021 } 
00022 
00023 TrajectoryList::~TrajectoryList()
00024 {
00025         int i, j;
00026 
00027         for(i = 0; i < curMaxNumTrajs; i++)
00028         {
00029                 if(trajs[i] != NULL)
00030                 {
00031                         free(trajs[i]->linesegs);
00032                 }
00033         }
00034 
00035         free(trajs);
00036 }
00037 
00038 //add a new vertex to the end of the list, if it succeeds, return true
00039  bool TrajectoryList::append(Trajectory *s)
00040 {
00041         if(isFull ())
00042                 if(!extend(100))
00043                         return false;             //if not enough memory available, return false
00044         trajs[ntrajs] = s;
00045         //copyElem(s, polist[nporbits]);
00046         s->index=ntrajs;
00047         ntrajs++;
00048         return true;
00049 } 
00050 
00051 bool TrajectoryList::del_End() //delete the vertex at the end of the list
00052 {
00053         if(isEmpty())  return false;
00054         ntrajs --;
00055         return true;
00056 } 
00057 
00058 void TrajectoryList::copy_Elem(Trajectory *s, Trajectory *d)
00059 {
00060 }
00061 
00062 //delete the corresponding  vertex, if it succeeds, return true
00063 bool TrajectoryList::del_Node(Trajectory *s) 
00064 {
00065         if(isEmpty())  return false;
00066 
00067         //find the vertex, if find it, delete and move the following vertices forward
00068         //otherwise, return false;
00069 
00070         int i, pos = -1;
00071 
00072         for(i = 0; i < ntrajs; i++)
00073         {
00074                 if(trajs[i] == s)
00075                 {
00076                         pos = i;
00077                         break;
00078                 }
00079         }
00080 
00081         if(pos == -1) return false;
00082 
00083         //delete it
00084         for(i = pos; i < ntrajs-1; i++)
00085         {
00086                 //we need a copy function
00087                 copy_Elem(trajs[i], trajs[i+1]);
00088         }
00089 
00090         ntrajs--;
00091 
00092         return true;
00093 } 
00094 
00095 bool TrajectoryList::isEmpty()  //judge whether the list is empty
00096 {
00097         if(ntrajs == 0)   return true;
00098         return false;
00099 }
00100 
00101 bool TrajectoryList::isFull()
00102 {
00103         if(ntrajs == curMaxNumTrajs) return true;
00104         return false;
00105 }
00106 
00107 //extend the original list, if it succeeds, return true
00108 bool TrajectoryList::extend(int step = 100)
00109 {
00110         Trajectory **temp = trajs;
00111         trajs = (Trajectory **) malloc(sizeof(Trajectory *) * (curMaxNumTrajs + step));
00112         if( trajs == NULL)
00113         {
00114                 curMaxNumTrajs = 0;
00115                 trajs = temp;
00116                 exit(-1);
00117 
00118                 return false;
00119         }
00120 
00121         int i;
00122 
00123         for(i = 0; i < curMaxNumTrajs; i++)
00124                 trajs[i] = temp[i];
00125         for(i = curMaxNumTrajs; i < curMaxNumTrajs+step; i++)
00126                 trajs[i] = NULL;
00127 
00128         curMaxNumTrajs += step;
00129 
00130         free(temp);
00131         return true;
00132 }
00133 void TrajectoryList::reset()
00134 {
00135         ntrajs = 0;
00136 }


tensor_field_nav_core
Author(s): Lintao Zheng, Kai Xu
autogenerated on Thu Jun 6 2019 19:50:56