<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2800.1589" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>The
organization I had in mind was to make sure the whole
hierarchy</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>is
visible in the key:</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2>{{Line,Key,ColumnKey}, Value}</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>Then,
if you know the line and key values, the following command</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>will
efficiently retrieve all column key instances:</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2>ets:match_object(Tab, {{Line, Key, '_'}, '_'}).</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>The
cost will be roughly proportional to the number of objects
returned,</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>by the
search - not (so much) to the number of objects in the
table,</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>even
though the table size does affect the timings slightly.</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>If you
don't know the Line, then searching will be linear, but this
</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>is the
case with your dict of dicts as well.</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>The
doc states that select is a more general form of match() and
match_object(),</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>so you
need to follow the references back to match/2 and
match_object/2 </FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>where
the following is </FONT></SPAN><SPAN class=038032308-16042007><FONT face=Arial
color=#0000ff size=2>written:</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=038032308-16042007>"If the key is specified in the pattern, the
match is very efficient. If the key is not specified, i.e. if it is a variable
or an underscore, the entire table must be searched. The search time can be
substantial if the table is very large."</SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>It
doesn't state that even a partially bound key gives an efficient
match.</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff
size=2>BR,</FONT></SPAN></DIV>
<DIV><SPAN class=038032308-16042007><FONT face=Arial color=#0000ff size=2>Ulf
W</FONT></SPAN></DIV>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> erlang-questions-bounces@erlang.org
[mailto:erlang-questions-bounces@erlang.org] <B>On Behalf Of </B>Ludovic
Coquelle<BR><B>Sent:</B> den 16 april 2007 09:02<BR><B>To:</B> Ulf
Wiger<BR><B>Cc:</B> Erlang<BR><B>Subject:</B> Re: [erlang-questions] is
storing complex objects in ets a goodidea?<BR></FONT><BR></DIV>
<DIV></DIV>Thanks.<BR>Your answer make me understand than ets is more like a
DB than like a dict.<BR><BR>So if my current matrix representation is
something like:<BR>{line key, dict-of-column-values}<BR>a new implementation
with ets should split the dict-of-column structure: <BR>{line key, column key
1, value}<BR>{line key, column key 2, value}<BR>...<BR><BR>Btw, it was not
clear to me from the doc than select and lookup imply different efficiency
results.<BR><BR>Thank you very much.<BR><BR>
<DIV><SPAN class=gmail_quote>On 4/16/07, <B class=gmail_sendername>Ulf
Wiger</B> <<A href="mailto:ulf@wiger.net">ulf@wiger.net</A>>
wrote:</SPAN>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">2007/4/16,
Ludovic Coquelle <<A
onclick="return top.js.OpenExtLink(window,event,this)"
href="mailto:lcoquelle@gmail.com" target=_blank>lcoquelle@gmail.com</A>>:
<DIV><SPAN class=e id=q_111f9126946daa9d_1>
<DIV><SPAN class=gmail_quote></SPAN>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">Hi,<BR><BR>In
my application, I have to store a huge (but sparse) 2D matrix. I
implemented that successfully using dict-of-dict kind of
structure.<BR><BR>I was then thinking to use a ets-of-dict structure: each
line is a dict (containing all colum values) and I store each line (a
dict) as an entry of a ets structure. <BR>But I read in the doc
introduction of ets that "In the current implementation, every object
insert and look-up operation results in one copy of the object.". Does
this means that each access (even read) to an element of my matrix would
imply a new dict copy?<BR></BLOCKQUOTE></DIV><BR></SPAN></DIV>Yes. While ets
outperforms all other alternatives for random access for sets<BR>of small
objects (at least sets of size > 20), it doesn't necessarily do so
for<BR>large objects. <BR><BR>But if a dict-of-dict structure works, you
might want to consider just using an <BR>ordset ets table. If you have a
tuple as the key in an ordset table, ets:select(),<BR>using a pattern where
the first part of the key is bound, will be very efficient.<BR><BR>You can
also create ets tables where the tuples stored have only one element -
<BR>the key. Mnesia doesn't let you do that, but ets does.<BR><BR>BR,<BR>Ulf
W<BR></BLOCKQUOTE></DIV><BR></BLOCKQUOTE></BODY></HTML>