|
|
|
merge sorting including accent marked strings
Added on 4/29/2005
|
|
Compatibilities:
|
This item has not yet been rated
|
You can sort linear or proerty lists. If you use accentmarked or other non ASCII characters, you will need it
-- - mergeSort(ptr_list, int_startPos, int_endPos)
-- a=["d":["1","1","1","1"],"á":["2","2","2","2"],"a":["3","3","3","3"],"x":["4","4","4","4"]]
-- mergeSort(a, 1, a.count)
-- ["a": ["3", "3", "3", "3"], "á": ["2", "2", "2", "2"], "d": ["1", "1", "1", "1"], "x": ["4", "4", "4", "4"]]
-- - AddPropAt(propertyList, index, propertyName, propertyValue)
-- put AddPropAt(["d":["1","1","1","1"],"a":["2","2","2","2"],"f":["3","3","3","3"],"x":["4","4","4","4"]], 3, "roti", ["r":"we"])
-- ["d": ["1", "1", "1", "1"], "a": ["2", "2", "2", "2"], "roti": ["r": "we"], "f": ["3", "3", "3", "3"], "x": ["4", "4", "4", "4"]]
Download PC Source
--******************************************************
-- by Roti, roti@al.pmmf.hu
-- 2005.04.29.
-- collected/corrected/own functions for merge sorting including accent marked strings
--
-- You can sort linear or proerty lists. If you use accentmarked or other non ASCII characters, you will need it
-- - mergeSort(ptr_list, int_startPos, int_endPos)
-- a=["d":["1","1","1","1"],"á":["2","2","2","2"],"a":["3","3","3","3"],"x":["4","4","4","4"]]
-- mergeSort(a, 1, a.count)
-- ["a": ["3", "3", "3", "3"], "á": ["2", "2", "2", "2"], "d": ["1", "1", "1", "1"], "x": ["4", "4", "4", "4"]]
-- - AddPropAt(propertyList, index, propertyName, propertyValue)
-- put AddPropAt(["d":["1","1","1","1"],"a":["2","2","2","2"],"f":["3","3","3","3"],"x":["4","4","4","4"]], 3, "roti", ["r":"we"])
-- ["d": ["1", "1", "1", "1"], "a": ["2", "2", "2", "2"], "roti": ["r": "we"], "f": ["3", "3", "3", "3"], "x": ["4", "4", "4", "4"]]
--------------------------------------------------------
-- merge sort linear or property list
-- mergeSort (modified by Roti for property lists)
-- http://xtras.calu.us/articles1.php
on mergeSort(ptr_list, int_startPos, int_endPos)
if int_startPos < int_endPos then
_int_midpoint = ( int_startPos + int_endPos ) / 2
mergeSort(ptr_list, int_startPos, _int_midpoint)
mergeSort(ptr_list, _int_midpoint+1, int_endPos)
listMerge(ptr_list, int_startPos, _int_midpoint+1, int_endPos)
end if
end
on listMerge ptr_list, int_startPos, int_midpoint, int_endPos
_int_copiedLeft = 0
_int_copiedRight = 0
_int_copied = 0
_int_absLeftEndPos = int_midpoint - int_startPos
_int_absRightEndPos = int_endPos - int_midpoint + 1
if ptr_list.ilk=#proplist then
_list_tmp = [:]
else
_list_tmp = []
end if
repeat while ( _int_copiedLeft < _int_absLeftEndPos ) and ( _int_copiedRight < _int_absRightEndPos)
_int_copied = _int_copied + 1
_int_leftPos = int_startPos+_int_copiedLeft
_int_rightPos = int_midpoint+_int_copiedRight
if ptr_list.ilk=#proplist then
_int_leftValue = ptr_list.getpropat(_int_leftPos)
_int_rightValue = ptr_list.getpropat(_int_rightPos)
else
_int_leftValue = ptr_list[_int_leftPos]
_int_rightValue = ptr_list[_int_rightPos]
end if
-- compare lists to see if the left or right side is bigger
if my_str1grstr2(_int_rightValue,_int_leftValue) then
if ptr_list.ilk=#proplist then
-- _list_tmp[_int_copied] = ptr_list.getpropat(_int_leftPos)
_list_tmp.addprop(ptr_list.getpropat(_int_leftPos),ptr_list[_int_leftPos])
else
_list_tmp[_int_copied] = ptr_list[_int_leftPos]
end if
_int_copiedLeft = _int_copiedLeft + 1
else
if ptr_list.ilk=#proplist then
_list_tmp.addprop(ptr_list.getpropat(_int_rightPos),ptr_list[_int_rightPos])
else
_list_tmp[_int_copied] = ptr_list[_int_rightPos]
end if
_int_copiedRight = _int_copiedRight + 1
end if
end repeat
-- add everything remaining on the left list
repeat while ( _int_copiedLeft < _int_absLeftEndPos )
_int_copied = _int_copied + 1
if ptr_list.ilk=#proplist then
_list_tmp.addprop(ptr_list.getpropat(int_startPos+_int_copiedLeft),ptr_list[int_startPos+_int_copiedLeft])
else
_list_tmp[_int_copied] = ptr_list[int_startPos+_int_copiedLeft]
end if
_int_copiedLeft = _int_copiedLeft + 1
end repeat
-- add everything remaining on the right list
repeat while ( _int_copiedRight < _int_absRightEndPos )
_int_copied = _int_copied + 1
if ptr_list.ilk=#proplist then
_list_tmp.addprop(ptr_list.getpropat(int_midpoint+_int_copiedRight),ptr_list[int_midpoint+_int_copiedRight])
else
_list_tmp[_int_copied] = ptr_list[int_midpoint+_int_copiedRight]
end if
_int_copiedRight = _int_copiedRight + 1
end repeat
-- change it back in the pointer
_int_counter = 1
repeat with i = int_startPos to int_endPos
-- reattach pointer that points to the correct address from the tmp list
if ptr_list.ilk=#proplist then
addpropat(ptr_list,i, _list_tmp.getpropat(_int_counter) ,_list_tmp[_int_counter])
ptr_list.deleteat(i+1)
else
ptr_list[i] = _list_tmp[_int_counter]
end if
_int_counter = _int_counter + 1
end repeat
end
-- needed functions:
---------------------------------------------------------------
-- for character or integer compares
-- with integers, it corrects the accent marked compares
--put "á"<"a"
-- 0
--put "áa"<"aa"
-- 0
--put "áab"<"aaa"
-- 0
--put "áacv"<"aás"
-- 1 --BAD
--put "áac"<"aás"
-- 1 --BAD
--put "áac">"aás"
-- 0 --BAD
-- is str1 _greater_ than str2
-- return true or false
--put my_str1grstr2("áac","aás")
-- 1
on my_str1grstr2 str1,str2
-- threat integers
if str1.ilk=#integer and str2.ilk=#integer then
if str1>str2 then
return true
else
return false
end if
else
end if
-- go to the shorter length
h=[]
h.add(str1.length)
h.add(str2.length)
h=min(h)
-- delete the same characters
repeat while str1.char[1]=str2.char[1] and str1.char[1]<>"" and str2.char[1]<>""
delete char 1 of str1
delete char 1 of str2
end repeat
if str1="" and str2<>"" then
return false
end if
if str1<>"" and str2="" then
return true
end if
-- equal strings
if str1="" and str2="" then
return false
end if
-- the first character makes sense
if str1.char[1] return false
end if
return true
end
---------------------------------------------------------------
-- originally from:
-- addpropat: (slow on big lists)
-- http://listserv.uark.edu/scripts/wa.exe?A2=ind0403A&L=DIRECT-L&P=R12965&I=-3
on AddPropAt(propertyList, index, propertyName, propertyValue)
-- Adds a property named with a value
-- at position of . If is greater
-- than the number of elements in then the new
-- property is added at the end of the list.
--
-- An error is returned if is not a propList, or if
-- is negative.
--
-- The original list is modified, and a pointer to it is returned.
--------
if not(ilk(propertyList, #propList)) then
return #invalidPropList
else if index < 0 then
return #invalidIndex
end if
tempList = [:]
i = propertyList.count()
repeat while i >= index
tempList.addProp(propertyList.getPropAt(i), propertyList[i])
propertyList.deleteAt(i)
i = i - 1
end repeat
propertyList.addProp(propertyName, propertyValue)
i = tempList.count()
repeat while i
propertyList.addProp(tempList.getPropAt(i), tempList[i])
i = i - 1
end repeat
return propertyList -- Unnecessary but nice
end AddPropAt
|
|