Created
July 6, 2009 22:01
-
-
Save adh/141721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dfsch_object_t* dfsch_sort_list(dfsch_object_t* list, | |
dfsch_object_t* comp){ | |
size_t k = 1; | |
dfsch_object_t* p = list; | |
size_t p_s; | |
dfsch_object_t* q; | |
size_t q_s; | |
dfsch_object_t* l; | |
dfsch_object_t* lt; | |
int nmerges; | |
size_t i; | |
dfsch_object_t* e; | |
list = dfsch_ensure_mutable_list(list); | |
l = list; | |
do { | |
nmerges = 0; | |
p = l; | |
l = NULL; | |
lt = NULL; | |
while (DFSCH_PAIR_P(p)){ | |
nmerges++; | |
q = p; | |
p_s = 0; | |
for (i = 0; DFSCH_PAIR_P(q) && i < k; i++){ | |
q = DFSCH_FAST_CDR(q); | |
p_s++; | |
} | |
q_s = k; | |
while (p_s > 0 || (q_s >0 && DFSCH_PAIR_P(q))){ | |
if (p_s == 0){ | |
q_s--; | |
e = q; | |
q = DFSCH_FAST_CDR(q); | |
} else if (q_s == 0 || !DFSCH_PAIR_P(q)){ | |
p_s--; | |
e = p; | |
p = DFSCH_FAST_CDR(p); | |
} else if (dfsch_apply(comp, dfsch_list(2, DFSCH_FAST_CAR(q), | |
DFSCH_FAST_CAR(p))) != NULL){ | |
q_s--; | |
e = q; | |
q = DFSCH_FAST_CDR(q); | |
} else { | |
p_s--; | |
e = p; | |
p = DFSCH_FAST_CDR(p); | |
} | |
DFSCH_FAST_CDR_MUT(e) = NULL; | |
if (l) { | |
DFSCH_FAST_CDR_MUT(lt) = e; | |
lt = e; | |
} else { | |
l = lt = e; | |
} | |
} | |
p = q; | |
} | |
k *= 2; | |
} while(nmerges > 1); | |
return l; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment