Skip to content

Commit

Permalink
Added search
Browse files Browse the repository at this point in the history
  • Loading branch information
Daulet committed Sep 12, 2021
1 parent 772d223 commit 7a8c323
Showing 1 changed file with 77 additions and 128 deletions.
205 changes: 77 additions & 128 deletions Links/linkstorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "linkdata.hpp"
#include "linkindex.hpp"
#include "linksmemory.hpp"
#include <stack>
#include <iostream>
#include <vector>

template <typename T>
class LinkStorage
{
Expand All @@ -14,15 +14,10 @@ class LinkStorage
MemoryMappedFileArray<LinkIndex<T>> index_array;
T free_links;
T last_free_link;
void LeftRotateInSourceTree(T rnode, T tnode);
void RightRotateInSourceTree(T rnode, T tnode);
void LeftRotateInTargetTree(T rnode, T tnode);
void RightRotateInTargetTree(T rnode, T tnode);
void MaintainInSourceTree(T rnode, T node, bool flag);
void MaintainInTargetTree(T rnode, T node, bool flag);
void InsertLinkToSourceTree(T link);
void InsertLinkToTargetTree(T link);
T SearchLinkInSourceTree(T source, T target);
T SearchLinkInTargetTree(T source, T target);
public:
LinkStorage(const char* data_file, const char* index_file);
~LinkStorage();
Expand Down Expand Up @@ -90,7 +85,7 @@ T LinkStorage<T>::CreateLink(T source, T target) {

//Insert to tree
this->InsertLinkToSourceTree(link);

this->InsertLinkToTargetTree(link);
return link;
}

Expand All @@ -116,7 +111,7 @@ T LinkStorage<T>::CreateDot() {

//Insert to tree
this->InsertLinkToSourceTree(link);

this->InsertLinkToTargetTree(link);
return link;
}

Expand Down Expand Up @@ -151,155 +146,80 @@ void LinkStorage<T>::InsertLinkToSourceTree(T link) {
T source = link_data.source;
T target = link_data.target;
T root = this->index_array[source].root_as_source;
T root_left = this->index_array[root].left_as_source;
T root_right = this->index_array[root].right_as_source;
if(!root) {
this->index_array[source].root_as_source = link;
this->index_array[source].size_as_source = 1;
return;
}
T current_link = root;
std::vector<T> nodes;
while(true) {
if(this->data_array[current_link].target > target) {
if(this->index_array[current_link].left_as_source != 0) {
this->index_array[current_link].size_as_source++;
nodes.push_back(current_link);
current_link = this->index_array[current_link].left_as_source;
continue;
}
else {
this->index_array[current_link].size_as_source++;
nodes.push_back(current_link);
this->index_array[current_link].left_as_source = link;
break;
}
}
else {
if(this->index_array[current_link].right_as_source != 0) {
this->index_array[current_link].size_as_source++;
nodes.push_back(current_link);
current_link = this->index_array[current_link].right_as_source;
continue;
}
else {
this->index_array[current_link].size_as_source++;
nodes.push_back(current_link);
this->index_array[current_link].right_as_source = link;
break;
}
}
}
while(nodes.size() > 2) {
size_t pos = nodes.size() - 1;
T lnode = nodes[pos];
nodes.pop_back();
T hnode = nodes[pos];
T rnode = nodes[pos - 1];
this->MaintainInSourceTree(rnode, hnode, this->data_array[lnode].target > this->data_array[hnode].target);
}
if(nodes.size() == 2) {
T lnode = nodes.back();
nodes.pop_back();
T hnode = nodes.back();
nodes.pop_back();
this->MaintainInSourceTree(0, hnode, this->data_array[lnode].target > this->data_array[hnode].target);
T root_left = this->index_array[root].left_as_source;
T root_right = this->index_array[root].right_as_source;
if(this->index_array[root_left].right_as_source == root) {
this->index_array[source].root_as_source = root_left;
}
else if(this->index_array[root_right].left_as_source == root){
this->index_array[source].root_as_source = root_right;
}
}
}

template <typename T>
void LinkStorage<T>::LeftRotateInSourceTree(T rnode, T tnode) {

T knode = this->index_array[tnode].right_as_source;
this->index_array[tnode].right_as_source = this->index_array[knode].left_as_source;
this->index_array[knode].left_as_source = tnode;
this->index_array[knode].size_as_source = this->index_array[tnode].size_as_source;
T tnode_left = this->index_array[tnode].left_as_source;
T tnode_right = this->index_array[tnode].right_as_source;
T tnode_left_size = this->index_array[tnode_left].size_as_source;
T tnode_right_size = this->index_array[tnode_right].size_as_source;
this->index_array[tnode].size_as_source = tnode_left_size + tnode_right_size + 1;

if(!rnode) {
void LinkStorage<T>::InsertLinkToTargetTree(T link) {
LinkData<T> link_data = this->data_array[link];
T source = link_data.source;
T target = link_data.target;
T root = this->index_array[target].root_as_target;
T root_left = this->index_array[root].left_as_target;
T root_right = this->index_array[root].right_as_target;
if(!root) {
this->index_array[source].root_as_target = link;
return;
}

if(this->index_array[rnode].right_as_source == tnode) {
this->index_array[rnode].right_as_source = knode;
}
else {
this->index_array[rnode].left_as_source = knode;
}

}

template <typename T>
void LinkStorage<T>::RightRotateInSourceTree(T rnode, T tnode) {

T knode = this->index_array[tnode].left_as_source;
this->index_array[tnode].left_as_source = this->index_array[knode].right_as_source;
this->index_array[knode].right_as_source = tnode;
this->index_array[knode].size_as_source = this->index_array[tnode].size_as_source;
T tnode_left = this->index_array[tnode].left_as_source;
T tnode_right = this->index_array[tnode].right_as_source;
T tnode_left_size = this->index_array[tnode_left].size_as_source;
T tnode_right_size = this->index_array[tnode_right].size_as_source;
this->index_array[tnode].size_as_source = tnode_left_size + tnode_right_size + 1;

if(!rnode) {
return;
T current_link = root;
while(true) {
if(this->data_array[current_link].source > source) {
if(this->index_array[current_link].left_as_target != 0) {
this->index_array[current_link].size_as_target++;
current_link = this->index_array[current_link].left_as_target;
continue;
}
else {
this->index_array[current_link].size_as_target++;
this->index_array[current_link].left_as_target = link;
break;
}
}
else {
if(this->index_array[current_link].right_as_target != 0) {
this->index_array[current_link].size_as_target++;
current_link = this->index_array[current_link].right_as_target;
continue;
}
else {
this->index_array[current_link].size_as_target++;
this->index_array[current_link].right_as_target = link;
break;
}
}
}

if(this->index_array[rnode].right_as_source == tnode) {
this->index_array[rnode].right_as_source = knode;
}
else {
this->index_array[rnode].left_as_source = knode;
}

}

template <typename T>
void LinkStorage<T>::MaintainInSourceTree(T rnode, T node, bool flag) {
T node_left = this->index_array[node].left_as_source;
T node_right = this->index_array[node].right_as_source;

if(flag) {
if(this->index_array[node_left].size_as_source < this->index_array[this->index_array[node_right].left_as_source].size_as_source) {
//case 1
this->RightRotateInSourceTree(node, this->index_array[node].right_as_source);
this->LeftRotateInSourceTree(rnode, node);
}
else if(this->index_array[node_left].size_as_source < this->index_array[this->index_array[node_right].right_as_source].size_as_source) {
//case 2
this->LeftRotateInSourceTree(rnode, node);
}
else {
return;
}
}
else {
if(this->index_array[node_right].size_as_source < this->index_array[this->index_array[node_left].right_as_source].size_as_source) {
//case 1'
this->LeftRotateInSourceTree(node, this->index_array[node].left_as_source);
this->RightRotateInSourceTree(rnode, node);
}
else if(this->index_array[node_right].size_as_source < this->index_array[this->index_array[node_left].left_as_source].size_as_source) {
this->RightRotateInSourceTree(rnode, node);
}
else {
return;
}
}
this->MaintainInSourceTree(node, this->index_array[node].left_as_source, false);
this->MaintainInSourceTree(node, this->index_array[node].right_as_source, true);
this->MaintainInSourceTree(rnode, node, true);
this->MaintainInSourceTree(rnode, node, false);
}

template <typename T>
Expand Down Expand Up @@ -330,17 +250,46 @@ T LinkStorage<T>::SearchLinkInSourceTree(T source, T target) {
}
}

/*
template <typename T>
void LinkStorage<T>::MaintainInTargetTree(T rnode, T node, bool flag) {
T LinkStorage<T>::SearchLinkInTargetTree(T source, T target) {
T current_link = this->index_array[target].root_as_source;
while(true) {
if(this->data_array[current_link].source > source) {
if(this->index_array[current_link].left_as_target != 0) {
current_link = this->index_array[current_link].left_as_target;
continue;
}
else {
return 0;
}
}
else if(this->data_array[current_link].source < source) {
if(this->index_array[current_link].right_as_target != 0) {
current_link = this->index_array[current_link].right_as_target;
continue;
}
else {
return 0;
}
}
else {
return current_link;
}
}
}
*/

template <typename T>
T LinkStorage<T>::SearchLink(T source, T target) {
T link = this->SearchLinkInSourceTree(source, target);
return link;
T root_as_source = this->index_array[source].root_as_source;
T size_as_source = this->index_array[root_as_source].size_as_source;
T root_as_target = this->index_array[target].root_as_target;
T size_as_target = this->index_array[root_as_target].size_as_target;
if(size_as_source <= size_as_target) {
return this->SearchLinkInSourceTree(source, target);
}
else {
return this->SearchLinkInTargetTree(source, target);
}
}

template <typename T>
Expand Down

0 comments on commit 7a8c323

Please sign in to comment.