LocationEnumeration.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2000, 2002 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.mapping;
  4. public class LocationEnumeration
  5. implements
  6. java.util.Iterator<NamedLocation<Object>>,
  7. java.util.Enumeration<NamedLocation<Object>>
  8. {
  9. SimpleEnvironment env;
  10. NamedLocation prevLoc;
  11. NamedLocation nextLoc;
  12. /** Index field used by Environment.hasMoreElements.
  13. If inherited==null, index in bindings, else index in env.inherited. */
  14. int index;
  15. LocationEnumeration inherited;
  16. NamedLocation[] bindings;
  17. public LocationEnumeration(NamedLocation[] bindings, int count)
  18. {
  19. this.bindings = bindings;
  20. index = count;
  21. }
  22. public LocationEnumeration(SimpleEnvironment env)
  23. {
  24. this(env.table, 1 << env.log2Size);
  25. }
  26. public boolean hasMoreElements()
  27. {
  28. return env.hasMoreElements(this);
  29. }
  30. public NamedLocation nextElement()
  31. {
  32. return nextLocation();
  33. }
  34. public NamedLocation nextLocation()
  35. {
  36. if (nextLoc == null && ! hasMoreElements())
  37. throw new java.util.NoSuchElementException();
  38. NamedLocation oldPrev = prevLoc;
  39. if (prevLoc == null)
  40. {
  41. NamedLocation first = bindings[index];
  42. if (nextLoc != first)
  43. prevLoc = first;
  44. }
  45. while (prevLoc != null && prevLoc.next != nextLoc)
  46. prevLoc = prevLoc.next;
  47. NamedLocation r = nextLoc;
  48. nextLoc = nextLoc.next;
  49. return r;
  50. }
  51. public boolean hasNext ()
  52. {
  53. return hasMoreElements();
  54. }
  55. public NamedLocation next ()
  56. {
  57. return nextLocation();
  58. }
  59. public void remove ()
  60. {
  61. NamedLocation curLoc = prevLoc != null ? prevLoc.next : bindings[index];
  62. if (curLoc == null || curLoc.next != nextLoc)
  63. throw new IllegalStateException();
  64. curLoc.next = null;
  65. if (prevLoc != null)
  66. prevLoc.next = nextLoc;
  67. else
  68. bindings[index] = nextLoc;
  69. env.num_bindings--;
  70. }
  71. }