Pair.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Copyright (c) 2001, 2003 Per M.A. Bothner and Brainfood Inc.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.lists;
  4. import java.io.*;
  5. /** A "pair" object, as used in Lisp-like languages.
  6. * When used to implement a list, the 'car' field contains an
  7. * element's value, and the 'cdr' field points to either the next Pair
  8. * or LList.Empty (which represents the end of the list). (The names
  9. * "car" and "cdr" [pronounced "coulder"] are historical; better names
  10. * might be "value" and "next".) While a Pair is normally usued to
  11. * implement a linked list, sometimes the 'cdr' field points to some
  12. * other non-list object; this is traditionally callled a "dotted list".
  13. */
  14. public class Pair extends LList implements Externalizable
  15. {
  16. protected Object car;
  17. protected Object cdr;
  18. /** A special pair used to indicate incomplete input. */
  19. public static final Pair incompleteListMarker
  20. = new ImmutablePair("<incomplete>", LList.Empty);
  21. public Pair (Object carval, Object cdrval)
  22. {
  23. car = carval;
  24. cdr = cdrval;
  25. }
  26. public Pair ()
  27. {
  28. }
  29. public Object getCar () { return car; }
  30. public Object getCdr () { return cdr; }
  31. public void setCar(Object car) { this.car = car; }
  32. public void setCdr(Object cdr) { this.cdr = cdr; }
  33. /** May go away soon. */
  34. public void setCarBackdoor (Object car) { this.car = car; }
  35. public void setCdrBackdoor (Object cdr) { this.cdr = cdr; }
  36. public int size()
  37. {
  38. int n = listLength(this, true);
  39. if (n >= 0)
  40. return n;
  41. if (n == -1)
  42. return Integer.MAX_VALUE;
  43. throw new RuntimeException("not a true list");
  44. }
  45. public boolean isEmpty()
  46. {
  47. return false;
  48. }
  49. // A generalization of LList.list_length
  50. // FIXME is this needed, given listLength?
  51. public int length ()
  52. {
  53. // Based on list-length implementation in
  54. // Guy L Steele jr: "Common Lisp: The Language", 2nd edition, page 414
  55. int n = 0;
  56. Object slow = this;
  57. Object fast = this;
  58. for (;;)
  59. {
  60. if (fast == Empty)
  61. return n;
  62. if (! (fast instanceof Pair))
  63. {
  64. if (fast instanceof Sequence)
  65. {
  66. int j = ((Sequence) fast).size();
  67. return j >= 0 ? n + j : j;
  68. }
  69. return -2;
  70. }
  71. Pair fast_pair = (Pair) fast;
  72. Object fast_pair_cdr = fast_pair.getCdr();
  73. if (fast_pair_cdr == Empty)
  74. return n+1;
  75. if (fast == slow && n > 0)
  76. return -1;
  77. if (! (fast_pair_cdr instanceof Pair))
  78. {
  79. n++;
  80. fast = fast_pair_cdr;
  81. continue;
  82. }
  83. if (!(slow instanceof Pair))
  84. return -2;
  85. slow = ((Pair)slow).getCdr();
  86. fast = ((Pair)fast_pair_cdr).getCdr();
  87. n += 2;
  88. }
  89. }
  90. public boolean hasNext (int ipos)
  91. {
  92. if (ipos <= 0)
  93. return ipos == 0;
  94. return PositionManager.getPositionObject(ipos).hasNext();
  95. }
  96. public int nextPos (int ipos)
  97. {
  98. if (ipos <= 0)
  99. {
  100. if (ipos < 0)
  101. return 0;
  102. return PositionManager.manager
  103. .register(new LListPosition(this, 1, true));
  104. }
  105. LListPosition it = (LListPosition) PositionManager.getPositionObject(ipos);
  106. return it.gotoNext() ? ipos : 0;
  107. }
  108. public Object getPosNext (int ipos)
  109. {
  110. if (ipos <= 0)
  111. return ipos == 0 ? getCar() : eofValue;
  112. return PositionManager.getPositionObject(ipos).getNext();
  113. }
  114. public Object getPosPrevious (int ipos)
  115. {
  116. if (ipos <= 0)
  117. return ipos == 0 ? eofValue : lastPair().getCar();
  118. return PositionManager.getPositionObject(ipos).getPrevious();
  119. }
  120. public Pair lastPair()
  121. {
  122. Pair pair = this;
  123. for (;;)
  124. {
  125. Object next = pair.getCdr();
  126. if (next instanceof Pair)
  127. pair = (Pair) next;
  128. else
  129. return pair;
  130. }
  131. }
  132. /*
  133. public void print(java.io.PrintWriter ps)
  134. {
  135. ps.print("(");
  136. printNoParen(this, ps);
  137. ps.print(")");
  138. }
  139. */
  140. static public boolean equals (Pair pair1, Pair pair2)
  141. {
  142. if (pair1 == pair2)
  143. return true;
  144. if (pair1 == null || pair2 == null)
  145. return false;
  146. for (;;)
  147. {
  148. Object x1 = pair1.getCar();
  149. Object x2 = pair2.getCar();
  150. if (x1 != x2 && (x1 == null || ! x1.equals(x2)))
  151. return false;
  152. x1 = pair1.getCdr();
  153. x2 = pair2.getCdr();
  154. if (x1 == x2)
  155. return true;
  156. if (x1 == null || x2 == null)
  157. return false;
  158. if (! (x1 instanceof Pair) || !(x2 instanceof Pair))
  159. return x1.equals(x2);
  160. pair1 = (Pair) x1;
  161. pair2 = (Pair) x2;
  162. }
  163. }
  164. /* #ifdef JAVA2 */
  165. static public int compareTo (Pair pair1, Pair pair2)
  166. {
  167. if (pair1 == pair2)
  168. return 0;
  169. if (pair1 == null )
  170. return -1;
  171. if (pair2 == null)
  172. return 1;
  173. for (;;)
  174. {
  175. Object x1 = pair1.getCar();
  176. Object x2 = pair2.getCar();
  177. int d = ((Comparable) x1).compareTo((Comparable) x2);
  178. if (d != 0)
  179. return d;
  180. x1 = pair1.cdr;
  181. x2 = pair2.cdr;
  182. if (x1 == x2)
  183. return 0;
  184. if (x1 == null)
  185. return -1;
  186. if (x2 == null)
  187. return 1;
  188. if (! (x1 instanceof Pair) || !(x2 instanceof Pair))
  189. return ((Comparable) x1).compareTo((Comparable) x2);
  190. pair1 = (Pair) x1;
  191. pair2 = (Pair) x2;
  192. }
  193. }
  194. public int compareTo(Object obj)
  195. {
  196. if (obj == Empty)
  197. return 1;
  198. else
  199. return compareTo(this, (Pair) obj);
  200. }
  201. /* #endif */
  202. public Object get (int index)
  203. {
  204. Pair pair = this;
  205. int i = index;
  206. while (i > 0)
  207. {
  208. i--;
  209. Object pair_cdr = pair.getCdr();
  210. if (pair_cdr instanceof Pair)
  211. pair = (Pair)pair_cdr;
  212. else if (pair_cdr instanceof Sequence)
  213. return ((Sequence)pair_cdr).get(i);
  214. else
  215. break;
  216. }
  217. if (i == 0)
  218. return pair.getCar();
  219. else
  220. throw new IndexOutOfBoundsException ();
  221. }
  222. public boolean equals (Object obj)
  223. {
  224. if ((obj != null) && (obj instanceof Pair))
  225. return equals (this, (Pair) obj);
  226. else
  227. return false;
  228. }
  229. public static Pair make (Object car, Object cdr)
  230. {
  231. return new Pair (car, cdr);
  232. }
  233. public Object[] toArray()
  234. {
  235. int len = size(); // size()
  236. Object[] arr = new Object[len];
  237. int i = 0;
  238. Sequence rest = this;
  239. for ( ; i < len && rest instanceof Pair; i++)
  240. {
  241. Pair pair = (Pair) rest;
  242. arr[i] = pair.getCar();
  243. rest = (Sequence) pair.getCdr();
  244. }
  245. int prefix = i;
  246. for ( ; i < len; i++)
  247. {
  248. arr[i] = rest.get(i - prefix);
  249. }
  250. return arr;
  251. }
  252. public Object[] toArray(Object[] arr)
  253. {
  254. int alen = arr.length;
  255. int len = length();
  256. if (len > alen)
  257. {
  258. // FIXME Collection spec requires arr to keep same run-time type
  259. arr = new Object[len];
  260. alen = len;
  261. }
  262. int i = 0;
  263. Sequence rest = this;
  264. for ( ; i < len && rest instanceof Pair; i++)
  265. {
  266. Pair pair = (Pair) rest;
  267. arr[i] = pair.getCar();
  268. rest = (Sequence) pair.getCdr();
  269. }
  270. int prefix = i;
  271. for ( ; i < len; i++)
  272. {
  273. arr[i] = rest.get(i - prefix);
  274. }
  275. if (len < alen)
  276. arr[len] = null;
  277. return arr;
  278. }
  279. /**
  280. * @serialData Write the car followed by the cdr.
  281. */
  282. public void writeExternal(ObjectOutput out) throws IOException
  283. {
  284. out.writeObject(car);
  285. out.writeObject(cdr);
  286. }
  287. public void readExternal(ObjectInput in)
  288. throws IOException, ClassNotFoundException
  289. {
  290. car = in.readObject();
  291. cdr = in.readObject();
  292. }
  293. /** Needed to override readResolve in LList. */
  294. public Object readResolve() throws ObjectStreamException
  295. {
  296. return this;
  297. }
  298. };