vector_append.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package kawa.standard;
  2. import gnu.lists.*;
  3. import gnu.mapping.*;
  4. /**
  5. * Implement the Scheme extended function "vector-append".
  6. * @author Per Bothner
  7. */
  8. public class vector_append extends ProcedureN
  9. {
  10. public static final vector_append vectorAppend
  11. = new vector_append("vector-append");
  12. public vector_append(String name)
  13. {
  14. super(name);
  15. }
  16. public Object applyN (Object[] args)
  17. {
  18. return apply$V(args);
  19. }
  20. public static FVector apply$V (Object[] args) {
  21. return new FVector(appendToArray(args));
  22. }
  23. public static Object[] appendToArray(Object[] args)
  24. {
  25. int length = 0;
  26. int args_length = args.length;
  27. for (int i = args_length; --i >= 0; )
  28. {
  29. Object arg = args[i];
  30. if (arg instanceof FVector)
  31. length += ((FVector)arg).size();
  32. else
  33. {
  34. int n = LList.listLength(arg, false);
  35. if (n < 0)
  36. throw new WrongType (vectorAppend, i, arg, "list or vector");
  37. length += n;
  38. }
  39. }
  40. Object[] result = new Object [length];
  41. int position = 0;
  42. for (int i = 0; i < args_length; i++)
  43. {
  44. Object arg = args[i];
  45. if (arg instanceof FVector)
  46. {
  47. FVector vec = (FVector) arg;
  48. int vec_length = vec.size();
  49. for (int j = 0; j < vec_length; j++)
  50. result[position++] = vec.get(j);
  51. }
  52. else if (arg instanceof Pair)
  53. {
  54. while (arg != LList.Empty)
  55. {
  56. Pair pair = (Pair) arg;
  57. result[position++] = pair.getCar();
  58. arg = pair.getCdr();
  59. }
  60. }
  61. }
  62. return result;
  63. }
  64. }