PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/antlr-2.7.5/lib/csharp/src/antlr/TokenQueue.cs

https://github.com/boo/boo-lang
C# | 121 lines | 71 code | 11 blank | 39 comment | 6 complexity | 1a862695ce85f00b82e93d9a6ac78016 MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. namespace antlr
  3. {
  4. /*ANTLR Translator Generator
  5. * Project led by Terence Parr at http://www.jGuru.com
  6. * Software rights: http://www.antlr.org/license.html
  7. *
  8. * $Id:$
  9. */
  10. //
  11. // ANTLR C# Code Generator by Micheal Jordan
  12. // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
  13. // Anthony Oguntimehin
  14. //
  15. // With many thanks to Eric V. Smith from the ANTLR list.
  16. //
  17. /*A private circular buffer object used by the token buffer */
  18. class TokenQueue
  19. {
  20. /*Physical circular buffer of tokens */
  21. private IToken[] buffer;
  22. /*buffer.length-1 for quick modulos */
  23. private int sizeLessOne;
  24. /*physical index of front token */
  25. private int offset;
  26. /*number of tokens in the queue */
  27. protected internal int nbrEntries;
  28. public TokenQueue(int minSize)
  29. {
  30. // Find first power of 2 >= to requested size
  31. int size;
  32. if (minSize < 0)
  33. {
  34. init(16); // pick some value for them
  35. return ;
  36. }
  37. // check for overflow
  38. if (minSize >= (int.MaxValue / 2))
  39. {
  40. init(int.MaxValue); // wow that's big.
  41. return ;
  42. }
  43. for (size = 2; size < minSize; size *= 2)
  44. {
  45. ;
  46. }
  47. init(size);
  48. }
  49. /*Add token to end of the queue
  50. * @param tok The token to add
  51. */
  52. public void append(IToken tok)
  53. {
  54. if (nbrEntries == buffer.Length)
  55. {
  56. expand();
  57. }
  58. buffer[(offset + nbrEntries) & sizeLessOne] = tok;
  59. nbrEntries++;
  60. }
  61. /*Fetch a token from the queue by index
  62. * @param idx The index of the token to fetch, where zero is the token at the front of the queue
  63. */
  64. public IToken elementAt(int idx)
  65. {
  66. return buffer[(offset + idx) & sizeLessOne];
  67. }
  68. /*Expand the token buffer by doubling its capacity */
  69. private void expand()
  70. {
  71. IToken[] newBuffer = new IToken[buffer.Length * 2];
  72. // Copy the contents to the new buffer
  73. // Note that this will store the first logical item in the
  74. // first physical array element.
  75. for (int i = 0; i < buffer.Length; i++)
  76. {
  77. newBuffer[i] = elementAt(i);
  78. }
  79. // Re-initialize with new contents, keep old nbrEntries
  80. buffer = newBuffer;
  81. sizeLessOne = buffer.Length - 1;
  82. offset = 0;
  83. }
  84. /*Initialize the queue.
  85. * @param size The initial size of the queue
  86. */
  87. private void init(int size)
  88. {
  89. // Allocate buffer
  90. buffer = new IToken[size];
  91. // Other initialization
  92. sizeLessOne = size - 1;
  93. offset = 0;
  94. nbrEntries = 0;
  95. }
  96. /*Clear the queue. Leaving the previous buffer alone.
  97. */
  98. public void reset()
  99. {
  100. offset = 0;
  101. nbrEntries = 0;
  102. }
  103. /*Remove token from front of queue */
  104. public void removeFirst()
  105. {
  106. offset = (offset + 1) & sizeLessOne;
  107. nbrEntries--;
  108. }
  109. }
  110. }