Please help me!! Newbie in C++

Reply

Join Date: Apr 2008
Posts: 3
Reputation: sommanatt is an unknown quantity at this point 
Solved Threads: 0
sommanatt sommanatt is offline Offline
Newbie Poster

Please help me!! Newbie in C++

 
0
  #1
Apr 15th, 2008
I have more fluently in writting Java but for this program my advisor need me to convert my program from java to c++. I've already done with c++ code and the result is correct and got the same result as java program but the big problem is that the program is very very slow than java. I think the the problem is due to the way of my programming(i'm familiar with java coding style). I've tried to change the code for few days but it has nothing better. Can anyone please help me to look these program and it would be very grateful if you can tell me what i've done wrong and how i can correct it.
My program is for creating suffix tree by reading the one line of text from the file.
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <sys/time.h>
  6. #include <unistd.h>
  7.  
  8. #define microsec 1000000.0
  9. using namespace std;
  10.  
  11. vector <int> startPos;
  12. vector <int> endPos;
  13.  
  14. class TimeTracker {
  15. private:
  16. struct timeval start_time;
  17. struct timeval stop_time;
  18. bool running;
  19. public:
  20. TimeTracker() {
  21. running=false;
  22. }
  23.  
  24. void Start() {
  25. gettimeofday(&start_time, (struct timezone *)0);
  26. running=true;
  27. }
  28.  
  29. double Stop() {
  30. double st, en;
  31. if (!running) return(-1.0);
  32. else {
  33. gettimeofday(&stop_time, (struct timezone *)0);
  34. st = start_time.tv_sec + (start_time.tv_usec/microsec);
  35. en = stop_time.tv_sec + (stop_time.tv_usec/microsec);
  36. running=false;
  37. return (double)((en - st));
  38. }
  39. }
  40. };
  41.  
  42. struct SuffNode // declare a new struct
  43. {
  44. string pre;
  45. vector <SuffNode> suff;
  46. vector <int> pos;
  47. };
  48.  
  49. string toString(string level, SuffNode* suffix) //return the output string
  50. {
  51. string tree = level + suffix->pre;
  52. tree += "\n";
  53. if(suffix->suff.size() != 0)
  54. {
  55. int c = suffix->suff.size();
  56. level += "-";
  57. for(int i = 0; i < c; i++)
  58. {
  59. SuffNode aSuff = suffix->suff.at(i);
  60. tree += toString(level, &aSuff);
  61. }
  62. }
  63. return tree;
  64. }
  65.  
  66. vector<SuffNode> put(string key, vector <SuffNode>& tree, int poss) //method for creating suffix tree by put each suffix one by one
  67. {
  68. if(tree.size() == 0) //put first suffix
  69. {
  70. SuffNode node;
  71. node.pre = key;
  72. node.pos.push_back(poss);
  73. tree.push_back( node);
  74. }
  75. else //put subsequent suffix
  76. {
  77. int tsize = tree.size(); //size of arraylist
  78. int i = 0;
  79. bool check = true;
  80.  
  81. while( i < tsize && check) //if it already has list keep checking the suffix with the string in each node
  82. {
  83. string test = tree[i].pre; //get string in that node
  84. vector <int> poslist;
  85. poslist.assign(tree[i].pos.begin(), tree[i].pos.end());
  86. int keysize = key.length(); //get size of input suffix
  87. int testsize = test.length(); //get size of string in the node
  88. int j = 1;
  89. bool c = true;
  90.  
  91. if(test[0] == key[0]) //if first character of suffix match with the first character of string in the node
  92. {
  93. while( j < keysize && j < testsize && c) //keep checking the matching
  94. {
  95. if(test[j] == key[j]) //if next character match
  96. {
  97. j++; //go to next character
  98. }
  99. else //out of while loop
  100. {
  101. c = false;
  102. }
  103. }//end inner while
  104. string newPre = test.substr(0, j); //get new string to put in current node
  105. tree[i].pre = newPre; //add new string
  106.  
  107. if(!c) //if match only some part of the string
  108. {
  109. string newS1 = test.substr(j);
  110. string newS2 = key.substr(j);
  111. if(tree[i].suff.size() == 0) //if there are no list in the node
  112. {
  113. SuffNode node1; //create new object(node)
  114. node1.pre = newS1; //add string into new node
  115. node1.pos.assign(poslist.begin(), poslist.end());
  116. tree[i].suff.push_back(node1); //add new node into the arraylist
  117. SuffNode node2; //create new object(node)
  118. node2.pre = newS2; //add string into new node
  119. node2.pos.push_back(poss); //add new position into position list in this node
  120. tree[i].suff.push_back(node2); //add new node into the arraylist
  121. }
  122. else //if it already has list
  123. {
  124. SuffNode node11; //create new object(node)
  125. node11.pre = newS1; //add string (new string of current node) into new node
  126. node11.suff.assign(tree[i].suff.begin(), tree[i].suff.end());
  127. node11.pos.assign(poslist.begin(), poslist.end());
  128. tree[i].suff.clear(); //clear list of current node
  129. tree[i].suff.push_back(node11); //add new list to current node
  130. SuffNode node12; //create new object(node)
  131. node12.pre = newS2; //add string (new string of input suffix) into new node
  132. node12.pos.push_back(poss); //add new position into the position list int this node
  133. tree[i].suff.push_back(node12); //add new node into the current list
  134. }
  135. } //end if !c
  136. else //if every character in the test stirng match
  137. {
  138. string newString = key.substr(j); //get new string of input suffix
  139.  
  140. if(tree[i].suff.size() == 0) //if no list in current node
  141. {
  142. SuffNode nnode; //create new node
  143. nnode.pre = newString; //add string into new node
  144. nnode.pos.push_back(poss); //add new position into the position list
  145. tree[i].suff.push_back(nnode); //input new node into current list
  146. }
  147. else //if it already has the list
  148. {
  149. vector <SuffNode> newL = put(newString, tree[i].suff, poss); //put new input suffix into the current tree(current list)
  150. tree[i].suff.assign(newL.begin(), newL.end());
  151. }
  152. }//end else !c
  153. tree[i].pos.push_back(poss); //add new position
  154. check = false;
  155. }//end if first character match
  156. else // if first character not match go to next index of arraylist
  157. {
  158. i++;
  159. }
  160. }//end outer while
  161.  
  162. if(i >= tsize) //no match in arraylist
  163. {
  164. SuffNode nNode; //create new node
  165. nNode.pre = key; //add input suffix into new node
  166. nNode.pos.push_back(poss); //add position into new node
  167. tree.push_back(nNode); //add new node into current list
  168. }
  169. }//end else
  170.  
  171. return tree;
  172. }
  173.  
  174.  
  175. bool matching(vector<SuffNode> tree, string s, int count) //method for find the match substring in arraylist
  176. {
  177. bool match = false;
  178. int sSize = s.length();
  179. int listSize = tree.size();
  180. int i = 0;
  181. bool check = true;
  182. while( i < listSize && check) //keep checking each string in each node until know whether match or not match
  183. {
  184. string test = tree[i].pre; //get string in that node
  185. int testsize = test.length();
  186. int j = 1;
  187. bool c = true;
  188. if(test[0] == s[0]) //if first character of string match with the first character of string in the node
  189. {
  190. if(count == 0)
  191. {
  192. startPos = tree[i].pos; //get position from the node that the first character matched
  193. }
  194. while( j < sSize && j < testsize && c) //keep checking the matching
  195. {
  196. if(test[j] == s[j]) //if next character match
  197. {
  198. j++; //go to next character
  199. }
  200. else //out of while loop
  201. {
  202. c = false;
  203. }
  204. }//end inner while
  205.  
  206. if(!c && j == sSize) //if all characters match
  207. {
  208. match = true;
  209. endPos = tree[i].pos;
  210. }
  211. else if(!c) //if either one characters not match
  212. {
  213. match = false;
  214. }
  215. else if (j == sSize) //if all character match
  216. {
  217. match = true;
  218. endPos = tree[i].pos; //get position from the node that the last character matched
  219. }
  220. else if (j < sSize) //if match the string in the node but still have character left in the input string
  221. {
  222. string newString = s.substr(j); //get the string have left
  223. count++;
  224. match = matching(tree[i].suff, newString, count); //continue finding the match
  225. }
  226. check = false;
  227. } //end if match
  228. else
  229. {
  230. i++;
  231. } //end if first character not match
  232. } //end while
  233.  
  234. if(i >= listSize) //no match in arraylist
  235. {
  236. match = false;
  237. }
  238. return match;
  239. }
  240.  
  241.  
  242. main()
  243. {
  244. string line, filename;
  245. putname:
  246. cout << "Please input file name: ";
  247. getline (cin, filename);
  248.  
  249. char *name = new char[filename.length()+1];
  250.  
  251. strcpy ( name, filename.c_str() ); // that is correct
  252.  
  253.  
  254. ifstream myfile (name);
  255. if (myfile.is_open())
  256. {
  257. getline (myfile,line);
  258. myfile.close();
  259. }
  260. else
  261. {
  262. cout << "Unable to open file\n";
  263. goto putname;
  264. }
  265.  
  266. line += "$";
  267. cout << line << endl;
  268. Tree t;
  269. TimeTracker tt;
  270. tt.Start();
  271. vector <SuffNode> suftree;
  272. for (int i = line.length(); i > 0; i--) //put each suffix of input string into class tree to create suffix tree
  273. {
  274. suftree = put(line.substr(i-1),suftree,(i-1));
  275. }
  276. cout << "Build Done! Time taken = " << tt.Stop() << " seconds.\n" << endl;
  277. string tree = "";
  278. for (int j = 0; j < suftree.size(); j++ ) //get created suffix tree and keep in form of string for presentation
  279. {
  280. tree += toString("-", &suftree[j]);
  281. }
  282. cout << tree;
  283. string ans;
  284. cout << "\nNeed to search (y/n) : ";
  285. getline(cin, ans);
  286. if ( ans == "y")
  287. {
  288. string search, files;
  289. putname2:
  290. cout << "Please input file name: ";
  291. getline (cin, files);
  292.  
  293. char *names = new char[files.length()+1];
  294. strcpy ( names, files.c_str() ); // that is correct
  295. ifstream myfiles (names);
  296. if (myfiles.is_open())
  297. {
  298. getline (myfiles,search);
  299. myfiles.close();
  300. TimeTracker ts;
  301. ts.Start();
  302. bool match = matching(suftree,search,0);
  303. cout << "Searching Done! Time taken = " << ts.Stop() << " seconds." << endl;
  304.  
  305. if(match) //if searched string match
  306. {
  307. cout << "\nMATCH!!!" << endl;
  308. vector<int> posi;
  309. int sSize = startPos.size();
  310. int eSize = endPos.size();
  311.  
  312. for(int k = 0; k < eSize; k++)
  313. {
  314. int epoint = endPos[k];
  315. for(int m = 0; m < sSize; m++)
  316. {
  317. int spoint = startPos[m];
  318. if(epoint == spoint)
  319. {
  320. posi.push_back(spoint);
  321. }
  322. }//end inner for loop
  323. }//end outer for loop
  324. int length = search.length() - 1;
  325. int numMatch = posi.size();
  326. cout << "Number of Matching : " << numMatch << "\n\n";
  327. for(int p = numMatch-1; p >= 0; p--)
  328. {
  329. int stat = posi[p];
  330. int en = stat + length;
  331. cout << "position : ( " << stat << "," << en << " )\n";
  332. }
  333.  
  334. }//end if match
  335. else
  336. {
  337. cout << "\nNOT MATCH!!!" << endl;
  338. }//end else (not match)
  339.  
  340. }
  341.  
  342. else
  343. {
  344. cout << "Unable to open file\n";
  345. goto putname2;
  346. }
  347.  
  348. }
  349. getchar();
  350.  
  351. }
Last edited by Ancient Dragon; Apr 15th, 2008 at 11:06 pm. Reason: replaced icode tags with code tags and add line numbers
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC