⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alphabetizer.java

📁 KWIC系统 采用管道/过滤器风格实现
💻 JAVA
字号:
import java.util.ArrayList;
import java.util.Iterator;
import java.io.CharArrayWriter;
import java.io.IOException;


public class Alphabetizer extends Filter
{
    public Alphabetizer(Pipe input, Pipe output)
    {
    	super(input, output);
    }

   /* Sorts lines from a given set, read from the input pipe.  */

    protected void transform()
    {
    try
    {
      ArrayList lines = new ArrayList();      //包含很多行
      CharArrayWriter writer = new CharArrayWriter();  // writer is a buffer that contains
                                                       // characters written to by the
                                                       // write(c) method

      int c = input_.read();   //调用私有变量input_,属于Pipe类型
                               // Reads the next character of data from this piped stream.
      while(c != -1)           // 只要不到文件末尾
      {
        writer.write(c);       // 每次将一个character 写进writer
        if(((char) c) == '\n') // 当遇到 "\n" 时,证明 writer里面已经有了一个完整的行
        {
          String line = writer.toString();  //用toString()将writer中的内容转化成String并且存入line
          lines.add(line);                  //将line添加到lines里,lines 属于 ArrayList 类型,含很多行
          writer.reset();                   //清空writer
        }
        c = input_.read();     // 每次读进一个character c,然后, writer.write(c) 将
                               // c 写进writer里
      }

      sort(lines);      //至此,所有的行已经被写入lines, 它属于ArrayList类型
                        //现在开始排序

      Iterator iterator = lines.iterator();  //创建iterator
      while(iterator.hasNext())  // iterator.hasNext()包含一个word
      {
        char[] chars = ((String) iterator.next()).toCharArray(); // 将这个word转化为String类型
        for(int i = 0; i < chars.length; i++)   //将这个word每次一个字符写进output流
          output_.write(chars[i]);
      }

      output_.closeWriter();    //关闭
    }catch(IOException exc)
    {
      exc.printStackTrace();
      System.err.println("KWIC Error: Could not sort circular shifts.");
      System.exit(1);
    }

  }

//----------------------------------------------------------------------
/**
 * Sorts lines from the given set of lines.
 */

  private void sort(ArrayList lines){ // heap sort
    int size = lines.size();

    for(int i = (size / 2 - 1); i >= 0; i--)
      siftDown(lines, i, size);

    for(int i = (size - 1); i >= 1; i--){
      Object tmp = lines.get(0);
      lines.set(0, lines.get(i));
      lines.set(i, tmp);
      siftDown(lines, 0, i);
    }
  }

//----------------------------------------------------------------------
/**
 * This method builds and reconstucts the heap for the heap sort algorithm.
 * @param lines set of lines
 * @param root heap root
 * @param bottom heap bottom
 */

  private void siftDown(ArrayList lines, int root, int bottom){
    int max_child = root * 2 + 1;

    while(max_child < bottom){
      if((max_child + 1) < bottom)
        if(((String) lines.get(max_child + 1)).compareToIgnoreCase((String) lines.get(max_child)) > 0)
          max_child++;

      if(((String) lines.get(root)).compareToIgnoreCase((String) lines.get(max_child)) < 0){
        Object tmp = lines.get(root);
        lines.set(root, lines.get(max_child));
        lines.set(max_child, tmp);
        root = max_child;
        max_child = root * 2 + 1;
      }else
        break;
    }
  }
}


//           《 关 于 CharArrayWriter 》
// java.io.Writer
//      java.io.CharArrayWriter
// public class CharArrayWriterextends Writer
// This class implements a character buffer that can be used as an Writer.
// The buffer automatically grows when data is written to the stream.
// The data can be retrieved using toCharArray() and toString().
// write(int c) Writes a character to the buffer.
// public String toString() Converts input data to a string.


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -