Stream.m
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:11k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. #import <collections/Stream.h>
  2. #import <collections/List.h>
  3. #import <collections/predicates.h>
  4. #import <defobj/defalloc.h>
  5. #import "../defobj/internal.h" // fcall_type_size
  6. #include <objc/objc-api.h>
  7. @implementation ArchiverKeyword_c
  8. PHASE(Creating)
  9. - setKeywordName: (const char *)name
  10. {
  11.   keywordName = STRDUP (name);
  12.   return self;
  13. }
  14. PHASE(Using)
  15. - (const char *)getKeywordName
  16. {
  17.   return keywordName;
  18. }
  19. - (void)lispOutShallow: (id <OutputStream>)stream
  20. {
  21.   [self lispOutDeep: stream];
  22. }
  23. - (void)lispOutDeep: (id <OutputStream>)stream
  24. {
  25.   [stream catC: "#:"];
  26.   [stream catC: keywordName];
  27. }
  28. - (void)drop
  29. {
  30.   FREEBLOCK (keywordName);
  31.   [super drop];
  32. }
  33. @end
  34.     
  35. @implementation ArchiverArray_c
  36. PHASE(Creating)
  37. - setArray: array
  38. {
  39.   id <List> l;
  40.   id proto;
  41.   
  42.   for (l = array, rank = 0; archiver_list_p (l); rank++)
  43.     l = [l getFirst];
  44.   proto = l;
  45.   if (!valuep (proto))
  46.     raiseEvent (InvalidArgument, "Array element not numeric");
  47.   
  48.   dims = [getZone (self) alloc: rank * sizeof (unsigned)];
  49.   
  50.   {
  51.     unsigned dimnum;
  52.     
  53.     elementCount = 1;
  54.     for (l = array, dimnum = 0; archiver_list_p (l); l = [l getFirst], dimnum++)
  55.       {
  56.         dims[dimnum] = [l getCount];
  57.         elementCount *= dims[dimnum];
  58.       }
  59.   }
  60.   type = [proto getValueType];
  61.   elementSize = fcall_type_size (type);
  62.   {
  63.     size_t size = elementCount * elementSize;
  64.     data = [getZone (self) alloc: size];
  65.     memset (data, 0, size);
  66.   }
  67.   
  68.   {
  69.     unsigned coord[rank];
  70.     
  71.     void expand (id val, unsigned dimnum)
  72.       {
  73.         if (archiver_list_p (val))
  74.           {
  75.             id <Index> li = [val begin: scratchZone];
  76.             id item;
  77.             unsigned pos = 0;
  78.             
  79.             while ((item = [li next]) != nil)
  80.               {
  81.                 coord[dimnum] = pos;
  82.                 expand (item, dimnum + 1);
  83.                 pos++;
  84.               }
  85.             [li drop];
  86.           }
  87.         else
  88.           {
  89.             unsigned i;
  90.             unsigned mult = 1;
  91.             unsigned offset = 0;
  92.             
  93.             offset = coord[rank - 1];
  94.             if (!valuep (val))
  95.               raiseEvent (InvalidArgument, "Array element not a number");
  96.             for (i = rank - 1; i > 0; i--)
  97.               {
  98.                 mult *= dims[i];
  99.                 offset += coord[i - 1] * mult;
  100.               }
  101.             switch (type)
  102.               {
  103.               case fcall_type_object:
  104.                 ((id *) data)[offset] = [val getObject];
  105.                 break;
  106.       case fcall_type_class:
  107. ((Class *) data)[offset] = [val getClass];
  108. break;
  109.               case fcall_type_boolean:
  110.                 ((BOOL *) data)[offset] = [val getBoolean];
  111.                 break;
  112.               case fcall_type_slonglong:
  113.                 ((long long *) data)[offset] = [val getLongLong];
  114.                 break;
  115.               case fcall_type_float:
  116.                 ((float *) data)[offset] = [val getFloat];
  117.                 break;
  118.               case fcall_type_double:
  119.                 ((double *) data)[offset] = [val getDouble];
  120.                 break;
  121.               case fcall_type_long_double:
  122.                 ((long double *) data)[offset] = [val getLongDouble];
  123.                 break;
  124.               case fcall_type_schar:
  125.                 ((unsigned char *) data)[offset] = [val getChar];
  126.                 break;
  127.               default:
  128.                 raiseEvent (InvalidArgument, "Unknown element type");
  129.               }
  130.           }
  131.       }
  132.     expand (array, 0);
  133.   }
  134.   return self;
  135. }
  136. PHASE(Using)
  137. - (unsigned)getRank
  138. {
  139.   return rank;
  140. }
  141. - (void *)getData
  142. {
  143.   return data;
  144. }
  145. - convertToType: (fcall_type_t)destType dest: (void *)ptr
  146. {
  147.   unsigned coord[rank];
  148.   void permute (unsigned dim)
  149.     {
  150.       unsigned i;
  151.       if (dim < rank)
  152.         {
  153.           for (i = 0; i < dims[dim]; i++)
  154.             {
  155.               coord[dim] = i;
  156.               permute (dim + 1);
  157.             }
  158.         }
  159.       else
  160.         {
  161.           unsigned offset = 0;
  162.           unsigned mult = 1;
  163.           long long val;
  164.           
  165.           offset = coord[rank - 1];
  166.           for (i = rank - 1; i > 0; i--)
  167.             {
  168.               mult *= dims[i];
  169.               offset += coord[i - 1] * mult;
  170.             }
  171.           val = ((long long *) data)[offset];
  172.           switch (destType)
  173.             {
  174.             case fcall_type_sint:
  175.               ((int *) ptr)[offset] = (int) val;
  176.               break;
  177.             case fcall_type_uint:
  178.               ((unsigned *) ptr)[offset] = (unsigned) val;
  179.               break;
  180.             case fcall_type_slong:
  181.               ((long *) ptr)[offset] = (long) val;
  182.               break;
  183.             case fcall_type_ulong:
  184.               ((unsigned long *) ptr)[offset] = (unsigned long) val;
  185.               break;
  186.             case fcall_type_slonglong:
  187.               ((long long *) ptr)[offset] = (long long) val;
  188.               break;
  189.             case fcall_type_ulonglong:
  190.               ((unsigned long long *) ptr)[offset] = (unsigned long long) val;
  191.               break;
  192.             case fcall_type_sshort:
  193.               ((short *) ptr)[offset] = (short) val;
  194.               break;
  195.             case fcall_type_ushort:
  196.               ((unsigned short *) ptr)[offset] = (unsigned short) val;
  197.               break;
  198.             default:
  199.               abort ();
  200.             }
  201.         }
  202.     }
  203.   if (type == fcall_type_slonglong)
  204.     permute (0);
  205.   else
  206.     memcpy (ptr, data, fcall_type_size (type) * elementCount);
  207.   return self;
  208. }
  209. - (unsigned *)getDims
  210. {
  211.   return dims;
  212. }
  213. - (unsigned)getElementCount
  214. {
  215.   return elementCount;
  216. }
  217. - (size_t)getElementSize
  218. {
  219.   return elementSize;
  220. }
  221. - (fcall_type_t)getArrayType
  222. {
  223.   return type;
  224. }
  225. - (void)lispOutShallow: (id <OutputStream>)stream
  226. {
  227.   [self lispOutDeep: stream];
  228. }
  229. - (void)lispOutDeep: (id <OutputStream>)stream
  230. {
  231.   lisp_process_array (rank, dims, type, data, data, stream, YES);
  232. }
  233. - (void)drop
  234. {
  235.   [getZone (self) free: dims];
  236.   [getZone (self) free: data];
  237.   [getZone (self) free: (void *) type];
  238.   [super drop];
  239. }
  240. @end
  241. @implementation ArchiverValue_c
  242. PHASE(Creating)
  243. - setDouble: (double)val
  244. {
  245.   type = fcall_type_double;
  246.   value.d = val;
  247.   return self;
  248. }
  249. - setLongDouble: (long double)val
  250. {
  251.   type = fcall_type_long_double;
  252.   value.ld = val;
  253.   return self;
  254. }
  255. - setBoolean: (BOOL)val
  256. {
  257.   type = fcall_type_boolean;
  258.   value.bool = val;
  259.   return self;
  260. }
  261. - setFloat: (float)val
  262. {
  263.   type = fcall_type_float;
  264.   value.f = val;
  265.   return self;
  266. }
  267. - setLongLong: (long long)val
  268. {
  269.   type = fcall_type_slonglong;
  270.   value.ll = val;
  271.   return self;
  272. }  
  273. - setChar: (char)val
  274. {
  275.   type = fcall_type_schar;
  276.   value.ch = val;
  277.   return self;
  278. }  
  279. - setNil
  280. {
  281.   type = fcall_type_object;
  282.   value.obj = nil;
  283.   return self;
  284. }
  285. - setClass: (Class)aClass
  286. {
  287.   type = fcall_type_class;
  288.   value.class = aClass;
  289.   return self;
  290. }
  291. PHASE(Using)
  292. - (fcall_type_t)getValueType
  293. {
  294.   return type;
  295. }
  296. - (long double)getLongDouble
  297. {
  298.   return value.ld;
  299. }
  300. - (double)getDouble
  301. {
  302.   return value.d;
  303. }
  304. - (float)getFloat
  305. {
  306.   return value.f;
  307. }
  308. - (long long)getLongLong
  309. {
  310.   return value.ll;
  311. }
  312. - (int)getInteger
  313. {
  314.   if (type != fcall_type_slonglong)
  315.     raiseEvent (InvalidArgument, "expecting an integer");
  316.   return (int) value.ll;
  317. }
  318. - (unsigned)getUnsigned
  319. {
  320.   if (type != fcall_type_slonglong)
  321.     raiseEvent (InvalidArgument, "expecting an integer");
  322.   return (unsigned) value.ll;
  323. }
  324. - (char)getChar
  325. {
  326.   return value.ch;
  327. }
  328. - (BOOL)getBoolean
  329. {
  330.   return value.ch;
  331. }
  332. - getObject
  333. {
  334.   return value.obj;
  335. }
  336. - (Class)getClass
  337. {
  338.   return value.class;
  339. }
  340. - (void)lispOutShallow: (id <OutputStream>)stream
  341. {
  342.   [self lispOutDeep: stream];
  343. }
  344. - (void)lispOutDeep: (id <OutputStream>)stream
  345. {
  346.   switch (type)
  347.     {
  348.     case fcall_type_boolean:
  349.       [stream catBoolean: value.bool];
  350.       break;
  351.     case fcall_type_schar: case fcall_type_uchar:
  352.       [stream catChar: value.ch];
  353.       break;
  354.     case fcall_type_double:
  355.       [stream catDouble: value.d];
  356.       break;
  357.     case fcall_type_long_double:
  358.       [stream catLongDouble: value.ld];
  359.       break;
  360.     case fcall_type_float:
  361.       [stream catFloat: value.f];
  362.       break;
  363.     case fcall_type_slonglong:
  364.       [stream catLongLong: value.ll];
  365.       break;
  366.     case fcall_type_object:
  367.       if (value.obj != nil)
  368.         abort ();
  369.       else
  370.         [stream catNil];
  371.       break;
  372.     default:
  373.       [stream catC: "serialization for this type not implemented yet"];
  374.       break;
  375.     }
  376. }
  377. @end
  378. @implementation ArchiverPair_c
  379. PHASE(Creating)
  380. - setCar: val;
  381. {
  382.   car = val;
  383.   return self;
  384. }
  385. - setCdr: val
  386. {
  387.   cdr = val;
  388.   return self;
  389. }
  390. - setConsFormatFlag: (BOOL)theConsFormatFlag
  391. {
  392.   consFormatFlag = theConsFormatFlag;
  393.   return self;
  394. }
  395. PHASE(Using)
  396. - getCar
  397. {
  398.   return car;
  399. }
  400. - getCdr
  401. {
  402.   return cdr;
  403. }
  404. - (BOOL)getConsFormatFlag
  405. {
  406.   return consFormatFlag;
  407. }
  408. - (void)lispOutShallow: (id <OutputStream>)stream
  409. {
  410.   [self lispOutDeep: stream];
  411. }
  412. - (void)lispOutDeep: (id <OutputStream>)stream
  413. {
  414.   [stream catC: (consFormatFlag ? "(cons " : "(")];
  415.   [car lispOutDeep: stream];
  416.   [stream catC: (consFormatFlag ? " ": " . ")];
  417.   [cdr lispOutDeep: stream];
  418.   [stream catC: ")"];
  419. }
  420. @end
  421. @implementation ArchiverList_c
  422. PHASE(Creating)
  423. PHASE(Setting)
  424. PHASE(Using)
  425. - (void)lispOutShallow: (id <OutputStream>)stream
  426. {
  427.   [self lispOutDeep: stream];
  428. }
  429. - (void)lispOutDeep: (id <OutputStream>)stream
  430. {
  431.   id index = [self begin: getZone (self)];
  432.   id member;
  433.   [stream catC: "("];
  434.   // get the first member of the list
  435.   member = [index next];
  436.   if (list_literal_p (member))
  437.     [stream catC: [member getC]];
  438.   else if (cons_literal_p (member))
  439.     [stream catC: [member getC]];
  440.   else if (stringp (member))
  441.     {
  442.       const char *funcName = [member getC];
  443.       if (strcmp (funcName, MAKE_INSTANCE_FUNCTION_NAME) == 0
  444.           || strcmp (funcName, MAKE_CLASS_FUNCTION_NAME) == 0) 
  445.          {
  446.            [stream catC: [member getC]];
  447.            member = [index next];
  448.            [stream catC: " "];
  449.            [member lispOutDeep: stream];
  450.          }
  451.       else if (strcmp (funcName, PARSE_FUNCTION_NAME) == 0)
  452.         [stream catC: [member getC]];        
  453.       else
  454.         raiseEvent (InvalidArgument, "function not one of ",
  455.                     MAKE_INSTANCE_FUNCTION_NAME
  456.                     " or "
  457.                     MAKE_CLASS_FUNCTION_NAME
  458.                     " or "
  459.                     PARSE_FUNCTION_NAME);
  460.     }
  461.   else
  462.     [member lispOutDeep: stream];
  463.   
  464.   for (member = [index next]; [index getLoc] == Member; member = [index next])
  465.     {
  466.       [stream catC: " "];
  467.       if (ARCHIVEREOLP (member))
  468.         break;
  469.       else if (keywordp (member)
  470.                || valuep (member)
  471.                || arrayp (member)
  472.                || pairp (member)
  473.                || quotedp (member)
  474.                || stringp (member)
  475.                || archiver_list_p (member))
  476.         [member lispOutDeep: stream];
  477.       else
  478.         raiseEvent (InvalidArgument, "expression type not supported");
  479.     }
  480.   [stream catC: ")"];
  481.   [index drop];
  482. }
  483. @end
  484. @implementation ArchiverQuoted_c
  485. PHASE(Creating)
  486. - setQuotedObject: aValue
  487. {
  488.   value = aValue;
  489.   return self;
  490. }
  491. PHASE(Using)
  492. - getQuotedObject
  493. {
  494.   return value;
  495. }
  496. - (void)lispOutDeep: (id <OutputStream>)stream
  497. {
  498.   [stream catC: "'"];
  499.   if (stringp (value))
  500.     [stream catC: [value getC]];
  501.   else
  502.     [value lispOutDeep: stream];
  503. }
  504. @end