Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.27 KB | None | 0 0
  1. #ifndef Ordered2DSet_CPP
  2. #define Ordered2DSet_CPP
  3.  
  4. #include "Ordered2DSet.h"
  5.  
  6. template <class key, class value>
  7. Btree<key,value>::Btree() {
  8. root =0;
  9. }
  10.  
  11. template <class key, class value>
  12. Btree <key,value>::Btree(const Btree& orig) {
  13. root = new Node<key,value>;
  14. if (orig.root!= 0)
  15. copy (orig.root, root);
  16. else
  17. root=0;
  18. }
  19.  
  20. template <class key, class value>
  21. Btree<key,value>::~Btree() {
  22. clear_all (root);
  23. root=0;
  24. }
  25.  
  26. template <class key, class value>
  27. void Btree<key,value>::copy (Node<key,value> *a, Node<key,value> *b){
  28. Node<key,value> *p[2*n+1];
  29.  
  30. if (a !=0){
  31. b->RAmount = a->RAmount;
  32. for (int i=0; i < a->RAmount; i++){
  33. b->klyuch[i].k = a->klyuch[i].k;
  34. }
  35. if ((a->RAmount >= 1) && (a->children!=0)){
  36. p[0] = new Node<key,value>;
  37. p[1] = new Node<key,value>;
  38. }
  39. else{
  40. p[0]=0;
  41. p[1]=0;
  42. }
  43. for(int i=1;i<2*n;i++){
  44. if ((a->RAmount >=i+1) && (a->children!=0))
  45. p[i+1]=new Node<key,value>;
  46. else p[i+1]=0;
  47. }
  48. b->children = p[0];
  49. for(int i=1;i<2*n+1;i++){
  50. b->klyuch[i].p = p[i];
  51. }
  52. if (a->RAmount >= 1)
  53. {
  54. copy (a->children, p[0]);
  55. copy (a->klyuch[0].p, p[1]);
  56. }
  57. for(int i=1;i<2*n;i++){
  58. if (a->RAmount >=i+1) copy (a->klyuch[i].p, p[i+1]);
  59. }
  60. }
  61. }
  62.  
  63. template <class key, class value>
  64. void Btree<key,value>::Add (const key k,const value v){
  65. bool grow;
  66. Tochka<key,value> u;
  67. Node<key,value> *bufer_root;
  68. search_add_key (k,v, root, grow, u);
  69. if (grow == true)
  70. {
  71. bufer_root = root;
  72. root = new Node<key,value>;
  73. root->children = 0;
  74. for (int i=0;i<2*n+1;i++){
  75. root->klyuch[i].p =0;
  76. }
  77. root->RAmount = 1;
  78. root->children = bufer_root;
  79. root->klyuch[0] = u;
  80.  
  81. }
  82. }
  83.  
  84. template <class key, class value>
  85. void Btree<key,value>::search_add_key (const key k,const value v, Node<key,value> *a, bool& grow, Tochka<key,value>& m) {
  86. int i, L, R;
  87. Node<key,value> *b;
  88. Tochka<key,value> u;
  89. if (a==0){
  90. grow = true;
  91. m.k=k; m.v=v;
  92. m.p =0;
  93. }
  94. else{
  95. L=0;
  96. R=a->RAmount;
  97. while (L<R){
  98. i = (L+R)/2;
  99. if (a->klyuch[i].k <=k)
  100. L = i+1;
  101. else
  102. R=i;
  103. }
  104. R = R-1;
  105. if ((R>=0) && (a->klyuch[R].k == k)){
  106. grow=false;}
  107. else{
  108. if (R==-1){
  109. search_add_key(k,v, a->children, grow, u);
  110. }
  111. else
  112. search_add_key(k,v, a->klyuch[R].p, grow, u);
  113. if (grow==true){
  114. if (a->RAmount<2*n){
  115. grow= false;
  116. a->RAmount++;
  117. for (i=a->RAmount-1;i>=R+2;i--)
  118. a->klyuch[i] =a->klyuch[i-1];
  119. a->klyuch[R+1]=u;
  120. }
  121. else{
  122. b=new Node<key,value>;
  123. b->children=0;
  124. for(int c=0;c<2*n+1;c++){
  125. b->klyuch[c].p=0;
  126. }
  127. if (R<=(n-1)){
  128. if (R==(n-1))
  129. m=u;
  130. else{
  131. m=a->klyuch[n-1];
  132. for (i=n-1; i>=R+n-1; i--)
  133. a->klyuch[i]=a->klyuch[i-1];
  134. a->klyuch[R+1]=u;
  135. }
  136. for (i=0; i<=n; i++)
  137. b->klyuch[i] = a->klyuch[i+n];
  138.  
  139. }
  140. else{
  141. R=R-n;
  142. m=a->klyuch[n];
  143. for (i=0; i<=R-1; i++)
  144. b->klyuch[i] = a->klyuch[i+n+1];
  145.                          b->klyuch[R]=u;
  146.                          for (i=R+1; i<=n-1; i++)
  147.                               b->klyuch[i] = a->klyuch[i+n];
  148.                      }
  149.                      a->RAmount = n;
  150.                      b->RAmount = n;
  151.                      //a->klyuch[b->RAmount-1]->  b->klyuch[b->RAmount-1]->parent=b;
  152.                      b->children = m.p;
  153.                      m.p=b;
  154. }
  155. }
  156. }
  157. }
  158. }
  159.  
  160. template <class key, class value>
  161. bool Btree<key,value>::Search (const key k){
  162. return (search_key (k, root));
  163. }
  164.  
  165. template <class key, class value>
  166. bool Btree<key,value>::search_key (const key k,const Node<key,value> *a)const{
  167. int i, L, R;
  168. if (a==0){
  169. return (false);
  170. }
  171. else{
  172. L = 0;
  173. R = a->RAmount;
  174. while (L < R){
  175. i = (L+R)/2;
  176. if (a->klyuch[i].k <= k)
  177. L = i+1;
  178. else
  179. R = i;
  180. }
  181. R = R-1;
  182. if ((R >= 0) && (a->klyuch[R].k==k))
  183. return (true);
  184. else
  185. if (R == -1)
  186. return search_key (k,a->children);
  187. else
  188. return search_key (k,a->klyuch[R].p);
  189. }
  190. }
  191.  
  192. template <class key, class value>
  193. value Btree<key,value>::Search_k (const key k) {
  194. return (search_k_key (k, root));
  195. }
  196.  
  197. template <class key, class value>
  198. value Btree<key,value>::search_k_key (const key k,const Node<key,value> *a)const{
  199. int i, L, R;
  200. L = 0;
  201. R = a->RAmount;
  202. while (L < R){
  203. i = (L+R)/2;
  204. if (a->klyuch[i].k <= k)
  205. L = i+1;
  206. else
  207. R = i;
  208. }
  209. R = R-1;
  210. if ((R >= 0) && (a->klyuch[R].k==k))
  211. return (a->klyuch[R].v);
  212. else
  213. if (R == -1)
  214. return search_k_key (k,a->children);
  215. else
  216. return search_k_key (k,a->klyuch[R].p);
  217.  
  218. }
  219.  
  220. template <class key, class value>
  221. void Btree<key,value>::Delete (const key k){
  222. bool smallsize;
  223. Node<key,value> *bufer_root;
  224. delete_key (k, root, smallsize);
  225. if (smallsize==true){
  226. if (root->RAmount == 0){
  227. bufer_root = root;
  228. root = bufer_root->children;
  229. bufer_root=0;
  230. }
  231. }
  232. }
  233.  
  234. template <class key, class value>
  235. void Btree<key,value>::delete_key (const key k, Node<key,value> *a, bool& smallsize){
  236. int i, L, R;
  237. Node<key,value> *q;
  238. if (a==0)
  239. smallsize=false;
  240. else{
  241. L=0;
  242. R=a->RAmount;
  243. while (L < R){
  244. i = (L+R)/2;
  245. if (a->klyuch[i].k< k)
  246. L = i+1;
  247. else R = i;
  248. }
  249. if (R == 0)
  250. q = a->children;
  251. else
  252. q = a->klyuch[R-1].p;
  253. if ((R <= a->RAmount-1) && (a->klyuch[R].k==k)){
  254. if (q==0){
  255. a->RAmount--;
  256. smallsize = (a->RAmount < n);
  257. for (i=R; i<a->RAmount; i++)
  258. a->klyuch[i] = a->klyuch[i+1];
  259. }
  260. else{
  261. del(R, a, q, smallsize);
  262. if (smallsize==true)
  263. fusion (a, q, R-1, smallsize);
  264. }
  265. }
  266. else{
  267. delete_key (k, q, smallsize);
  268. if (smallsize == true)
  269. fusion (a, q, R-1, smallsize);
  270. }
  271. }
  272. }
  273.  
  274. template <class key, class value>
  275. void Btree<key,value>::del (int R, Node<key,value> *a, Node<key,value> *q, bool& smallsize){
  276. Node<key,value> *b;
  277. b = q->klyuch[q->RAmount-1].p;
  278. if (b !=0){
  279. del (R, a, b, smallsize);
  280. if (smallsize==true)
  281. fusion (q, b, q->RAmount-1, smallsize);
  282. }
  283. else{q->klyuch[q->RAmount-1].p = a->klyuch[R].p;
  284. a->klyuch[R] = q->klyuch[q->RAmount-1];
  285. q->RAmount--;
  286. smallsize = (q->RAmount < n);
  287. }
  288. }
  289.  
  290. template <class key, class value>
  291. void Btree<key,value>::fusion (Node<key,value> *father, Node<key,value> *son, int R, bool& smallsize)
  292. {
  293. Node<key,value> *b;
  294. int i, k, RAb, RAfather;
  295. RAfather = father->RAmount;
  296. if (R < RAfather-1){
  297. R++;
  298. b = father->klyuch[R].p;
  299. RAb = b->RAmount;
  300. k = (RAb-n+1)/2;
  301. son->klyuch[n-1] = father->klyuch[R];
  302. son->klyuch[n-1].p = b->children;
  303. if (k > 0){
  304. for (i=0; i <= k-1; i++)
  305. son->klyuch[i+n] = b->klyuch[i];
  306. father->klyuch[R] = b->klyuch[k];
  307. father->klyuch[R].p = b;
  308. b->children = b->klyuch[k].p;
  309. RAb = RAb-k-1;
  310. for (i=0; i < RAb; i++)
  311. b->klyuch[i] = b->klyuch[i+k+1];
  312. b->RAmount = RAb;
  313. son->RAmount = n-1+(k+1);
  314. smallsize = false;
  315. }
  316. else{
  317. for (i=0; i < n; i++)
  318. son->klyuch[i+n] = b->klyuch[i];
  319. for (i=R; i < RAfather-1; i++)
  320. father->klyuch[i] = father->klyuch[i+1];
  321. son->RAmount = 2*n;
  322. father->RAmount--;
  323. smallsize = (father->RAmount < n);
  324. b=0;
  325. }
  326. }
  327. else{
  328. if (R == 0)
  329. b = father->children;
  330. else
  331. b = father->klyuch[R-1].p;
  332. RAb = b->RAmount+1;
  333. k = (RAb-n)/2;
  334. if (k > 0){
  335. son->klyuch[k] = son->klyuch[0];
  336. son->klyuch[k-1] = father->klyuch[R];
  337. son->klyuch[k-1].p = son->children;
  338. RAb = RAb-k-1;
  339. son->children = b->klyuch[RAb].p;
  340. father->klyuch[R] = b->klyuch[RAb];
  341. father->klyuch[R].p = son;
  342. b->RAmount--;
  343. son->RAmount = n-1+k;
  344. smallsize = false;
  345. }
  346. else{
  347. b->klyuch[RAb-1] = father->klyuch[R];
  348. b->klyuch[RAb-1].p = son->children;
  349. b->klyuch[RAb] = son->klyuch[0];
  350. b->RAmount = 2*n;
  351. father->RAmount--;
  352. smallsize = (father->RAmount < n);
  353. son=0;
  354. }
  355. }
  356. }
  357.  
  358.  
  359. template <class key, class value>
  360. int Btree<key,value>::Amount_key() const{
  361. return amount_allkey(root, 0);
  362. }
  363.  
  364. template <class key, class value>
  365. int Btree<key,value>::amount_allkey(Node<key,value> *a,int b)const{
  366. int i;
  367. if (a!=0){
  368. for (i=0; i<a->RAmount; i++)
  369. b++;
  370. b=b+amount_allkey(a->children,0);
  371. for (i=0; i<a->RAmount; i++)
  372. b=b+amount_allkey(a->klyuch[i].p,0);
  373. }
  374. return b;
  375. }
  376.  
  377. template <class key, class value>
  378. void Btree<key,value>::Clear (){
  379. clear_all (root);
  380. root=0;
  381.  
  382. }
  383.  
  384. template <class key, class value>
  385. void Btree<key,value>::clear_all (Node<key,value> *a){
  386. Node<key,value> *p[2*n+1];
  387. if (a!=0){
  388. if (a->RAmount >=1){
  389. p[0] = a->children;
  390. p[1] = a->klyuch[0].p;
  391. }
  392. else{
  393. p[0]=0;
  394. p[1]=0;
  395. }
  396. for(int i=1;i<2*n;i++){
  397. if (a->RAmount >= i+1)
  398. p[i+1]=a->klyuch[i].p;
  399. else
  400. p[i+1]=0;
  401. }
  402. a=0;
  403. for(int i=0;i<2*n+1;i++) clear_all(p[i]);
  404. }
  405. }
  406.  
  407.  
  408. template <class key, class value>
  409. Tochka<key, value> Btree<key,value>::getFirst()const{
  410. if (root!=0)
  411. return getFirst_key(root);
  412. }
  413. template <class key, class value>
  414. Tochka<key, value> Btree<key,value>::getFirst_key(const Node<key,value> *a)const{
  415. if ((a->children)!= NULL)
  416. return getFirst_key (a->children);
  417. else return a->klyuch[0];
  418. }
  419.  
  420. template <class key, class value>
  421. Tochka<key, value> Btree<key,value>::NextKey(const key k) const{
  422. //predmin=k;
  423. Tochka<key, value> *next=&this->getFirst();
  424. next_key(root,k,&next);
  425. return *next;
  426. }
  427. template <class key, class value>
  428. void Btree<key,value>::next_key(const Node<key,value> *a,const key k, Tochka<key, value> **b) const{
  429. int i;
  430. if (a!=0){
  431. for(i=0; i<a->RAmount;i++){
  432. if (a->klyuch[i].k>k){
  433. if ((*b)->k<=k) {
  434. **b=a->klyuch[i];
  435. if (i==0)  next_key(a->children,k,b);
  436. else next_key(a->klyuch[i-1].p,k,b);
  437. }
  438. if ((*b)->k>k){
  439. if (a->klyuch[i].k<(*b)->k){
  440. **b=a->klyuch[i];
  441. }
  442. if (i==0)  next_key(a->children,k,b);
  443. else next_key(a->klyuch[i-1].p,k,b);
  444.  
  445. }
  446. }
  447. }
  448. if (a->klyuch[a->RAmount-1].k<=k){
  449. next_key(a->klyuch[a->RAmount-1].p,k,b);
  450. }
  451.  
  452. /*L = 0;
  453. R = a->RAmount;
  454. while (L < R){
  455. i = (L+R)/2;
  456. if (a->klyuch[i].k <= k)
  457. L = i+1;
  458. else
  459. R = i;
  460. }
  461. //R = R-1;
  462. if ((R >=1) && (a->klyuch[R].k>k)){
  463. if (predmin>=k) {
  464. predmin=a->klyuch[R].k;
  465. next=a->klyuch[R];
  466. }
  467. next_key (a->klyuch[R].p,k);
  468. }
  469. else
  470. if (R ==0)
  471. next_key (a->children,k);
  472. else
  473. next_key (a->klyuch[R+1].p,k);*/
  474. }
  475. }
  476.  
  477. /*template <class key, class value>
  478. Tochka<key, value> Btree<key,value>::next_key(const Node<key,value> *a,const key k)const{
  479. }*/
  480.  
  481.  
  482.  
  483. template <class key, class value>
  484. void Btree<key,value>::Print (){
  485. print_b_Node (root, 0);
  486. }
  487.  
  488. template <class key, class value>
  489. void Btree<key,value>::print_b_Node (Node<key,value> *a, int level){
  490. int i;
  491. if (a != NULL){
  492. for (i=0; i<level; i++)
  493. cout << "---";
  494. for (i=0; i<a->RAmount; i++)
  495. {
  496. cout << a->klyuch[i].k;
  497. if (i != a->RAmount-1)
  498. cout << ", ";
  499. }
  500. cout << endl;
  501. print_b_Node (a->children, level+1);
  502. for (i=0; i<a->RAmount; i++)
  503. print_b_Node (a->klyuch[i].p, level+1);
  504. }
  505. }
  506. bool operator>(const Point &o1, const Point &o2)
  507. {if((o1.x>o2.x)||((o1.x==o2.x)&&(o1.y>o2.y))) return 1;
  508. else return 0;
  509. }
  510. bool operator<(const Point &o1, const Point &o2){
  511. if((o1.x<o2.x)||((o1.x==o2.x)&&(o1.y<o2.y))) return 1;
  512. else return 0;
  513. }
  514. bool operator==(const Point &o1, const Point &o2){
  515. if((o1.x==o2.x)&&(o1.y==o2.y)) return 1;
  516. else return 0;
  517. }
  518. bool operator>=(const Point &o1, const Point &o2){
  519. if((o1>o2)||(o1==o2)) return 1;
  520. else return 0;
  521. }
  522. bool operator<=(const Point &o1, const Point &o2){
  523. if((o2>o1)||(o1==o2)) return 1;
  524. else return 0;
  525. }
  526. ostream& operator<<(ostream& out, const Point& k){
  527. out<<"(";
  528. out<<k.x;
  529. out<<" , ";
  530. out<<k.y;
  531. out<<")";
  532. return out;
  533. }
  534. istream& operator>>(istream& in, Point& k){
  535. in>>k.x;
  536. in>>k.y;
  537. return in;
  538. };
  539. Ordered2DSet::Ordered2DSet(){
  540. ;
  541. }
  542. Ordered2DSet::Ordered2DSet(const Ordered2DSet& orig){
  543. k.Btree(orig.k);
  544. }
  545. Ordered2DSet::~Ordered2DSet()
  546. {k.~Btree();
  547. }
  548. void Ordered2DSet ::Delete (const Point l){
  549. k.Delete(l);
  550. }
  551. void Ordered2DSet ::Add (const Point l){
  552. k.Add(l,l);
  553. }
  554. bool Ordered2DSet ::Search (const Point l){
  555. k.Search(l);
  556. }
  557. int Ordered2DSet ::Amount_key()const{
  558. k.Amount_key();
  559. }
  560. void Ordered2DSet ::Clear(){
  561. k.Clear();
  562. }
  563. void Ordered2DSet::Print(){
  564. k.Print();
  565. }
  566. #endif  /* Ordered2DSet_CPP */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement