Custom data model

Discussion in 'iOS Programming' started by kikko088, Oct 23, 2013.

  1. kikko088 macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #1
    I need to implement a data model with 2 field: y and x (this is like an "index") where values can be positive or negative and need to be float.
    The array isn't the best way because it start from 0 to n and have a step of 1 while I need a model that can start from -100 and arrive to 120 with a step of 0.5 (for example).
    I need to insert value at the end or at the beginning or in a determinated position (for example insert between index -50/-45) and search for some value given an index.

    Which is the best way for do it?


    kikko088
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    unless I am misunderstanding, it sounds like an NSDictionary would solve what you are looking for. You can add items by a specified key (your -120 to 100 by 0.5 step) and can retrieve them based on that key

    Edit: You would want an NSMutableDictionary because it sounds like you need to set it up and make modifications on the fly
     
  3. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #3
    The problem of a dictionary si that if i nave 2000/3000 value and i need to find a value i need a lot of time....the value are not sorted so (i think) i don't have a god performance...



    Kikko088
     
  4. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #4
    I don't know a ton about the performance issues that you are stating but I did just do a quick search and stumbled across this article.

    Stack Overflow

    In it, the interesting part is

    and then an edit by the author states this

    So, have you actually tried an NSDictionary?
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    Your description is vague and confusing.

    You want a model that holds 2 values, x and y. Then you say "this is like an index". What is? The X value is like an index?

    It sounds to me like an ordered list of some kind.

    You want to be able to look up an entry in this model using the x value (ranging from -100 to 120 with in-between values like -13.5)?

    For a modest sized dataset (a few hundred items at most) an array or a linked list would be fine. You'd just loop through the array/list until you find your item, and inserting an item at an arbitrary index would mean walking the array/list until you find the location where it belongs.

    For a larger data set, a binary tree or balanced b-tree would be better than an array or linked list. A balanced b-tree gives log(n) search performance, if memory serves.

    You could use a (mutable) dictionary, as the other poster suggested, where the index value is the key of the dictionary (converted to an NSNumber) and you simply fetch/replace the item in the dictionary using the index as a key.

    You need to provide more information about how this will be used. Do you need to insert new items into the data? How often? Sort an unsorted collection of elements?
     
  6. devilofspades macrumors member

    Joined:
    Jul 20, 2011
    #6
    what you want to do doesn't exist. you either need to use sqlite / core data or create your own class. creating your own custom class seems like the easiest thing to do. you would just need to write some methods maybe like "dataAtIndex:", "insertData:AtIndex:", "dataAtFirstIndex:", "dataAtLastIndex:", etc. from there you would just need to keep track of the indexes and data in arrays or dictionaries on the backend and use your custom methods to pull apart the list and put it back together so its storing and retrieving it like you want. doesn't seem like it would be to hard to do, just keep it short and simple.
     
  7. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #7

    I need to store The values of a function for display it in a graph, but i want to do itdynamically, fisrt i calculate The first 200 (or 1000, it dipende on user input) values then when user move the graphic calculate other value and add it to original model (this for i prove performance), after some movment The model con have 2000/3000 value. I think also to use tree model but i never use it before now so I don't know how use it...si battere use dict or tree?
     
  8. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #8
    PS.now I try with dictionary!
     
  9. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    For graphing, you probably want an array of points.

    I would suggest having global data for the graph that tells you about the origin, scale, and range, and how to display the values on the x and y axis.

    Then just store the result values. To graph, you would walk along the array, calculating the X value based on the starting x value + (index*step), and looking up the Y value.

    It's silly to use the x value as an index, or save it in your data, since the x value always varies by a fixed amount from step to step. (Unless you are graphing on a log scale or some other non-linear scale in the x dimension, which is very unusual)
     
  10. devilofspades macrumors member

    Joined:
    Jul 20, 2011
    #10
    any particular reason you don't just create your own custom object for all this data? you could allocate as many of these custom objects that you want and then store them in an array or dictionary. the biggest "challenge" would be just how do you plan on referencing the data store? once you know how you wanna call it, making it store it that way is the easy part.
     

Share This Page