swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problem with adding to Map collection


From: Marcus G. Daniels
Subject: Re: problem with adding to Map collection
Date: 10 Aug 1999 10:52:18 -0700
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4

AL> 3) add the list objects to a Map object so I can access them by a
AL>   unique key ==> PROBLEM

If you want to use integer keys, you need to tell Map that is your
intent.  The default behavior for Map is to send the -compare: method
to each key.  But since you can only send messages to objects (not
integers), -compare: won't work.

   [[[Map createBegin: aZone]
       setCompareFunction: compareUnsignedIntegers]
     createEnd];

AL> - Is this a good solution anyway?

Well, I think it's unfortunate you're having to write I/O code in C.
I prefer to use higher-level, interpreted tools to do that kind of work.

Supposing you want Maps of ragged-length data in R, i.e. data of
this form:

address@hidden $ cat lists.txt 
10 20 30 40 50
100 200 300 400 500
1000 2000 3000 4000 5000 6000

You can use a `R' program like this to create a HDF5 file from lists.txt.

address@hidden $ cat loadMap.R
wrap <- function (val) {
  obj <- list(val=val)
  attr (obj, "type") <- "MyObject"
  obj
}

mklist <- function (str) {
  l <- eval(parse(text=paste("c(", gsub(" ",",",str), ")")))
  l <- lapply (l, wrap)
  attr (l, "type") <- "List"
  l
}

loadMap <- function (filename) {
  l <- scan (filename,sep="\n",what=list(""))
  map <- lapply (l[[1]], mklist)
  attr(map,"names") <- lapply(1:length(map),"as.character") 
  attr(map,"type") <- "Map"
  attr(map,"component-type") <- "List"
  attr(map,"compare-function") <- "compare-integers"
  map
}

saveFor141 <- function (map) {
 loadMap <- list(default=list(map=map))
 hdf5save("map.hdf",loadMap)
}

saveFor20 <- function (map) {
 hdf5save("map.hdf", map)
}

Running it like so:

address@hidden $ /usr/local/bin/R -q
> source("loadMap.R")
> saveFor141(loadMap("lists.txt"))
Read 3 lines
NULL
> 

To recover the data from the HDF5, you can use the Swarm Archiver interface...

#import <simtools.h> // initSwarm
#import <collections.h> // Map
#import <defobj/Create.h> // CreateDrop

@interface MyObject: CreateDrop
{
  double val;
}
- (void)describe: outputCharStream;
@end

@implementation MyObject
- (void)describe: outputCharStream
{
  char buf[30];

  sprintf (buf, "val: %f\n", val);
  [outputCharStream catC: buf];
}
@end

int
main (int argc, const char **argv)
{
  id archiver;
  id map;
  
  initSwarmBatch (argc, argv);
  
  archiver = [[[[Archiver createBegin: globalZone]
                 setPath: "map.hdf"]
                setHDF5Flag: YES]
               createEnd];
  map = [archiver getObject: "map"];

  {
    id index = [map begin: globalZone];
    id member;
    int key;

    for (member = [index next: (id *) &key];
         [index getLoc] == Member;
         member = [index next: (id *) &key])
      {
        printf ("%d:\n", key);
        xfprint (member);
      }
    [index drop];
  }
}

/*
Local Variables:
compile-command: "/opt/gnu/bin/gcc -o loadMap -g -Wno-import 
-L/opt/SUNWtcl/8.0/sun4/lib -R/opt/SUNWtcl/8.0/sun4/lib -L/opt/SDGblt/2.4g/lib 
-R/opt/SDGblt/2.4g/lib -L/opt/SDGlibffi/1.20/lib -R/opt/SDGlibffi/1.20/lib 
-L/opt/SDGswarm/1.4.1/lib -L/opt/SDGzlib/1.1.3/lib -L/usr/local/X11/lib 
-R/usr/local/X11/lib -L/usr/openwin/lib -R/usr/openwin/lib 
-L/opt/SDGhdf5/1.0.1/lib -I/opt/SDGswarm/1.4.1/include loadMap.m -lanalysis 
-lsimtools -lsimtoolsgui -lactivity -ltkobjc -lrandom -lobjectbase  -ldefobj 
-lcollections -lmisc  -ltclobjc -ltk8.0 -ltcl8.0 -lBLT -lsocket -ldl -lnsl 
-L/usr/openwin/lib -lhdf5 -lpng -lz -lXpm -lX11 -lffi -lm -lobjc -lpthread 
-lposix4"
End:
*/

Which yields:

address@hidden $ ./loadMap
1:
val: 10.000000
val: 20.000000
val: 30.000000
val: 40.000000
val: 50.000000
2:
val: 100.000000
val: 200.000000
val: 300.000000
val: 400.000000
val: 500.000000
3:
val: 1000.000000
val: 2000.000000
val: 3000.000000
val: 4000.000000
val: 5000.000000
val: 6000.000000

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]