update sort function

now support long long int\float\double\string !
This commit is contained in:
Valk Richard Li 2019-08-08 21:17:50 +08:00 committed by GitHub
parent 34d06d6ad1
commit 5b8e9a67e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 3 deletions

View File

@ -141,7 +141,7 @@ var nasal_list::pop()
delete temp;
return temp_var;
}
nasal_list nasal_list::sort_list(const int sort_type,bool cmp_rule=true)
nasal_list nasal_list::sort_list(const int sort_type,const int _cmp=1)
{
nasal_list temp_list;
if(sort_type==SORT_INT)
@ -164,6 +164,85 @@ nasal_list nasal_list::sort_list(const int sort_type,bool cmp_rule=true)
return temp_list;
}
temp_list=*this;
nasal_list_unit *first_temp_this;
nasal_list_unit *second_temp_this;
nasal_list_unit *node_this;
first_temp_this=temp_list.head->next;
while(first_temp_this->next)
{
node_this=first_temp_this;
second_temp_this=first_temp_this->next;
while(second_temp_this->next)
{
if(_cmp>0 && *((int *)node_this->list_var.data)>*((int *)second_temp_this->list_var.data))//from small to large
node_this=second_temp_this;
else if(_cmp<=0 && *((int *)node_this->list_var.data)<*((int *)second_temp_this->list_var.data))//from large to small
node_this=second_temp_this;
second_temp_this=second_temp_this->next;
}
if(_cmp>0 && *((int *)node_this->list_var.data)>*((int *)second_temp_this->list_var.data))//from small to large func(a,b) a-b
node_this=second_temp_this;
else if(_cmp<=0 && *((int *)node_this->list_var.data)<*((int *)second_temp_this->list_var.data))//from large to small func(a,b) b-a
node_this=second_temp_this;
if(node_this!=first_temp_this)
{
int t;
t=*((int *)first_temp_this->list_var.data);
*((int *)first_temp_this->list_var.data)=*((int *)node_this->list_var.data);
*((int *)node_this->list_var.data)=t;
}
first_temp_this=first_temp_this->next;
}
}
else if(sort_type==SORT_DBL)
{
nasal_list_unit *temp=head;
while(temp->next)
{
temp=temp->next;
if(temp->list_var.type!=VAR_DOUBLE)
{
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be float."<<std::endl;
temp_list.setsize(1);
return temp_list;
}
}
if(temp->list_var.type!=VAR_DOUBLE)
{
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be float."<<std::endl;
temp_list.setsize(1);
return temp_list;
}
temp_list=*this;
nasal_list_unit *first_temp_this;
nasal_list_unit *second_temp_this;
nasal_list_unit *node_this;
first_temp_this=temp_list.head->next;
while(first_temp_this->next)
{
node_this=first_temp_this;
second_temp_this=first_temp_this->next;
while(second_temp_this->next)
{
if(_cmp>0 && *((double *)node_this->list_var.data)>*((double *)second_temp_this->list_var.data))//from small to large
node_this=second_temp_this;
else if(_cmp<=0 && *((double *)node_this->list_var.data)<*((double *)second_temp_this->list_var.data))//from large to small
node_this=second_temp_this;
second_temp_this=second_temp_this->next;
}
if(_cmp>0 && *((double *)node_this->list_var.data)>*((double *)second_temp_this->list_var.data))//from small to large func(a,b) a-b
node_this=second_temp_this;
else if(_cmp<=0 && *((double *)node_this->list_var.data)<*((double *)second_temp_this->list_var.data))//from large to small func(a,b) b-a
node_this=second_temp_this;
if(node_this!=first_temp_this)
{
double t;
t=*((double *)first_temp_this->list_var.data);
*((double *)first_temp_this->list_var.data)=*((double *)node_this->list_var.data);
*((double *)node_this->list_var.data)=t;
}
first_temp_this=first_temp_this->next;
}
}
else if(sort_type==SORT_STR)
{
@ -185,6 +264,35 @@ nasal_list nasal_list::sort_list(const int sort_type,bool cmp_rule=true)
return temp_list;
}
temp_list=*this;
nasal_list_unit *first_temp_this;
nasal_list_unit *second_temp_this;
nasal_list_unit *node_this;
first_temp_this=temp_list.head->next;
while(first_temp_this->next)
{
node_this=first_temp_this;
second_temp_this=first_temp_this->next;
while(second_temp_this->next)
{
if(_cmp>0 && *((std::string *)node_this->list_var.data)>*((std::string *)second_temp_this->list_var.data))//from small to large
node_this=second_temp_this;
else if(_cmp<=0 && *((std::string *)node_this->list_var.data)<*((std::string *)second_temp_this->list_var.data))//from large to small
node_this=second_temp_this;
second_temp_this=second_temp_this->next;
}
if(_cmp>0 && *((std::string *)node_this->list_var.data)>*((std::string *)second_temp_this->list_var.data))//from small to large func(a,b) cmp(a,b)
node_this=second_temp_this;
else if(_cmp<=0 && *((std::string *)node_this->list_var.data)<*((std::string *)second_temp_this->list_var.data))//from large to small func(a,b) cmp(b,a) or -cmp(a,b)
node_this=second_temp_this;
if(node_this!=first_temp_this)
{
std::string t;
t=*((std::string *)first_temp_this->list_var.data);
*((std::string *)first_temp_this->list_var.data)=*((std::string *)node_this->list_var.data);
*((std::string *)node_this->list_var.data)=t;
}
first_temp_this=first_temp_this->next;
}
}
return temp_list;
}

View File

@ -4,7 +4,8 @@
#include "nasal_var.h"
#define SORT_INT 1
#define SORT_STR 2
#define SORT_DBL 2
#define SORT_STR 3
class nasal_hash;
@ -26,7 +27,7 @@ class nasal_list
void setsize(const int);
nasal_list subvec(const int,const int);
var pop();
nasal_list sort_list(const int,bool);
nasal_list sort_list(const int,const int);
};
#endif