You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CharVector.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.hyphenation;
  19. import java.io.Serializable;
  20. /**
  21. * This class implements a simple char vector with access to the
  22. * underlying array.
  23. *
  24. * @author Carlos Villegas <cav@uniscope.co.jp>
  25. */
  26. public class CharVector implements Cloneable, Serializable {
  27. private static final long serialVersionUID = 4263472982169004048L;
  28. /**
  29. * Capacity increment size
  30. */
  31. private static final int DEFAULT_BLOCK_SIZE = 2048;
  32. private int blockSize;
  33. /**
  34. * The encapsulated array
  35. */
  36. private char[] array;
  37. /**
  38. * Points to next free item
  39. */
  40. private int n;
  41. /**
  42. * Construct char vector instance with default block size.
  43. */
  44. public CharVector() {
  45. this(DEFAULT_BLOCK_SIZE);
  46. }
  47. /**
  48. * Construct char vector instance.
  49. * @param capacity initial block size
  50. */
  51. public CharVector(int capacity) {
  52. if (capacity > 0) {
  53. blockSize = capacity;
  54. } else {
  55. blockSize = DEFAULT_BLOCK_SIZE;
  56. }
  57. array = new char[blockSize];
  58. n = 0;
  59. }
  60. /**
  61. * Construct char vector instance.
  62. * @param a char array to use
  63. */
  64. public CharVector(char[] a) {
  65. blockSize = DEFAULT_BLOCK_SIZE;
  66. array = a;
  67. n = a.length;
  68. }
  69. /**
  70. * Construct char vector instance.
  71. * @param a char array to use
  72. * @param capacity initial block size
  73. */
  74. public CharVector(char[] a, int capacity) {
  75. if (capacity > 0) {
  76. blockSize = capacity;
  77. } else {
  78. blockSize = DEFAULT_BLOCK_SIZE;
  79. }
  80. array = a;
  81. n = a.length;
  82. }
  83. /**
  84. * Reset length of vector, but don't clear contents.
  85. */
  86. public void clear() {
  87. n = 0;
  88. }
  89. /** {@inheritDoc} */
  90. public Object clone() {
  91. CharVector cv = new CharVector((char[])array.clone(), blockSize);
  92. cv.n = this.n;
  93. return cv;
  94. }
  95. /**
  96. * Obtain char vector array.
  97. * @return char array
  98. */
  99. public char[] getArray() {
  100. return array;
  101. }
  102. /**
  103. * Obtain number of items in array.
  104. * @return number of items
  105. */
  106. public int length() {
  107. return n;
  108. }
  109. /**
  110. * Obtain capacity of array.
  111. * @return current capacity of array
  112. */
  113. public int capacity() {
  114. return array.length;
  115. }
  116. /**
  117. * Pet char at index.
  118. * @param index the index
  119. * @param val a char
  120. */
  121. public void put(int index, char val) {
  122. array[index] = val;
  123. }
  124. /**
  125. * Get char at index.
  126. * @param index the index
  127. * @return a char
  128. */
  129. public char get(int index) {
  130. return array[index];
  131. }
  132. /**
  133. * This is to implement memory allocation in the array. Like malloc().
  134. * @param size to allocate
  135. * @return previous length
  136. */
  137. public int alloc(int size) {
  138. int index = n;
  139. int len = array.length;
  140. if (n + size >= len) {
  141. char[] aux = new char[len + blockSize];
  142. System.arraycopy(array, 0, aux, 0, len);
  143. array = aux;
  144. }
  145. n += size;
  146. return index;
  147. }
  148. /**
  149. * Trim char vector to current length.
  150. */
  151. public void trimToSize() {
  152. if (n < array.length) {
  153. char[] aux = new char[n];
  154. System.arraycopy(array, 0, aux, 0, n);
  155. array = aux;
  156. }
  157. }
  158. }