[ANN] Cocoa demos

Arthur Clemens aclemens at xs4all.nl
Mon Jan 12 16:25:11 CST 2004


I have updated the Valentina Cocoa demo for XCode:  
<http://www.visiblearea.com/valentina/ValentinaCocoademoXCode.dmg>

And I have created a Sort demo that shows how to sort a list based on  
another list. For example, to sort a list of words alphabetically OR by  
its modification date:  
<http://www.visiblearea.com/valentina/ValentinaCocoaSortdemoXCode.dmg>
This demo is also made with Cocoa, but the basis is C++, so general  
principles AND code can be re-used.



 From the Readme:

------------------------------------------------------------------------ 
-------------------------------------------


This demo application shows how to sort a list based on another list.  
For example, to sort a list of words alphabetically OR by its  
modification date.

You might note that this application is a bit bigger than the average  
demo, and perhaps not very fit as an educational example. Actually, I  
made it as a private test application to find out how to do sorting in  
a fast way. I distribute this application in the hope that others can  
use some of the ideas I have found.



Sorting difficulties generally arise when the database model is a  
little bit more complex and involves 1..n relations between tables.
In this application, the following model is used:

– Record table: base object, holds:
     – Word table: the actual content we are interested in
     – FormalDescription table: has field for modification date

It would have been simple if all fields were in the Record table: just  
sort the table based on the right field. But that's not life: I want  
Records to have more than one Word.

The solution I found is to generate for each sort option a list of  
pairs:
	pair<sortable content, Record's RecID>

To sort my database on RecID, word and modification date, I need:
	pair<Record's RecID, Record's RecID>
	pair<Word's word, Record's RecID>
	pair<FormalDescription's modification_date, Record's RecID>

There is some overhead in creating these pairs, but for a not too big  
database this is workable. To populate a list of Words of 100.000  
records from an existing database takes around 1.5 seconds on my  
computer, and a list of dates only 0.3 seconds.




First run

Adding records: I suggest you start with adding a fairly large amount  
of records, say 100.000. It takes my computer around 15 seconds, so  
have some patience.

All timings are reported in the box at the bottom. I have noticed that  
performance is directly related to the amount of free RAM available, so  
preferably quit all applications before measuring.

Use 'Add and sort' to time real-world usage of adding records.

Deleting: select a table row and press the Delete key.

Sorting is straightforward, just click on the table headers.
To time the rebuilding of a list, click the button 'Select all  
records'. This reads all records from the database and puts their  
RecIDs in the list. Only the currently sorted column is rebuilt.
When a column is almost sorted, for example when a couple of records  
are added to a big sorted list, it is economical to randomize the list  
first before sorting. Randomizing is a quick operation, and sorting a  
randomized list takes an almost equal amount of time each sort.

Searching uses only the Word table. It has the following syntax  
(partially borrowed from FileMakerPro):
	– prefix '=' for 'starts with'
	– prefix '==' for 'exact match'
	– suffix '=' for 'ends on'
	– just the search string for 'contains'



Code background

The application is made with Objective-C++, a hybrid of Objective-C and  
C++. Almost all implementation files therefore need to have the  
extension .mm.

All pair lists (vectors of struct Dictionary) are stored as pointers in  
a STL vector in a SortArray (C++) object. Sorting pointers is quite  
fast, as you can see in the demo.

I've tried to describe all class relationships in the UML diagram, but  
since this is my first UML drawing I ask you to bear with me (and point  
me to errors, so I can improve).

------------------------------------------------------------------------ 
-------------------------------------------

Arthur Clemens



More information about the Valentina mailing list