Skip to content

Commit

Permalink
Merge pull request #1562 from cacaodev/cappuccino
Browse files Browse the repository at this point in the history
---

for random numeric array and text array.
  • Loading branch information
aljungberg committed Jun 9, 2012
2 parents 0ed3983 + 0483f9d commit bb8a7ba
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 31 deletions.
2 changes: 1 addition & 1 deletion AppKit/CPTableView.j
Expand Up @@ -1257,7 +1257,7 @@ NOT YET IMPLEMENTED
*/
- (int)selectedColumn
{
[_selectedColumnIndexes lastIndex];
return [_selectedColumnIndexes lastIndex];
}

/*!
Expand Down
178 changes: 148 additions & 30 deletions Tests/Foundation/CPArrayPerformanceTest.j
@@ -1,72 +1,111 @@
var FILE = require("file");

@import <Foundation/CPArray.j>
@import <Foundation/CPString.j>
@import <Foundation/CPNumber.j>
@import <Foundation/CPSortDescriptor.j>


var ELEMENTS = 100,
REPEATS = 10;

@implementation CPArrayPerformanceTest : OJTestCase

- (void)testSortUsingDescriptorsSpeed
{
var array = [self makeUnsorted];
CPArray descriptors;
CPString the_big_sort;
}

- (void)setUp
{
var descriptors = [
[CPSortDescriptor sortDescriptorWithKey:"a" ascending:NO],
[CPSortDescriptor sortDescriptorWithKey:"b" ascending:YES]
];

var start = (new Date).getTime();
the_big_sort = FILE.read(FILE.join(FILE.dirname(module.path), "the_big_sort.txt"), { charset:"UTF-8" });
}

for (var i = 0; i < REPEATS; ++i)
{
var sorted = [array sortedArrayUsingDescriptors:descriptors];
- (void)testAlmostSortedNumericUsingMergeSort
{
CPLog.warn("\nNUMERIC ALMOST SORTED");
var a = [self makeUnsorted];
var sorted = [self sortUsingMergeSort:a];
[self checkAlmostSorted:sorted];
}

[self checkSorted:sorted];
}
- (void)testAlmostSortedNumericUsingNativeSort
{
var a = [self makeUnsorted];
var sorted = [self sortUsingNativeSort:a];
[self checkAlmostSorted:sorted];
}

var end = (new Date).getTime();
- (void)testRandomNumericUsingMergeSort
{
CPLog.warn("\nNUMERIC RANDOM");
var a = [self makeRandomNumeric];
var sorted = [self sortUsingMergeSort:a];
[self checkRandomSorted:sorted];
}

CPLog.warn(_cmd + ": " + (end - start) + "ms");
- (void)testRandomNumericUsingNativeSort
{
var a = [self makeRandomNumeric];
var sorted = [self sortUsingNativeSort:a];
[self checkRandomSorted:sorted];
}

- (void)testSortUsingNativeSort
- (void)testRandomTextUsingMergeSort
{
var array = [self makeUnsorted];
CPLog.warn("\nTEXT RANDOM");
var a = [self makeRandomText];
var sorted = [self sortUsingMergeSort:a];
[self checkRandomSorted:sorted];
}

var descriptors = [
[CPSortDescriptor sortDescriptorWithKey:"a" ascending:NO],
[CPSortDescriptor sortDescriptorWithKey:"b" ascending:YES]
];
- (void)testRandomTextUsingNativeSort
{
var a = [self makeRandomText];
var sorted = [self sortUsingNativeSort:a];
[self checkRandomSorted:sorted];
}

function sortFunction(lhs, rhs)
{
return ([lhs a] === [rhs a]) ? ([lhs b] - [rhs b]) : ([rhs a] - [lhs a]);
}
- (CPArray)sortUsingMergeSort:(CPArray)anArray
{
var sorted;

var start = (new Date).getTime();

for (var i = 0; i < REPEATS; ++i)
{
var sorted = [array copy];
sorted = [anArray sortedArrayUsingDescriptors:descriptors];

sorted.sort(sortFunction)
var end = (new Date).getTime();

[self checkSorted:sorted];
}
CPLog.warn(_cmd + ": " + (end - start) + "ms");

return sorted;
}

- (void)sortUsingNativeSort:(CPArray)anArray
{
var sorted;

var start = (new Date).getTime();

for (var i = 0; i < REPEATS; ++i)
sorted = [anArray _native_sortedArrayUsingDescriptors:descriptors];

var end = (new Date).getTime();

CPLog.warn(_cmd+": " + (end - start) + "ms");

return sorted;
}

- (CPArray)makeUnsorted
{
var array = [];

for (var i=0; i < ELEMENTS; ++i)
for (var i = 0; i < ELEMENTS; ++i)
{
var s = [Sortable new];

Expand All @@ -79,10 +118,46 @@ var ELEMENTS = 100,
return array;
}

- (void)checkSorted:(CPArray)sorted
- (CPArray)makeRandomNumeric
{
var array = [];

for (var i = 0; i < ELEMENTS; i++)
{
var s = [Sortable new],
n1 = ROUND(RAND() * ELEMENTS);
n2 = ROUND(RAND() * ELEMENTS);

[s setA:n1];
[s setB:n2];
array.push(s);
}

return array;
}

- (CPArray)makeRandomText
{
var words = the_big_sort.split(" ", ELEMENTS),
wordcount = words.length,
array = [];

for (var i=0; i < wordcount-1; i++)
{
var s = [Sortable new];

[s setA:words[i]];
[s setB:words[i+1]];
array.push(s);
}

return array;
}

- (void)checkAlmostSorted:(CPArray)sorted
{
// Verify it really got sorted.
for (var j=0; j < ELEMENTS; ++j)
for (var j=0; j < sorted.length; ++j)
{
var expectedA = 4-FLOOR(j * 5 / ELEMENTS);

Expand All @@ -96,6 +171,49 @@ var ELEMENTS = 100,
}
}

- (void)checkRandomSorted:(CPArray)sorted
{
// Verify it really got sorted.
for (var j=0; j < sorted.length - 1; ++j)
{
if ([sorted[j].a compare:sorted[j+1].a] == CPOrderedAscending)
[self fail:"a out of order: " + sorted[j].a + " > " + sorted[j+1].a];

if ([sorted[j].a compare:sorted[j+1].a] == CPOrderedSame && [sorted[j].b compare: sorted[j+1].b] == CPOrderedDescending)
[self fail:"b out of order: " + sorted[j].b + " < " + sorted[j+1].b];
}
}

@end

@implementation CPArray (NativeSort)

- (CPArray)_native_sortedArrayUsingDescriptors:(CPArray)descriptors
{
var sorted = [self copy];

[sorted _native_sortUsingDescriptors:descriptors];

return sorted;
}

- (CPArray)_native_sortUsingDescriptors:(CPArray)descriptors
{
var count = [descriptors count];

sort(function(lhs, rhs)
{
var i = 0,
result = CPOrderedSame;

while (i < count)
if ((result = [descriptors[i++] compareObject:lhs withObject:rhs]) != CPOrderedSame)
return result;

return result;
});
}

@end

@implementation Sortable : CPObject
Expand Down
1 change: 1 addition & 0 deletions Tests/Foundation/the_big_sort.txt
@@ -0,0 +1 @@
Introduction We have a neighborhood Internet listserve in South Austin that is often a source of good information about painters, plumbers, and lost animals. The e-discussion can also become a parody of liberal preciousness. One participant wrote to say that he planned to live-trap rats that had invaded his garage. The vermin-friendly homeowner wanted to know where it might be safe to relocate the rodents. The one Republican on the newsgroup, Stephen Mason, dared to say what most of us probably thought, volunteering that the man could release the varmints near Masons rat terrier, Hotard, who would happily relocate them to rat heaven. Our lone conservative correspondent, however, knew better than to make his comments overtly political. Mason, an intellectual property attorney and Texas A&M graduate, had tried to start a genuine political discussion on the list the year before. He wasnt about to try again. It was the spring of 2004, so things were already tense when Mason called the newsgroups attention to the election for the board of the local community college. Mason gave the names of both candidates, listed their websites, and then, after a warning that what followed was possibly inappropriate electioneering, recommended one of the candidates. The man Mason backed was deeply conservative, a member of the Federalist Society, a former officer of the Young Conservatives of Texas, and an opponent of gay marriage and adoption. Within the day, the newsgroup reacted in a way that wasnt as much ideological as biological. Mason wasnt just someone to be argued against. For the protection of the group, he needed to be isolated, sealed off, and expelled. First response: Okay, as a member of this list, Id really like to see this political discussion disappear. As a lesbian, obviously Im not going to vote for anybody who doesnt believe I shouldnt be allowed to adopt kids . . . As a resident of Travis Heights and a member of this neighborhood list, Im not interested in having this kind of discussion here. I have to defend myself against my government pretty much daily these days, and one place I dont want to have to do it is on this list. And then: A-men. Stephen, youre in the minority politically on this list and in this neighborhood, and while your opinions are your own to have, this list isnt the place for them . . . T-Hts is my home, and this list an extension of that . . . I hope we can all agree to prevent it from becoming a battleground. Mason responded: [The] ideological balkanization of America is dead-on true . . . Living here, especially as an out Republican, is a great deal of fun, and I learn a great deal from it. The most valuable thing that I learn daily is the capacity to respect people with whom I have disagreements. I hope not to be exiled to some place where the vast majority agrees with me. Then, alas, enough with the calls for reasoned discussion: Im really not interested [in] being surprised by right-wing e-mail in my in box, no matter what its guise. It makes me feel bad, and I dont like it. There were dissenting views. I really dont want political or other uncomfortable subjects segregated into some kind of opinion ghetto where only proselytizing activists and ideologues would venture, wrote one South Austinite. Another: Im going to stick my neck out here. I DO want to have political discussion . . . It is a tragedy that this country is becoming more and more polarized. Neighborhoods are becoming more and more homogeneous, either Democrat or Republican. Talk about segregation! But Mason got the picture: he was a Republican Crusoe on this Democratic island, and so he withdrew, promising never to talk politics again with his neighbors. Look, I know a lot about homogeneous political communities, Mason said one afternoon at a South Austin coffeehouse. Hed grown up in the Republican suburbs of Houston and attended the conservative hive of Texas A&M University. He was pugnacious about his politics, ending every e-mail message with a quote from Teddy Roosevelt: Aggressive fighting for the right is the greatest sport in the world. Mason was also engaged to marry a feminist filmmaker, an experience that taught him that nearly everything is political. But nothing had prepared him for the unsettling experience of being a political minority in the community where he lived — for being a minority in the age of political segregation. I ran off that listserve when the rhetoric became so shrill that I didnt have the taste for saying about anyone the things you would need to say to win that argument, Mason continued. Im not going to use that listserve for politics again, and there is some shame in that. The experience had changed the way he saw himself in the neighborhood. Knowing he was a minority, he wondered what people thought of him as he walked Hotard. In some way after that exchange, I think Im viewed with suspicion by my neighbors because of an act of political expression, which is a little on the bizarre side, he said. Im just a guy who has a dog and works a job. Chapter 3: The Psychology of the Tribe Washington was, from its beginning, a politically segregated city. In his forty-year-old study, The Washington Community, historian James Sterling Young mapped three Washingtons, one created for each of the three branches of government. The nine members of the U.S. Supreme Court lived in the same house until 1845. Executive branch workers gathered in one section of the city, near the White House, while congressmen were bunched together nearer the Capitol. Men whom the Constitution merely separated into different work groups separated themselves into different societies, wrote Young. Congressmen lived in boarding houses. They formed eating clubs around common tables, and they slept together, two to a room. Young tracked the membership of these new boarding house communities and found that the residential

0 comments on commit bb8a7ba

Please sign in to comment.