source3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. #ifndef __source3_H__
  2. #define __source3_H__
  3. #include <iostream>
  4. #include <vector>
  5. #include <math.h>
  6. using namespace std;
  7. class Triehard // compressed decimal trie
  8. // constructor should make 1-10 base nodes that are empty for search to work
  9. // magnitude is 1 for length 1, so it must be >= 1
  10. // no more flag, instead we have a count field which counts the number of instances
  11. // the node represents. A small change, but much more functionality
  12. {
  13. private:
  14. class Trienode
  15. {
  16. private:
  17. int magnitude;
  18. int count;
  19. vector<Trienode *> children;
  20. public:
  21. Trienode(int magnitude, int count):
  22. magnitude{magnitude}, count{count}
  23. {
  24. children.reserve(10);
  25. for (int i = 0; i < 10; ++i)
  26. {
  27. children[i] = nullptr;
  28. }
  29. }
  30. ~Trienode()
  31. {
  32. for (int i = 0; i < 10; ++i)
  33. {
  34. delete children[i];
  35. }
  36. }
  37. int getMag()
  38. {
  39. return magnitude;
  40. }
  41. int getCount()
  42. {
  43. return count;
  44. }
  45. Trienode * getX(int x)
  46. {
  47. return children[x];
  48. }
  49. void addMag()
  50. {
  51. ++magnitude;
  52. }
  53. void subMag()
  54. {
  55. --magnitude;
  56. }
  57. void addCount()
  58. {
  59. ++count;
  60. }
  61. void subCount()
  62. {
  63. --count;
  64. }
  65. void zeroCount()
  66. {
  67. count = 0;
  68. }
  69. void setCount(int x)
  70. {
  71. count = x;
  72. }
  73. Trienode * setX(int x, int mag, int cnt)
  74. {
  75. children[x] = new Trienode(mag, cnt);
  76. return children[x];
  77. }
  78. void copyX(int x, Trienode * node)
  79. {
  80. children[x] = node;
  81. }
  82. bool isLeaf()
  83. {
  84. for (int i = 0; i < 10; ++i)
  85. {
  86. if (children[i]) return false;
  87. }
  88. return true;
  89. }
  90. // true if x is the only child node
  91. bool onlyKid(int x)
  92. {
  93. for (int i = 0; i < 10; ++i)
  94. {
  95. if (i == x)
  96. {
  97. if (!children[i]) return false;
  98. continue;
  99. }
  100. if (children[i]) return false;
  101. }
  102. return true;
  103. }
  104. void changeCustody(Trienode * parent)
  105. {
  106. for (int i = 0; i < 10; ++i)
  107. {
  108. parent->copyX(i, children[i]);
  109. }
  110. }
  111. int sumMag()
  112. {
  113. int result = magnitude;
  114. for (int i = 0; i < 10; ++i)
  115. {
  116. if (children[i]) result += children[i]->sumMag();
  117. }
  118. return result;
  119. }
  120. int sumCount()
  121. {
  122. int result = 1;
  123. for (int i = 0; i < 10; ++i)
  124. {
  125. if (children[i]) result += children[i]->sumCount();
  126. }
  127. return result;
  128. }
  129. };
  130. vector<Trienode *> nodes;
  131. public:
  132. Triehard() // Initializes all nodes as empty, but makes it searchable, mutatable
  133. {
  134. nodes.reserve(10);
  135. for (int i = 0; i < 10; ++i)
  136. {
  137. nodes[i] = new Trienode(0, 0);
  138. }
  139. }
  140. ~Triehard() // Same concern (syntax) as nodes, don't forget to write an erase method as well, maybe an empty/wipe
  141. {
  142. for (int i = 0; i < 10; ++i)
  143. {
  144. delete nodes[i];
  145. }
  146. }
  147. // build an array of what is "processed" so far. then when a flag is hit, print that array.
  148. void mainPrint(Trienode * curnode, vector<int> * chars, int val)
  149. {
  150. if (!curnode) return;
  151. int curmag = curnode->getMag();
  152. int curcount = curnode->getCount();
  153. while (curmag)
  154. {
  155. chars->push_back(val);
  156. --curmag;
  157. }
  158. while (curcount)
  159. {
  160. int len = chars->size();
  161. for (int i = 0; i < len; i++)
  162. {
  163. cout << (*chars)[i] << " ";
  164. }
  165. cout << endl;
  166. --curcount;
  167. }
  168. for (int i = 0; i < 10; ++i)
  169. {
  170. mainPrint(curnode->getX(i), chars, i);
  171. }
  172. curmag = curnode->getMag();
  173. while (curmag)
  174. {
  175. chars->pop_back();
  176. --curmag;
  177. }
  178. }
  179. void myPrintIsBetterThanYoursLogan()
  180. {
  181. for (int i = 0; i < 10; ++i)
  182. {
  183. vector<int> * chars = new vector<int>();
  184. mainPrint(nodes[i], chars, i);
  185. delete chars;
  186. }
  187. }
  188. int search(vector<int> * val) // val is the string
  189. {
  190. Trienode * curnode = nodes[(*val)[0]];
  191. int pos = (*val)[0]; // represents what value your current node is
  192. int curmag = curnode->getMag();
  193. for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy.
  194. {
  195. if ((*val)[i] == pos) // if we are on the correct node already
  196. {
  197. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  198. {
  199. --curmag;
  200. }
  201. else if (curnode->getX(pos)) // if our current node is exhausted, move on to the next one
  202. {
  203. curnode = curnode->getX(pos);
  204. curmag = curnode->getMag() - 1;
  205. }
  206. else
  207. {
  208. return 0;
  209. }
  210. }
  211. else // we are not on the right node
  212. {
  213. if (curmag) // if we have magnitude left
  214. {
  215. return 0;
  216. }
  217. else if (curnode->getX((*val)[i])) // if our child for that # exists
  218. {
  219. curnode = curnode->getX((*val)[i]);
  220. curmag = curnode->getMag() - 1;
  221. pos = (*val)[i];
  222. }
  223. else // we don't have the child, must be absent
  224. {
  225. return 0;
  226. }
  227. }
  228. }
  229. if (!curmag)
  230. {
  231. return curnode->getCount();
  232. }
  233. return 0;
  234. }
  235. void insert(vector<int> * val) // assumes valid input
  236. {
  237. Trienode * curnode = nodes[(*val)[0]];
  238. int pos = (*val)[0]; // represents what value your current node is
  239. int curmag = curnode->getMag(); // "remaining" magnitude of the current node
  240. for (int i = 0; i < val->size(); i++) // each iteration validates against curnode for position i
  241. {
  242. if ((*val)[i] == pos) // curnode matches current value
  243. {
  244. if (curmag) // curnode has magnitude left, just sub1 and continue
  245. {
  246. --curmag;
  247. }
  248. else if (curnode->getX(pos)) // curnode is exhausted, but we have the same child with mag >=1, so use that
  249. {
  250. curnode = curnode->getX(pos);
  251. curmag = curnode->getMag() - 1;
  252. }
  253. else if (!(curnode->getCount()) && curnode->isLeaf()) // we aren't flagged and are a leaf, so add mag
  254. {
  255. curnode->addMag();
  256. }
  257. else
  258. {
  259. curnode = curnode->setX(pos, 1, 0); // we must create a child with mag1, curmag is 0 so no change
  260. }
  261. }
  262. else // curnode is not the same digit as val[i]
  263. {
  264. if (curmag) // this means we are going to have to decompress
  265. {
  266. Trienode * newnode = new Trienode(0, curnode->getCount()); // this'll be the second half of curnode-
  267. curnode->zeroCount(); // newnode should be flagged (if curnode was), not curnode
  268. while (curmag) // put extra magnitude into newnode
  269. {
  270. curnode->subMag();
  271. --curmag;
  272. newnode->addMag();
  273. }
  274. for (int i = 0; i < 10; i++) // move children to newnode
  275. {
  276. newnode->copyX(i, curnode->getX(i));
  277. curnode->copyX(i, nullptr);
  278. }
  279. curnode->copyX(pos, newnode); // put newnode in its place
  280. curnode = curnode->setX((*val)[i], 1, 0); // insert new node for the string being inserted
  281. curmag = curnode->getMag() - 1; // reset curmag
  282. pos = (*val)[i]; // update pos
  283. }
  284. else if (curnode->getX((*val)[i])) // we have a child of the correct val
  285. {
  286. pos = (*val)[i];
  287. curnode = curnode->getX(pos);
  288. curmag = curnode->getMag() - 1;
  289. }
  290. else // insert a child, curmag is still 0
  291. {
  292. pos = (*val)[i];
  293. curnode = curnode->setX(pos, 1, 0);
  294. }
  295. }
  296. }
  297. // at this point, the node we are at needs to be flagged. However, there is an issue: this node may have magnitude remaining
  298. // if this is the case, we need to split it up at curnode->getMag() - curmag. lets check for the easy case, then proceed
  299. // with that logic if necessary
  300. // basically curmag is our "extra" magnitude that needs to be sent along
  301. if (!curmag)
  302. {
  303. curnode->addCount();
  304. }
  305. else
  306. {
  307. Trienode * newnode = new Trienode(0, curnode->getCount()); // this is our new node, which should retain old flagging
  308. curnode->setCount(1); // curnode will now end where we want to insert, so this should be true
  309. while (curmag) // fills newnode with the extra magnitude
  310. {
  311. curnode->subMag();
  312. --curmag;
  313. newnode->addMag();
  314. }
  315. for (int i = 0; i < 10; i++) // move children to newnode
  316. {
  317. newnode->copyX(i, curnode->getX(i));
  318. curnode->copyX(i, nullptr);
  319. }
  320. curnode->copyX(pos, newnode); // ensure newnode is actually linked to curnode
  321. }
  322. }
  323. void cut(vector<int> * val) // this is delete because i can't use delete :(
  324. {
  325. Trienode * curnode = nodes[(*val)[0]];
  326. Trienode * prevnode = nullptr;
  327. int pos = (*val)[0]; // represents the represented value of curnode (0-9)
  328. int pos2; // previous node's side
  329. int curmag = curnode->getMag();
  330. for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy.
  331. {
  332. if ((*val)[i] == pos) // curnode matches current value
  333. {
  334. if (curmag) // we have mag left
  335. {
  336. --curmag;
  337. pos2 = pos;
  338. }
  339. else if (curnode->getX(pos)) // if we have the correct child
  340. {
  341. prevnode = curnode;
  342. curnode = curnode->getX(pos);
  343. curmag = curnode->getMag() - 1;
  344. pos2 = pos;
  345. }
  346. else
  347. {
  348. return; // node does not exist, will make this an error later
  349. }
  350. }
  351. else // curnode does NOT match current value
  352. {
  353. if (curmag)
  354. {
  355. return; // should be error later, but the val isn't inserted, since there is mag left in the wrong number
  356. }
  357. else if (curnode->getX((*val)[i])) // if we have the correct child
  358. {
  359. pos2 = pos;
  360. pos = (*val)[i];
  361. prevnode = curnode;
  362. curnode = curnode->getX(pos);
  363. curmag = curnode->getMag() - 1;
  364. }
  365. else
  366. {
  367. return; // we don't have the right child, so return (to be error)
  368. }
  369. }
  370. }
  371. // at this point, we have curnode being the "end" of our value
  372. if (!(prevnode)) // if we are deleting one of the base trees
  373. {
  374. if (nodes[pos]->getCount()) nodes[pos]->subCount();
  375. else return; // later throw error for removing nothing
  376. }
  377. if (curnode->getCount()) curnode->subCount(); // Normally this is all that is necessary
  378. else return; // later throw error for removing nothing
  379. if (curnode->getCount()) return; // This means we aren't removing a node, so no compression is possible
  380. // Cases where nodes have to be removed/compressed
  381. // THIS NEEDS A LOT OF WORK!!!
  382. if (curnode->isLeaf()) // if our node has no children, destroy it and change parent's reference to NULL
  383. {
  384. delete curnode;
  385. prevnode->copyX(pos, nullptr);
  386. }
  387. else if (prevnode->onlyKid(pos) && pos == pos2) // we have kids (a given now), our parent has none else. compress
  388. {
  389. while (curnode->getMag()) // move mag to parent
  390. {
  391. curnode->subMag();
  392. prevnode->addMag();
  393. }
  394. curnode->changeCustody(prevnode); // move kids to parent
  395. delete curnode;
  396. // wait, can we have a parent and kid of the same # allowing us to compress into both, either, or just parent???
  397. // consider what the children of each have to be to create these conditions
  398. // eg what to do with 00 - 00 (now unflagged) -- 00 (has kids, or a flag)
  399. // no, the above is impossible. the first 00 must have another kid, or a flag or it wouldn't exist
  400. }
  401. else if (curnode->onlyKid(pos))
  402. // parent has other kids or a flag, cannot go into it (maybe like below?). if so, do those.if not,we gotta check if we can compress into our children (only if we have 1 kid, ofc)
  403. {
  404. Trienode * newnode = curnode->getX(pos);
  405. while (curnode->getMag())
  406. {
  407. curnode->subMag();
  408. newnode->addMag();
  409. }
  410. prevnode->copyX(pos, newnode);
  411. delete curnode;
  412. }
  413. }
  414. // update counter with children recursively
  415. void mainCount(Trienode * curnode, int len, int * counter)
  416. {
  417. if (!curnode) return;
  418. len += curnode->getMag();
  419. *counter += (len * curnode->getCount());
  420. for (int i = 0; i < 10; ++i)
  421. {
  422. mainCount(curnode->getX(i), len, counter);
  423. }
  424. }
  425. int countChars() // returns total word length of trie
  426. {
  427. int counter = 0;
  428. for (int i = 0; i < 10; ++i)
  429. {
  430. mainCount(nodes[i], 0, &counter);
  431. }
  432. return counter;
  433. }
  434. float compressionovertrie() // returns nodes / nodes in a normal trie
  435. {
  436. float total = 0;
  437. float compressed = -10;
  438. for (int i = 0; i < 10; ++i)
  439. {
  440. compressed += nodes[i]->sumCount();
  441. total += nodes[i]->sumMag();
  442. }
  443. return roundf(compressed/total * 100) / 100;
  444. }
  445. float compressionoverdict() // returns nodes / sum of all word length
  446. {
  447. float compressed = -10;
  448. for (int i = 0; i < 10; ++i)
  449. {
  450. compressed += nodes[i]->sumCount();
  451. }
  452. float total = countChars();
  453. return roundf(compressed/total * 100) / 100;
  454. }
  455. };
  456. #endif