简体   繁体   中英

Data structure for Matrix lookup in ruby, rails

i want to use a matrix type data structure for storing and looking up values. for this 2d array can be used. but i am looking for a better structure.

Requirements: Matrix columns are fixed, but rows can increase.

for eg see the following structure.

Issue| col1, col2, col3, col4
1    |   0,    1,   0,    0
2    |   0,    1,   0,    1
3    |   1,    1,   0,    0

[values in the structure are used as flag or status field]

now i want this structure to be used for look up

say i want to know the value for issue 2 col1 (which is 0 in above example)

what can be the better structure in ruby for the above scenario?

comments please?

What about a hash?

h = { 1 => [0,1,0,0],
  2 => [0,1,0,1],
  3 => [1,1,0,0] }

#fetch value for issue 2 col 1
puts h[2][0]

In case your data set is large and you want to have faster lookups and a more flexible design (what happens if you'll add a column later as your design evolves?), you might consider an in-memory database like supermodel . That way, you can avoid reinventing the wheel and you gain a lot of functionality and flexibility with very little effort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM