Dictionary or Array?

  • Hi list,
    I have a small database I created using a NSMutableArray of
    NSMutableDictionary records.

    e.g. If I need to get the field "name" of the record number 2 I do:

    name = [[databaseArray objectAtIndex:2] objectForKey:@"name"];

    Now I would like to get a pointer to the array containing all the entries
    for the field "name" in order to get the list of all the "names" in my
    database;

    Currently I iterate through the databaseArray and get one by one all the
    "name" entries. This requires time. And up above it's not dynamic because if
    the user adds a record or modifies a record, I have to rebuild the "names"
    array list.

    Is there a way to get "a pointer" to the vertical "names" array?

    I thought to rebuild the database using a vertical NSMutableArray for each
    field. Then in order to get a record I should iterate through the fields.

    name = [namesArray objectAtIndex:2];
    surname = [surnamesArray objectAtIndex:2];
    ...

    Is it a good idea?

    Best Regards
    --
    Lorenzo
    email: <archidea...>
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Lorenzo, if you are always going to have the same set of fields, then
    you really don't need a dictionary, and array access should be a lot
    faster. If you intend to add new fields in an arbitrary way, but a
    subset of these will always be fixed, then I think your hybrid scheme
    is fine, since NSMutable array allows you to insert/remove elements at
    any index.

    Maybe someone else will propose a more elegant solution.

    Randy

    On Friday, June 13, 2003, at 08:50 AM, Lorenzo wrote:

    > Hi list,
    > I have a small database I created using a NSMutableArray of
    > NSMutableDictionary records.
    >
    > e.g. If I need to get the field "name" of the record number 2 I do:
    >
    > name = [[databaseArray objectAtIndex:2] objectForKey:@"name"];
    >
    > Now I would like to get a pointer to the array containing all the
    > entries
    > for the field "name" in order to get the list of all the "names" in my
    > database;
    >
    > Currently I iterate through the databaseArray and get one by one all
    > the
    > "name" entries. This requires time. And up above it's not dynamic
    > because if
    > the user adds a record or modifies a record, I have to rebuild the
    > "names"
    > array list.
    >
    > Is there a way to get "a pointer" to the vertical "names" array?
    >
    > I thought to rebuild the database using a vertical NSMutableArray for
    > each
    > field. Then in order to get a record I should iterate through the
    > fields.
    >
    > name = [namesArray objectAtIndex:2];
    > surname = [surnamesArray objectAtIndex:2];
    > ...
    >
    > Is it a good idea?
    >
    >
    > Best Regards
    > --
    > Lorenzo
    > email: <archidea...>
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    >
    >

    Randy J. Zauhar, PhD
    Assoc. Prof. of Biochemistry
    Director, Graduate Program in Bioinformatics
    Dept. of Chemistry & Biochemistry
    University of the Sciences in Philadelphia
    600 S. 43rd Street
    Philadelphia, PA 19104

    Phone:    (215)596-8691
    FAX:        (215)596-8543
    E-mail:    <r.zauhar...>
    Web:    http://tonga.usip.edu/zauhar

    Discussion after watching Disney's "Lilo & Stitch":

    DAD: "But why did the space aliens speak English, as opposed to French,
    or Swahili? And why did the one alien speak English with an Eastern
    European accent? I don't get it."

    CATHERINE (age 7): "That's 'cause you don't have a good cartoon brain."
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Am Freitag, 13.06.03, um 14:50 Uhr (Europe/Berlin) schrieb Lorenzo:

    > Is there a way to get "a pointer" to the vertical "names" array?
    >

    I dont think what you ask for is possible. I cant imagine how that
    should work. Your dictionary is mutable, so there is no guarantee for
    the existence of any key. If you KNOW that all records have the same
    fields, then you could use a second array, which would probably be
    considerably faster because it wouldnt have to do the lookup in the
    keys-table. Still, you could never get a pointer to any such "vertical
    array", obviously, simply because the values will not be arranged in
    line in memory (they belong to distinct arrays, remember?).

    Enumeration will be the best approach for your case.

    > And up above it's not dynamic because if
    > the user adds a record or modifies a record, I have to rebuild the
    > "names"
    > array list.

    Well, it would be kind of dynamic. The values you store in your name
    array are only pointers to the NSMutableString values in your record
    arrays (given you use mutable strings and do not generate a new
    NSString instance evry time the user modifies an entry).

    > I thought to rebuild the database using a vertical NSMutableArray for
    > each
    > field. Then in order to get a record I should iterate through the
    > fields.
    >
    > name = [namesArray objectAtIndex:2];
    > surname = [surnamesArray objectAtIndex:2];

    You can do that. But then you have to ensure that all field arrays
    contain the same number of entries. You ll have to add an empty string
    if the field is blank for a given record. Then it would work.

    Still, i m not sure whether your concerns about performance are
    reasonable. How many records would you have? why would you need all the
    names in a single array? If it is for display purposes only, I really
    doubt performance would be a factor, if e.g. a table view would query
    you for the values, would it?!

    Marco

    --
    |\  /|        <Marco_Binder...>  http://www.marco-binder.de
    | \/ |        Telefon: 07531 / 94 19 94  Fax: 07531 / 94 19 92
    |    |ARCO        Snail-Mail: Banater Str. 3 - 78467 Konstanz
          BINDER _____________________________________________________
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Lorenzo:

    Rethinking how your data model works may help.  One option is to use a
    setter for your main array that also updates your array of names.  Something
    like (warning: written in mail):

    (void)addDictionaryToDatabase:(NSDictionary *)dict
    {
    [databaseArray addObject:dict];
    [nameArray addObject:[dict objectForKey:name]];
    }

    You could then re-sort nameArray.  It may be more efficient to find the
    right place to put the new name and use insertObject:atIndex: instead.

    Removing an entry from the database works in reverse:

    (void)removeDictionaryFromDatabase:(NSDictionary *)dict
    {
    [nameArray removeObject:[dict objectForKey:name]];
    [databaseArray removeObject:dict];
    }

    Jonathan

    > -----Original Message-----
    > From: <cocoa-dev-admin...>
    > [mailto:<cocoa-dev-admin...>]On Behalf Of Lorenzo
    > Sent: Friday, June 13, 2003 8:50 AM
    > To: <cocoa-dev...>
    > Subject: Dictionary or Array?
    >
    >
    > Hi list,
    > I have a small database I created using a NSMutableArray of
    > NSMutableDictionary records.
    >
    > e.g. If I need to get the field "name" of the record number 2 I do:
    >
    > name = [[databaseArray objectAtIndex:2] objectForKey:@"name"];
    >
    > Now I would like to get a pointer to the array containing all the entries
    > for the field "name" in order to get the list of all the "names" in my
    > database;
    >
    > Currently I iterate through the databaseArray and get one by one all the
    > "name" entries. This requires time. And up above it's not dynamic
    > because if
    > the user adds a record or modifies a record, I have to rebuild the "names"
    > array list.
    >
    > Is there a way to get "a pointer" to the vertical "names" array?
    >
    > I thought to rebuild the database using a vertical NSMutableArray for each
    > field. Then in order to get a record I should iterate through the fields.
    >
    > name = [namesArray objectAtIndex:2];
    > surname = [surnamesArray objectAtIndex:2];
    > ...
    >
    > Is it a good idea?
    >
    >
    > Best Regards
    > --
    > Lorenzo
    > email: <archidea...>
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Friday, June 13, 2003, at 02:11  pm, zauhar wrote:

    > Lorenzo, if you are always going to have the same set of fields, then
    > you really don't need a dictionary, and array access should be a lot
    > faster.

    i thought i read somewhere that dictionaries are faster than arrays? i
    could very easily be wrong there, but i think i read that somewhere.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • You must have that reversed. A dictionary lookup requires a hash
    operation, an array lookup is simple arithmetic.

    Randy

    On Friday, June 13, 2003, at 02:34 PM, Ben Dougall wrote:

    >
    > On Friday, June 13, 2003, at 02:11  pm, zauhar wrote:
    >
    >> Lorenzo, if you are always going to have the same set of fields, then
    >> you really don't need a dictionary, and array access should be a lot
    >> faster.
    >
    > i thought i read somewhere that dictionaries are faster than arrays? i
    > could very easily be wrong there, but i think i read that somewhere.
    >
    >
    >

    Randy J. Zauhar, PhD
    Assoc. Prof. of Biochemistry
    Director, Graduate Program in Bioinformatics
    Dept. of Chemistry & Biochemistry
    University of the Sciences in Philadelphia
    600 S. 43rd Street
    Philadelphia, PA 19104

    Phone:    (215)596-8691
    FAX:        (215)596-8543
    E-mail:    <r.zauhar...>
    Web:    http://tonga.usip.edu/zauhar

    Discussion after watching Disney's "Lilo & Stitch":

    DAD: "But why did the space aliens speak English, as opposed to French,
    or Swahili? And why did the one alien speak English with an Eastern
    European accent? I don't get it."

    CATHERINE (age 7): "That's 'cause you don't have a good cartoon brain."
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • > You must have that reversed. A dictionary lookup requires a hash
    > operation, an array lookup is simple arithmetic.

    If you know the index of the item you want.  If you don't, but do have
    some kind of key - like a field name - then for more than 50 items (or
    so) a hash lookup is going to be much faster than a linear search on
    the array.

    Wade Tregaskis
    -- Sed quis custodiet ipsos custodes?
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Yes,
    that's true, but... if I need to search by the field *containing* a string?
    For example, I want to retrive all the objects containing the word "Cocoa".

    MAIN ARRAY
    Index  String
    --------------------------
    1      "I drink Cocoa"
    2      "I Cocoa thus I am..."
    3      "Cocoa is not chocolate"

    Currently I make a simple (and slow) linear search...
    In order to increase the speed I presume I should build an index of the
    field, but sincerely I don't know how. I have only a vague idea like
    building 2 new and parallel arrays like

    INDEX ARRAYS
    Index  Word        Records containing that word
    ------------------- ----------------------------
    1      "am"        record number 2
    2      "chocolate" record number 3
    3      "Cocoa"    record number 1,2,3
    4      "drink"    record number 1
    5      "I"        record number 1,2
    6      "is"        record number 3
    7      "not"      record number 3
    8      "thus"      record number 2

    So I make an hash lookup to the first array, I find the word "Cocoa" soon at
    the index 3, then I get the values 1,2,3 from the index 3 of the second
    array then I get the 1th, the 2nd and the 3rd objects from my Main Array.

    Am I going on the right way? Or not?

    Best Regards
    ------------
    Lorenzo
    email: <archidea...>

    > From: Wade Tregaskis <wjtregaskis...>
    > Date: Sat, 14 Jun 2003 10:13:37 +1000
    > To: zauhar <r.zauhar...>
    > Cc: Ben Dougall <bend...>, <cocoa-dev...>, Lorenzo
    > <archidea...>
    > Subject: Re: Dictionary or Array?
    >
    >> You must have that reversed. A dictionary lookup requires a hash
    >> operation, an array lookup is simple arithmetic.
    >
    > If you know the index of the item you want.  If you don't, but do have
    > some kind of key - like a field name - then for more than 50 items (or
    > so) a hash lookup is going to be much faster than a linear search on
    > the array.
    >
    > Wade Tregaskis
    > -- Sed quis custodiet ipsos custodes?
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month june 2003 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
Go to today