On Jan 7, 2011, at 1:49 AM, <john.whitta...@kc.rr.com> 
<john.whitta...@kc.rr.com> wrote:

> Joe,
> 
> Thanks for the advice on using specialized-arrays.  I started to use them, 
> but then realized that what I wanted to do was more dynamic in nature.  I 
> have this file of log entries where each entry consists of a fixed size bunch 
> of bookkeeping data followed by a blob of application dependent  bytes.  
> Different log files may have different sized blobs of application data for 
> their entries.  Thus I could not create one struct that captured all of the 
> possibilities, and thus could not think of a clean way to use a specialized 
> array.  So, instead what I do now is divide my 128 K blocks into "groups" 
> (using the "group" word) at runtime.  Then I can create a TUPLE from each 
> group.  The TUPLE is defined as my known fixed-size bookkeeping struct and a 
> byte-array.   Here is some of the code for what I did (ignore any Factor 
> newbie ugliness please :)):

Your approach is perfectly workable. However, if you're interested in 
maintaining a packed in-memory representation of your data, note that struct 
objects and specialized-arrays can provide views over parts of a byte-array, 
and you can have multiple objects with views over different parts of the same 
byte-array. You can get a handle to an offset within a byte-array using 
<displaced-alien>:

! get pointer to the application-dependent data in byte-array
bookkeeping-record heap-size byte-array <displaced-alien>

You can also use <displaced-alien> on a displaced-alien to get a new offset 
relative to an existing offset. You can then use the words <direct-X-array> (to 
create a specialized-array of X) and memory>struct (to create a struct) to 
create array or structure views over different parts of the byte-array:

:: structure-bookkeeping-data ( bytes -- bytes' bookkeeping-record 
application-dependent-bytes )
    ! get the fixed-size bookkeeping record
    bytes bookkeeping-record memory>struct :> record
    ! determine the size of the application-dependent bytes
    bookkeeping-record bytes <displaced-alien> :> after-record-bytes
    after-record-bytes determine-application-dependent-size :> ( 
after-size-bytes size )
    ! get the application dependent array
    after-size-bytes size <direct-uchar-array> :> app-bytes
    ! return the pointer after the read data, the bookkeeping record, and the 
app bytes
    size after-size-bytes <displaced-alien>
    record app-bytes ;

You could then run the above function in a loop until the returned bytes' value 
reaches the end of the array. This will let you keep the packed in-memory 
representation while using high-level Factor objects to manipulate the data. 
<displaced-alien> also works with raw pointers from the C FFI, for which it 
just does pointer arithmetic, so the above approach works whether the raw data 
is kept in a byte-array, a malloced buffer, a memory-mapped file, or any 
contiguous block of memory.

-Joe
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to 
best implement a security strategy that keeps consumers' information secure 
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl 
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to