PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/powerloom/src/main/java/edu/isi/stella/List.java

https://bitbucket.org/kbanczyk/tds-repo
Java | 1258 lines | 835 code | 174 blank | 249 comment | 164 complexity | cc9d5851282e84532a56ec54342880ac MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // -*- Mode: Java -*-
  2. //
  3. // List.java
  4. /*
  5. +---------------------------- BEGIN LICENSE BLOCK ---------------------------+
  6. | |
  7. | Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
  8. | |
  9. | The contents of this file are subject to the Mozilla Public License |
  10. | Version 1.1 (the "License"); you may not use this file except in |
  11. | compliance with the License. You may obtain a copy of the License at |
  12. | http://www.mozilla.org/MPL/ |
  13. | |
  14. | Software distributed under the License is distributed on an "AS IS" basis, |
  15. | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
  16. | for the specific language governing rights and limitations under the |
  17. | License. |
  18. | |
  19. | The Original Code is the STELLA Programming Language. |
  20. | |
  21. | The Initial Developer of the Original Code is |
  22. | UNIVERSITY OF SOUTHERN CALIFORNIA, INFORMATION SCIENCES INSTITUTE |
  23. | 4676 Admiralty Way, Marina Del Rey, California 90292, U.S.A. |
  24. | |
  25. | Portions created by the Initial Developer are Copyright (C) 1996-2010 |
  26. | the Initial Developer. All Rights Reserved. |
  27. | |
  28. | Contributor(s): |
  29. | |
  30. | Alternatively, the contents of this file may be used under the terms of |
  31. | either the GNU General Public License Version 2 or later (the "GPL"), or |
  32. | the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
  33. | in which case the provisions of the GPL or the LGPL are applicable instead |
  34. | of those above. If you wish to allow use of your version of this file only |
  35. | under the terms of either the GPL or the LGPL, and not to allow others to |
  36. | use your version of this file under the terms of the MPL, indicate your |
  37. | decision by deleting the provisions above and replace them with the notice |
  38. | and other provisions required by the GPL or the LGPL. If you do not delete |
  39. | the provisions above, a recipient may use your version of this file under |
  40. | the terms of any one of the MPL, the GPL or the LGPL. |
  41. | |
  42. +---------------------------- END LICENSE BLOCK -----------------------------+
  43. */
  44. package edu.isi.stella;
  45. import edu.isi.stella.javalib.*;
  46. public class List extends Sequence {
  47. public Cons theConsList;
  48. /** Return a list containing <code>values</code>, in order.
  49. * @param values
  50. * @return List
  51. */
  52. public static List list(Cons values) {
  53. { List list = List.newList();
  54. { Stella_Object v = null;
  55. Cons iter000 = values;
  56. Cons collect000 = null;
  57. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  58. v = iter000.value;
  59. if (collect000 == null) {
  60. {
  61. collect000 = Cons.cons(v, Stella.NIL);
  62. if (list.theConsList == Stella.NIL) {
  63. list.theConsList = collect000;
  64. }
  65. else {
  66. Cons.addConsToEndOfConsList(list.theConsList, collect000);
  67. }
  68. }
  69. }
  70. else {
  71. {
  72. collect000.rest = Cons.cons(v, Stella.NIL);
  73. collect000 = collect000.rest;
  74. }
  75. }
  76. }
  77. }
  78. return (list);
  79. }
  80. }
  81. public static List newList() {
  82. { List self = null;
  83. self = new List();
  84. self.theConsList = Stella.NIL;
  85. return (self);
  86. }
  87. }
  88. public static List cppSortUnitsForHeaderFile(List fileunits) {
  89. { Cons globals = Stella.NIL;
  90. Cons auxiliaryglobals = Stella.NIL;
  91. Cons methods = Stella.NIL;
  92. Cons includestatements = Stella.NIL;
  93. Cons classes = Stella.NIL;
  94. Cons main = Stella.NIL;
  95. { TranslationUnit unit = null;
  96. Cons iter000 = fileunits.theConsList;
  97. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  98. unit = ((TranslationUnit)(iter000.value));
  99. { Symbol testValue000 = unit.category;
  100. if ((testValue000 == Stella.SYM_STELLA_METHOD) ||
  101. ((testValue000 == Stella.SYM_STELLA_PRINT_METHOD) ||
  102. (testValue000 == Stella.SYM_STELLA_MACRO))) {
  103. if (TranslationUnit.cppUnitDefinesMainP(unit)) {
  104. main = Cons.cons(unit, main);
  105. }
  106. else {
  107. methods = Cons.cons(unit, methods);
  108. }
  109. }
  110. else if (testValue000 == Stella.SYM_STELLA_VERBATIM) {
  111. { Cons translation = ((Cons)(unit.translation));
  112. String code = ((StringWrapper)(translation.rest.value)).wrapperValue;
  113. if ((code.charAt(0) == '#') &&
  114. (!(Stella.$CHARACTER_TYPE_TABLE$[(int) (code.charAt(1))] == Stella.KWD_WHITE_SPACE))) {
  115. { TranslationUnit self000 = TranslationUnit.newTranslationUnit();
  116. self000.category = Stella.SYM_STELLA_VERBATIM;
  117. self000.translation = Stella_Object.copyConsTree(unit.translation);
  118. includestatements = Cons.cons(self000, includestatements);
  119. }
  120. translation.secondSetter(StringWrapper.wrapString(""));
  121. }
  122. }
  123. }
  124. else if (testValue000 == Stella.SYM_STELLA_CLASS) {
  125. classes = Cons.cons(unit, classes);
  126. }
  127. else if (testValue000 == Stella.SYM_STELLA_GLOBAL_VARIABLE) {
  128. if (TranslationUnit.auxiliaryVariableUnitP(unit)) {
  129. auxiliaryglobals = Cons.cons(unit, auxiliaryglobals);
  130. unit.category = Stella.SYM_STELLA_AUXILIARY_VARIABLE;
  131. }
  132. else {
  133. globals = Cons.cons(unit, globals);
  134. }
  135. }
  136. else if (testValue000 == Stella.SYM_STELLA_TYPE) {
  137. }
  138. else {
  139. { OutputStringStream stream000 = OutputStringStream.newOutputStringStream();
  140. stream000.nativeStream.print("`" + testValue000 + "' is not a valid case option");
  141. throw ((StellaException)(StellaException.newStellaException(stream000.theStringReader()).fillInStackTrace()));
  142. }
  143. }
  144. }
  145. }
  146. }
  147. return (List.list(Cons.cons(includestatements.reverse(), Cons.cons(classes.reverse(), Cons.cons(globals.reverse(), Cons.cons(methods.reverse(), Cons.cons(auxiliaryglobals.reverse(), Cons.cons(main, Stella.NIL))))))));
  148. }
  149. }
  150. public static Module computeFileUnitsModule(List fileunits) {
  151. { TranslationUnit unit = null;
  152. Cons iter000 = fileunits.theConsList;
  153. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  154. unit = ((TranslationUnit)(iter000.value));
  155. if (unit.tuHomeModule != null) {
  156. return (unit.tuHomeModule);
  157. }
  158. }
  159. }
  160. return (((Module)(Stella.$MODULE$.get())));
  161. }
  162. public static List ejectDemon(List demonlist, Demon demon) {
  163. if (demonlist == null) {
  164. return (null);
  165. }
  166. demonlist.remove(demon);
  167. if (demonlist.emptyP()) {
  168. demonlist.free();
  169. demonlist = null;
  170. }
  171. return (demonlist);
  172. }
  173. public static List injectDemon(List demonlist, Demon demon) {
  174. if (List.nullListP(demonlist)) {
  175. demonlist = List.newList();
  176. }
  177. { ListIterator it = ((ListIterator)(demonlist.allocateIterator()));
  178. while (it.nextP()) {
  179. if (Stella.stringEqlP(((Demon)(it.value)).demonName, demon.demonName)) {
  180. it.valueSetter(demon);
  181. return (demonlist);
  182. }
  183. }
  184. }
  185. demonlist.insert(demon);
  186. return (demonlist);
  187. }
  188. public static void helpPrintContextTree(List list, int level) {
  189. { Context c = null;
  190. Cons iter000 = list.theConsList;
  191. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  192. c = ((Context)(iter000.value));
  193. System.out.println();
  194. { int i = Stella.NULL_INTEGER;
  195. int iter001 = 1;
  196. int upperBound000 = level;
  197. boolean unboundedP000 = upperBound000 == Stella.NULL_INTEGER;
  198. for (;unboundedP000 ||
  199. (iter001 <= upperBound000); iter001 = iter001 + 1) {
  200. i = iter001;
  201. i = i;
  202. System.out.print(" ");
  203. }
  204. }
  205. System.out.print(c.contextNumber);
  206. if (c.contextName() != null) {
  207. System.out.print(" " + c.contextName());
  208. }
  209. System.out.println();
  210. List.helpPrintContextTree(c.childContexts, level + 1);
  211. }
  212. }
  213. }
  214. public static void clOutputSystemClStructsFile(List classunits) {
  215. { String outputfile = Stella.makeFileName(Stella.clYieldStructClassFileName(((SystemDefinition)(Stella.$CURRENTSYSTEMDEFINITION$.get())).name), Stella.KWD_LISP, true);
  216. OutputFileStream outputstream = OutputFileStream.newOutputFileStream(outputfile);
  217. List classes = List.newList();
  218. Module currentmodule = ((Module)(Stella.$MODULE$.get()));
  219. String renamed_Package = null;
  220. List ensuredpackages = List.list(Stella.NIL);
  221. { TranslationUnit unit = null;
  222. Cons iter000 = classunits.theConsList;
  223. Cons collect000 = null;
  224. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  225. unit = ((TranslationUnit)(iter000.value));
  226. if (collect000 == null) {
  227. {
  228. collect000 = Cons.cons(unit.theObject, Stella.NIL);
  229. if (classes.theConsList == Stella.NIL) {
  230. classes.theConsList = collect000;
  231. }
  232. else {
  233. Cons.addConsToEndOfConsList(classes.theConsList, collect000);
  234. }
  235. }
  236. }
  237. else {
  238. {
  239. collect000.rest = Cons.cons(unit.theObject, Stella.NIL);
  240. collect000 = collect000.rest;
  241. }
  242. }
  243. }
  244. }
  245. classes = List.sortClStructClasses(classes);
  246. if (((Integer)(Stella.$TRANSLATIONVERBOSITYLEVEL$.get())).intValue() >= 1) {
  247. System.out.println("Writing `" + outputfile + "'...");
  248. }
  249. { Object old$Module$000 = Stella.$MODULE$.get();
  250. Object old$Context$000 = Stella.$CONTEXT$.get();
  251. try {
  252. Native.setSpecial(Stella.$MODULE$, ((Module)(Stella.$MODULE$.get())));
  253. Native.setSpecial(Stella.$CONTEXT$, ((Module)(Stella.$MODULE$.get())));
  254. if (classes.nonEmptyP()) {
  255. Native.setSpecial(Stella.$MODULE$, ((Stella_Class)(classes.first())).homeModule());
  256. }
  257. else {
  258. Native.setSpecial(Stella.$MODULE$, Stella.getStellaModule(((SystemDefinition)(Stella.$CURRENTSYSTEMDEFINITION$.get())).cardinalModule, true));
  259. }
  260. currentmodule = ((Module)(Stella.$MODULE$.get()));
  261. OutputStream.clOutputFileHeader(outputstream, outputfile, true);
  262. { TranslationUnit unit = null;
  263. Cons iter001 = classunits.theConsList;
  264. Stella_Class renamed_Class = null;
  265. Cons iter002 = classes.theConsList;
  266. for (;(!(iter001 == Stella.NIL)) &&
  267. (!(iter002 == Stella.NIL)); iter001 = iter001.rest, iter002 = iter002.rest) {
  268. unit = ((TranslationUnit)(iter001.value));
  269. renamed_Class = ((Stella_Class)(iter002.value));
  270. unit.theObject = renamed_Class;
  271. unit.annotation = null;
  272. Native.setSpecial(Stella.$MODULE$, renamed_Class.homeModule());
  273. if (!(((Module)(Stella.$MODULE$.get())) == currentmodule)) {
  274. currentmodule = ((Module)(Stella.$MODULE$.get()));
  275. renamed_Package = ((Module)(Stella.$MODULE$.get())).lispPackage();
  276. if (!(ensuredpackages.memberP(StringWrapper.wrapString(renamed_Package)))) {
  277. ensuredpackages.push(StringWrapper.wrapString(renamed_Package));
  278. outputstream.nativeStream.println();
  279. OutputStream.clOutputEnsurePackageDefinition(outputstream, renamed_Package);
  280. }
  281. outputstream.nativeStream.println();
  282. OutputStream.clOutputInPackageDeclaration(outputstream, renamed_Package);
  283. }
  284. unit.translation = TranslationUnit.clTranslateDefineNativeClassUnit(unit);
  285. TranslationUnit.clOutputOneUnit(unit, outputstream.nativeStream);
  286. }
  287. }
  288. } finally {
  289. Stella.$CONTEXT$.set(old$Context$000);
  290. Stella.$MODULE$.set(old$Module$000);
  291. }
  292. }
  293. outputstream.free();
  294. }
  295. }
  296. public static List sortClStructClasses(List unsortedclasses) {
  297. { HashTable classtable = HashTable.newHashTable();
  298. List roots = List.list(Stella.NIL);
  299. List sortedclasses = List.list(Stella.NIL);
  300. { Stella_Class renamed_Class = null;
  301. Cons iter000 = unsortedclasses.theConsList;
  302. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  303. renamed_Class = ((Stella_Class)(iter000.value));
  304. classtable.insertAt(renamed_Class.classType, Stella.TRUE_WRAPPER);
  305. }
  306. }
  307. { Stella_Class renamed_Class = null;
  308. Cons iter001 = unsortedclasses.theConsList;
  309. Cons collect000 = null;
  310. for (;!(iter001 == Stella.NIL); iter001 = iter001.rest) {
  311. renamed_Class = ((Stella_Class)(iter001.value));
  312. { boolean testValue000 = false;
  313. { boolean foundP000 = false;
  314. { Surrogate renamed_Super = null;
  315. Cons iter002 = renamed_Class.classDirectSupers.theConsList;
  316. loop002 : for (;!(iter002 == Stella.NIL); iter002 = iter002.rest) {
  317. renamed_Super = ((Surrogate)(iter002.value));
  318. if (((BooleanWrapper)(classtable.lookup(renamed_Super))) != null) {
  319. foundP000 = true;
  320. break loop002;
  321. }
  322. }
  323. }
  324. testValue000 = foundP000;
  325. }
  326. testValue000 = !testValue000;
  327. if (testValue000) {
  328. if (collect000 == null) {
  329. {
  330. collect000 = Cons.cons(renamed_Class, Stella.NIL);
  331. if (roots.theConsList == Stella.NIL) {
  332. roots.theConsList = collect000;
  333. }
  334. else {
  335. Cons.addConsToEndOfConsList(roots.theConsList, collect000);
  336. }
  337. }
  338. }
  339. else {
  340. {
  341. collect000.rest = Cons.cons(renamed_Class, Stella.NIL);
  342. collect000 = collect000.rest;
  343. }
  344. }
  345. }
  346. }
  347. }
  348. }
  349. while (roots.nonEmptyP()) {
  350. sortedclasses.push(((Stella_Class)(roots.first())));
  351. { Surrogate sub = null;
  352. Cons iter003 = ((Stella_Class)(roots.pop())).classDirectSubs.theConsList;
  353. for (;!(iter003 == Stella.NIL); iter003 = iter003.rest) {
  354. sub = ((Surrogate)(iter003.value));
  355. if (((BooleanWrapper)(classtable.lookup(sub))) != null) {
  356. roots.push(((Stella_Class)(sub.surrogateValue)));
  357. }
  358. }
  359. }
  360. }
  361. return (sortedclasses.reverse());
  362. }
  363. }
  364. public static void extractStartupFunctionUnits(List startupunits, Symbol startupfnname) {
  365. { int remainingunits = startupunits.length();
  366. int minunitsperfunction = Native.floor((((double)(Stella.$MAX_NUMBER_OF_STARTUP_UNITS$)) / Stella.$STARTUP_TIME_PHASES$.length()) + 1);
  367. Cons cursor = startupunits.theConsList;
  368. Cons phasestart = cursor;
  369. int phaseunits = 0;
  370. Keyword phase = ((Keyword)(((TranslationUnit)(cursor.value)).codeRegister));
  371. Keyword unitphase = phase;
  372. int helpfncounter = 0;
  373. loop000 : while (!(cursor == Stella.NIL)) {
  374. unitphase = ((Keyword)(((TranslationUnit)(cursor.value)).codeRegister));
  375. if (unitphase == phase) {
  376. phaseunits = phaseunits + 1;
  377. if (phaseunits < Stella.$MAX_NUMBER_OF_STARTUP_UNITS$) {
  378. cursor = cursor.rest;
  379. continue loop000;
  380. }
  381. }
  382. if (phaseunits < minunitsperfunction) {
  383. phasestart = cursor;
  384. phaseunits = 0;
  385. phase = unitphase;
  386. continue loop000;
  387. }
  388. { Symbol functionname = Symbol.internDerivedSymbol(startupfnname, "HELP-" + startupfnname.symbolName + Native.integerToString(((long)(helpfncounter = helpfncounter + 1))));
  389. Cons helpfntree = ((Cons)(((TranslationUnit)(phasestart.value)).theObject));
  390. ((TranslationUnit)(phasestart.value)).theObject = Cons.cons(Cons.list$(Cons.cons(Stella.SYM_STELLA_SYS_CALL_FUNCTION, Cons.cons(functionname, Cons.cons(Stella.NIL, Stella.NIL)))), Stella.NIL);
  391. phasestart = phasestart.rest;
  392. { ConsIterator it = phasestart.allocateIterator();
  393. int i = Stella.NULL_INTEGER;
  394. int iter000 = 2;
  395. int upperBound000 = phaseunits;
  396. boolean unboundedP000 = upperBound000 == Stella.NULL_INTEGER;
  397. for (;it.nextP() &&
  398. (unboundedP000 ||
  399. (iter000 <= upperBound000)); iter000 = iter000 + 1) {
  400. i = iter000;
  401. i = i;
  402. helpfntree = helpfntree.concatenate(((Cons)(((TranslationUnit)(it.value)).theObject)), Stella.NIL);
  403. TranslationUnit.clearTranslationUnit(((TranslationUnit)(it.value)));
  404. it.valueSetter(null);
  405. }
  406. }
  407. helpfntree = Cons.list$(Cons.cons(Stella.SYM_STELLA_DEFUN, Cons.cons(functionname, Cons.cons(Cons.list$(Cons.cons(Stella.NIL, Cons.cons(Stella.KWD_PUBLICp, Cons.cons(Stella.SYM_STELLA_FALSE, Cons.cons(Cons.list$(Cons.cons(Stella.SYM_STELLA_VOID_SYS, Cons.cons(Cons.cons(Stella.SYM_STELLA_PROGN, helpfntree.concatenate(Stella.NIL, Stella.NIL)), Cons.cons(Stella.NIL, Stella.NIL)))), Cons.cons(Stella.NIL, Stella.NIL)))))), Stella.NIL))));
  408. KeyValueList.setDynamicSlotValue(((MethodSlot)(Cons.helpWalkAuxiliaryTree(helpfntree, true).theObject)).dynamicSlots, Stella.SYM_STELLA_METHOD_STARTUP_CLASSNAME, StringWrapper.wrapString(Symbol.yieldStartupFunctionClassname(startupfnname)), Stella.NULL_STRING_WRAPPER);
  409. remainingunits = remainingunits - (phaseunits - 1);
  410. if (remainingunits <= Stella.$MAX_NUMBER_OF_STARTUP_UNITS$) {
  411. break loop000;
  412. }
  413. if (unitphase == phase) {
  414. cursor = cursor.rest;
  415. }
  416. phasestart = cursor;
  417. phaseunits = 0;
  418. phase = ((Keyword)(((TranslationUnit)(cursor.value)).codeRegister));
  419. }
  420. }
  421. startupunits.remove(null);
  422. }
  423. }
  424. public Iterator allocateDestructiveListIterator() {
  425. { List self = this;
  426. { Iterator iterator = self.theConsList.allocateDestructiveListIterator();
  427. self.theConsList = Stella.NIL;
  428. self.free();
  429. return (iterator);
  430. }
  431. }
  432. }
  433. /** Return <code>self</code>.
  434. * @return List
  435. */
  436. public List listify() {
  437. { List self = this;
  438. return (self);
  439. }
  440. }
  441. /** Return the set difference of <code>self</code> and <code>otherlist</code> by destructively
  442. * removing elements from <code>self</code> that also occur in <code>otherlist</code>. Uses an <code>eqlP</code>
  443. * test and a simple quadratic-time algorithm. Note that the result is only
  444. * guaranteed to be a set if <code>self</code> is a set.
  445. * @param otherlist
  446. * @return List
  447. */
  448. public List subtract(List otherlist) {
  449. { List self = this;
  450. self.theConsList = self.theConsList.subtract(otherlist.theConsList);
  451. return (self);
  452. }
  453. }
  454. /** Return the set difference of <code>self</code> and <code>otherlist</code> (i.e., all elements
  455. * that are in <code>self</code> but not in <code>otherSet</code>). Uses an <code>eqlP</code> test and a simple
  456. * quadratic-time algorithm. Note that the result is only guaranteed to be a
  457. * set if both <code>self</code> and <code>otherlist</code> are sets.
  458. * @param otherlist
  459. * @return List
  460. */
  461. public List difference(List otherlist) {
  462. { List self = this;
  463. { List result = List.createDerivedList(self);
  464. result.theConsList = self.theConsList.difference(otherlist.theConsList);
  465. return (result);
  466. }
  467. }
  468. }
  469. /** Return the set union of <code>self</code> and <code>otherlist</code>. Uses an <code>eqlP</code> test
  470. * and a simple quadratic-time algorithm. Note that the result is only
  471. * guaranteed to be a set if both <code>self</code> and <code>otherlist</code> are sets.
  472. * @param otherlist
  473. * @return List
  474. */
  475. public List union(List otherlist) {
  476. { List self = this;
  477. { List result = List.createDerivedList(self);
  478. result.theConsList = self.theConsList.union(otherlist.theConsList);
  479. return (result);
  480. }
  481. }
  482. }
  483. /** Return the set intersection of <code>self</code> and <code>otherlist</code>. Uses an <code>eqlP</code>
  484. * test and a simple quadratic-time algorithm. Note that the result is only
  485. * guaranteed to be a set if both <code>self</code> and <code>otherlist</code> are sets.
  486. * @param otherlist
  487. * @return List
  488. */
  489. public List intersection(List otherlist) {
  490. { List self = this;
  491. { List result = List.createDerivedList(self);
  492. result.theConsList = self.theConsList.intersection(otherlist.theConsList);
  493. return (result);
  494. }
  495. }
  496. }
  497. /** Return true if every element of <code>self</code> occurs in <code>otherlist</code> and vice versa.
  498. * Uses an <code>eqlP</code> test and a simple quadratic-time algorithm. Note that
  499. * this does not check whether <code>self</code> and <code>otherlist</code> actually are sets.
  500. * @param otherlist
  501. * @return boolean
  502. */
  503. public boolean equivalentSetsP(List otherlist) {
  504. { List self = this;
  505. return (self.theConsList.equivalentSetsP(otherlist.theConsList));
  506. }
  507. }
  508. /** Return true if every element of <code>self</code> also occurs in <code>otherlist</code>.
  509. * Uses an <code>eqlP</code> test and a simple quadratic-time algorithm. Note that
  510. * this does not check whether <code>self</code> and <code>otherlist</code> actually are sets.
  511. * @param otherlist
  512. * @return boolean
  513. */
  514. public boolean subsetP(List otherlist) {
  515. { List self = this;
  516. return (self.theConsList.subsetP(otherlist.theConsList));
  517. }
  518. }
  519. public List removeDeletedMembers() {
  520. { List self = this;
  521. self.theConsList = self.theConsList.removeDeletedMembers();
  522. return (self);
  523. }
  524. }
  525. /** Generate all but the last element of the list <code>self</code>.
  526. * @return Iterator
  527. */
  528. public Iterator butLast() {
  529. { List self = this;
  530. return (self.theConsList.butLast());
  531. }
  532. }
  533. public AbstractIterator allocateIterator() {
  534. { List self = this;
  535. { ListIterator iterator = ListIterator.newListIterator();
  536. iterator.listIteratorCursor = self.theConsList;
  537. iterator.listIteratorCollection = self;
  538. iterator.firstIterationP = true;
  539. return (iterator);
  540. }
  541. }
  542. }
  543. /** Return an <code>equalP</code> hash code for <code>self</code>. Note that this
  544. * is O(N) in the number of elements of <code>self</code>.
  545. * @return int
  546. */
  547. public int equalHashCode() {
  548. { List self = this;
  549. { int code = 5619580;
  550. return ((code ^ (self.theConsList.equalHashCode())));
  551. }
  552. }
  553. }
  554. /** Return TRUE iff the lists <code>x</code> and <code>y</code> are structurally
  555. * equivalent. Uses <code>equalP</code> to test equality of elements.
  556. * @param y
  557. * @return boolean
  558. */
  559. public boolean objectEqualP(Stella_Object y) {
  560. { List x = this;
  561. if (Surrogate.subtypeOfP(Stella_Object.safePrimaryType(y), Stella.SGT_STELLA_LIST)) {
  562. { List y000 = ((List)(y));
  563. return (Stella_Object.equalP(x.theConsList, y000.theConsList));
  564. }
  565. }
  566. else {
  567. }
  568. return (false);
  569. }
  570. }
  571. /** Return NIL-LIST iff <code>self</code> is NULL or <code>self</code> otherwise.
  572. * @param self
  573. * @return List
  574. */
  575. public static List mapNullToNilList(List self) {
  576. return (((self == null) ? Stella.NIL_LIST : self));
  577. }
  578. /** Perform a stable, destructive sort of <code>self</code> according to
  579. * <code>predicate</code>, and return the result. If <code>predicate</code> has a '&lt;' semantics, the
  580. * result will be in ascending order. If <code>predicate</code> is NULL, a suitable
  581. * '&lt;' predicate is chosen depending on the first element of <code>self</code>, and it
  582. * is assumed that all elements of <code>self</code> have the same type (supported
  583. * element types are GENERALIZED-SYMBOL, STRING, INTEGER, and FLOAT).
  584. * @param predicate
  585. * @return List
  586. */
  587. public List sort(java.lang.reflect.Method predicate) {
  588. { List self = this;
  589. self.theConsList = self.theConsList.sort(predicate);
  590. return (self);
  591. }
  592. }
  593. /** Make <code>self</code> an empty list.
  594. */
  595. public void clear() {
  596. { List self = this;
  597. self.theConsList = Stella.NIL;
  598. }
  599. }
  600. /** Return a copy of the list <code>self</code>. The conses in the copy are
  601. * freshly allocated.
  602. * @return List
  603. */
  604. public List copy() {
  605. { List self = this;
  606. { List copy = List.createDerivedList(self);
  607. copy.theConsList = Cons.copyConsList(self.theConsList);
  608. return (copy);
  609. }
  610. }
  611. }
  612. /** Create a new list object with the same type as <code>self</code>.
  613. * @param self
  614. * @return List
  615. */
  616. public static List createDerivedList(List self) {
  617. { Surrogate listtype = self.primaryType();
  618. if (listtype == Stella.SGT_STELLA_LIST) {
  619. return (List.newList());
  620. }
  621. else if (listtype == Stella.SGT_STELLA_SET) {
  622. return (Set.newSet());
  623. }
  624. else {
  625. return (((List)(Surrogate.createObject(listtype, Stella.NIL))));
  626. }
  627. }
  628. }
  629. /** Copy <code>list2</code> onto the front of the list <code>self</code>.
  630. * The operation is destructive wrt <code>self</code>, but leaves <code>list2</code> intact.
  631. * @param list2
  632. * @return List
  633. */
  634. public List prepend(List list2) {
  635. { List self = this;
  636. if (list2.emptyP()) {
  637. return (self);
  638. }
  639. self.theConsList = Cons.copyConsList(list2.theConsList).concatenate(self.theConsList, Stella.NIL);
  640. return (self);
  641. }
  642. }
  643. /** Copy <code>list2</code> and all <code>otherlists</code> onto the end of <code>list1</code>.
  644. * The operation is destructive wrt <code>list1</code>, but leaves all other lists intact.
  645. * The two mandatory parameters allow us to optimize the common binary case by
  646. * not relying on the somewhat less efficient variable arguments mechanism.
  647. * @param list2
  648. * @param otherlists
  649. * @return List
  650. */
  651. public List concatenate(List list2, Cons otherlists) {
  652. { List list1 = this;
  653. { Cons firstlist = list1.theConsList;
  654. Cons lastlist = list2.theConsList;
  655. Cons copy = Stella.NIL;
  656. if (firstlist == Stella.NIL) {
  657. firstlist = Cons.copyConsList(lastlist);
  658. lastlist = firstlist;
  659. }
  660. else if (lastlist == Stella.NIL) {
  661. lastlist = firstlist;
  662. }
  663. else {
  664. lastlist = Cons.copyConsList(lastlist);
  665. Cons.lastCons(firstlist).rest = lastlist;
  666. }
  667. if (otherlists.length() > 0) {
  668. { List list = null;
  669. Cons iter000 = otherlists;
  670. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  671. list = ((List)(iter000.value));
  672. if (list.nonEmptyP()) {
  673. copy = Cons.copyConsList(list.theConsList);
  674. if (!(lastlist == Stella.NIL)) {
  675. Cons.lastCons(lastlist).rest = copy;
  676. }
  677. else {
  678. firstlist = copy;
  679. }
  680. lastlist = copy;
  681. }
  682. }
  683. }
  684. }
  685. list1.theConsList = firstlist;
  686. return (list1);
  687. }
  688. }
  689. }
  690. /** Destructively replace each appearance of <code>outvalue</code> by
  691. * <code>invalue</code> in the list <code>self</code>.
  692. * @param invalue
  693. * @param outvalue
  694. * @return List
  695. */
  696. public List substitute(Stella_Object invalue, Stella_Object outvalue) {
  697. { List self = this;
  698. self.theConsList = self.theConsList.substitute(invalue, outvalue);
  699. return (self);
  700. }
  701. }
  702. /** Reverse the members of <code>self</code> (in place).
  703. * @return List
  704. */
  705. public List reverse() {
  706. { List self = this;
  707. self.theConsList = self.theConsList.reverse();
  708. return (self);
  709. }
  710. }
  711. /** Remove and return the first element in the list <code>self</code>.
  712. * Return NULL if the list is empty.
  713. * @return Stella_Object
  714. */
  715. public Stella_Object pop() {
  716. { List self = this;
  717. { Cons cons = self.theConsList;
  718. Stella_Object value = null;
  719. if (cons == Stella.NIL) {
  720. return (null);
  721. }
  722. value = cons.value;
  723. self.theConsList = cons.rest;
  724. return (value);
  725. }
  726. }
  727. }
  728. /** <code>removeDuplicates</code> (which see) using an <code>equalP</code> test.
  729. * @return List
  730. */
  731. public List removeDuplicatesEqual() {
  732. { List self = this;
  733. if (!(self.theConsList == Stella.NIL)) {
  734. self.theConsList = self.theConsList.removeDuplicatesEqual();
  735. }
  736. return (self);
  737. }
  738. }
  739. /** Destructively remove duplicates from <code>self</code> and return the result.
  740. * Preserves the original order of the remaining members.
  741. * @return Collection
  742. */
  743. public Collection removeDuplicates() {
  744. { List self = this;
  745. if (!(self.theConsList == Stella.NIL)) {
  746. self.theConsList = self.theConsList.removeDuplicates();
  747. }
  748. return (self);
  749. }
  750. }
  751. /** Destructively remove all members of the list <code>self</code> for which
  752. * 'test?' evaluates to TRUE. <code>test</code> takes a single argument of type OBJECT and
  753. * returns TRUE or FALSE. Returns <code>self</code>.
  754. * @param testP
  755. * @return List
  756. */
  757. public List removeIf(java.lang.reflect.Method testP) {
  758. { List self = this;
  759. self.theConsList = self.theConsList.removeIf(testP);
  760. return (self);
  761. }
  762. }
  763. /** Destructively remove all entries in <code>self</code> that match <code>value</code>.
  764. * @param value
  765. * @return AbstractCollection
  766. */
  767. public AbstractCollection remove(Stella_Object value) {
  768. { List self = this;
  769. self.theConsList = self.theConsList.remove(value);
  770. return (self);
  771. }
  772. }
  773. /** Insert <code>value</code> as the last entry in the list <code>self</code>.
  774. * @param value
  775. */
  776. public void insertLast(Stella_Object value) {
  777. { List self = this;
  778. if (!(!(self == Stella.NIL_LIST))) {
  779. System.err.print("Safety violation: Attempt to insert into NIL-LIST.");
  780. }
  781. { Cons cursor = self.theConsList;
  782. Cons lastcons = Cons.cons(value, Stella.NIL);
  783. if (cursor == Stella.NIL) {
  784. self.theConsList = lastcons;
  785. }
  786. else {
  787. Cons.addConsToEndOfConsList(cursor, lastcons);
  788. }
  789. }
  790. }
  791. }
  792. /** Add <code>value</code> to the front of the list <code>self</code> unless its
  793. * already a member.
  794. * @param value
  795. */
  796. public void insertNew(Stella_Object value) {
  797. { List self = this;
  798. if (!(!(self == Stella.NIL_LIST))) {
  799. System.err.print("Safety violation: Attempt to insert into NIL-LIST.");
  800. }
  801. if (!self.theConsList.memberP(value)) {
  802. self.theConsList = Cons.cons(value, self.theConsList);
  803. }
  804. }
  805. }
  806. /** Add <code>value</code> to the front of the list <code>self</code>.
  807. * @param value
  808. */
  809. public void push(Stella_Object value) {
  810. { List self = this;
  811. if (!(!(self == Stella.NIL_LIST))) {
  812. System.err.print("Safety violation: Attempt to insert into NIL-LIST.");
  813. }
  814. self.theConsList = Cons.cons(value, self.theConsList);
  815. }
  816. }
  817. /** Add <code>value</code> to the front of the list <code>self</code>.
  818. * @param value
  819. */
  820. public void insert(Stella_Object value) {
  821. { List self = this;
  822. if (!(!(self == Stella.NIL_LIST))) {
  823. System.err.print("Safety violation: Attempt to insert into NIL-LIST.");
  824. }
  825. self.theConsList = Cons.cons(value, self.theConsList);
  826. }
  827. }
  828. /** Return the position of <code>renamed_Object</code> within the list
  829. * <code>self</code> (counting from zero); or return NULL if <code>renamed_Object</code> does not occur within
  830. * <code>self</code> (uses an <code>eqlP</code> test). If <code>end</code> was supplied as non-NULL, only
  831. * consider the sublist ending at <code>end</code>, however, the returned position
  832. * will always be relative to the entire list.
  833. * @param renamed_Object
  834. * @param end
  835. * @return int
  836. */
  837. public int lastPosition(Stella_Object renamed_Object, int end) {
  838. { List self = this;
  839. return (self.theConsList.lastPosition(renamed_Object, end));
  840. }
  841. }
  842. /** Return the position of <code>renamed_Object</code> within the list
  843. * <code>self</code> (counting from zero); or return NULL if <code>renamed_Object</code> does not occur within
  844. * <code>self</code> (uses an <code>eqlP</code> test). If <code>start</code> was supplied as non-NULL, only
  845. * consider the sublist starting at <code>start</code>, however, the returned position
  846. * will always be relative to the entire list.
  847. * @param renamed_Object
  848. * @param start
  849. * @return int
  850. */
  851. public int position(Stella_Object renamed_Object, int start) {
  852. { List self = this;
  853. return (self.theConsList.position(renamed_Object, start));
  854. }
  855. }
  856. public int length() {
  857. { List self = this;
  858. return (self.theConsList.length());
  859. }
  860. }
  861. public Stella_Object nthSetter(Stella_Object value, int position) {
  862. { List self = this;
  863. return (self.theConsList.nthSetter(value, position));
  864. }
  865. }
  866. public Stella_Object fifthSetter(Stella_Object value) {
  867. { List self = this;
  868. return (self.theConsList.fifthSetter(value));
  869. }
  870. }
  871. public Stella_Object fourthSetter(Stella_Object value) {
  872. { List self = this;
  873. return (self.theConsList.fourthSetter(value));
  874. }
  875. }
  876. public Stella_Object thirdSetter(Stella_Object value) {
  877. { List self = this;
  878. return (self.theConsList.thirdSetter(value));
  879. }
  880. }
  881. public Stella_Object secondSetter(Stella_Object value) {
  882. { List self = this;
  883. return (self.theConsList.secondSetter(value));
  884. }
  885. }
  886. public Stella_Object firstSetter(Stella_Object value) {
  887. { List self = this;
  888. return (self.theConsList.firstSetter(value));
  889. }
  890. }
  891. /** Return a cons list of all but the first item in the list <code>self</code>.
  892. * @return Cons
  893. */
  894. public Cons rest() {
  895. { List self = this;
  896. return (self.theConsList.rest);
  897. }
  898. }
  899. /** Return the last element of <code>self</code>.
  900. * @return Stella_Object
  901. */
  902. public Stella_Object last() {
  903. { List self = this;
  904. return (self.theConsList.last());
  905. }
  906. }
  907. /** Return the nth item in the list <code>self</code>, or NULL if empty.
  908. * @param position
  909. * @return Stella_Object
  910. */
  911. public Stella_Object nth(int position) {
  912. { List self = this;
  913. return (self.theConsList.nth(position));
  914. }
  915. }
  916. /** Return the fifth item in the list <code>self</code>, or NULL if empty.
  917. * @return Stella_Object
  918. */
  919. public Stella_Object fifth() {
  920. { List self = this;
  921. return (self.theConsList.fifth());
  922. }
  923. }
  924. /** Return the fourth item in the list <code>self</code>, or NULL if empty.
  925. * @return Stella_Object
  926. */
  927. public Stella_Object fourth() {
  928. { List self = this;
  929. return (self.theConsList.fourth());
  930. }
  931. }
  932. /** Return the third item in the list <code>self</code>, or NULL if empty.
  933. * @return Stella_Object
  934. */
  935. public Stella_Object third() {
  936. { List self = this;
  937. return (self.theConsList.rest.rest.value);
  938. }
  939. }
  940. /** Return the second item in the list <code>self</code>, or NULL if empty.
  941. * @return Stella_Object
  942. */
  943. public Stella_Object second() {
  944. { List self = this;
  945. return (self.theConsList.rest.value);
  946. }
  947. }
  948. /** Return the first item in the list <code>self</code>, or NULL if empty.
  949. * @return Stella_Object
  950. */
  951. public Stella_Object first() {
  952. { List self = this;
  953. { Cons cons = self.theConsList;
  954. return (((!(cons == Stella.NIL)) ? cons.value : null));
  955. }
  956. }
  957. }
  958. /** Return TRUE iff <code>renamed_Object</code> is a member of the cons list
  959. * <code>self</code> (uses an 'eq?' test).
  960. * @param renamed_Object
  961. * @return boolean
  962. */
  963. public boolean membP(Stella_Object renamed_Object) {
  964. { List self = this;
  965. { Stella_Object i = null;
  966. Cons iter000 = self.theConsList;
  967. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  968. i = iter000.value;
  969. if (i == renamed_Object) {
  970. return (true);
  971. }
  972. }
  973. }
  974. return (false);
  975. }
  976. }
  977. /** Return TRUE iff <code>renamed_Object</code> is a member of the list
  978. * <code>self</code> (uses an <code>eqlP</code> test).
  979. * @param renamed_Object
  980. * @return boolean
  981. */
  982. public boolean memberP(Stella_Object renamed_Object) {
  983. { List self = this;
  984. { Stella_Object i = null;
  985. Cons iter000 = self.theConsList;
  986. for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
  987. i = iter000.value;
  988. if (Stella_Object.eqlP(i, renamed_Object)) {
  989. return (true);
  990. }
  991. }
  992. }
  993. return (false);
  994. }
  995. }
  996. public static boolean terminateListP(List self) {
  997. if (self == Stella.NIL_LIST) {
  998. if (!(!(self == Stella.NIL_LIST))) {
  999. System.err.print("Safety violation: Attempt to free NIL-LIST.");
  1000. }
  1001. return (false);
  1002. }
  1003. { Cons cons = self.theConsList;
  1004. Cons rest = null;
  1005. while (!(cons == Stella.NIL)) {
  1006. rest = cons.rest;
  1007. cons.free();
  1008. cons = rest;
  1009. }
  1010. self.theConsList = Stella.NIL;
  1011. return (true);
  1012. }
  1013. }
  1014. /** Return TRUE if the list <code>self</code> has at least one member.
  1015. * @return boolean
  1016. */
  1017. public boolean nonEmptyP() {
  1018. { List self = this;
  1019. return (!(self.theConsList == Stella.NIL));
  1020. }
  1021. }
  1022. /** Return TRUE if the list <code>self</code> has no members.
  1023. * @return boolean
  1024. */
  1025. public boolean emptyP() {
  1026. { List self = this;
  1027. return (self.theConsList == Stella.NIL);
  1028. }
  1029. }
  1030. /** Return TRUE iff <code>self</code> is NULL or the <code>NIL_LIST</code>.
  1031. * @param self
  1032. * @return boolean
  1033. */
  1034. public static boolean nullListP(List self) {
  1035. return ((self == Stella.NIL_LIST) ||
  1036. (self == null));
  1037. }
  1038. /** Return TRUE unless <code>self</code> is NULL or the <code>NIL_LIST</code>.
  1039. * @param self
  1040. * @return boolean
  1041. */
  1042. public static boolean definedListP(List self) {
  1043. return ((!(self == Stella.NIL_LIST)) &&
  1044. (self != null));
  1045. }
  1046. /** Return a list of elements in <code>self</code>.
  1047. * @return Cons
  1048. */
  1049. public Cons consify() {
  1050. { List self = this;
  1051. return (self.theConsList);
  1052. }
  1053. }
  1054. public void printObject(java.io.PrintStream stream) {
  1055. { List self = this;
  1056. {
  1057. stream.print("|l|");
  1058. Cons.printCons(self.theConsList, stream, "(", ")");
  1059. }
  1060. }
  1061. }
  1062. public static Stella_Object accessListSlotValue(List self, Symbol slotname, Stella_Object value, boolean setvalueP) {
  1063. if (slotname == Stella.SYM_STELLA_THE_CONS_LIST) {
  1064. if (setvalueP) {
  1065. self.theConsList = ((Cons)(value));
  1066. }
  1067. else {
  1068. value = self.theConsList;
  1069. }
  1070. }
  1071. else {
  1072. { OutputStringStream stream000 = OutputStringStream.newOutputStringStream();
  1073. stream000.nativeStream.print("`" + slotname + "' is not a valid case option");
  1074. throw ((StellaException)(StellaException.newStellaException(stream000.theStringReader()).fillInStackTrace()));
  1075. }
  1076. }
  1077. return (value);
  1078. }
  1079. public Surrogate primaryType() {
  1080. { List self = this;
  1081. return (Stella.SGT_STELLA_LIST);
  1082. }
  1083. }
  1084. }