Symbol Table Applications
1 sets 集合的API
- 比symbol table简单,因为不需要处理值,只处理表中所有键的集合,和相应的值无关
- 源码见课本p315
package Chapter03;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.SET;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class WhiteList {
public static void main(String[] args) {
SET<String> set = new SET<String>();//create empty set of strings
//read in whitelist
In in = new In(args[0]);
while (!in.isEmpty()){
set.add(in.readString());
}
//print words in list
while (!StdIn.isEmpty()){
String word = StdIn.readString();
if (set.contains(word)){
StdOut.println(word);
}
}
}
}
2 dictionary clients 字典类用例
package Chapter03;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.ST;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class LookupCSV {
public static void main(String[] args) {
//process input file
In in = new In(args[0]);
int keyField = Integer.parseInt(args[1]);
int valField = Integer.parseInt(args[2]);
//build symbol table
ST<String, String> st = new ST<String, String>();
while (in.hasNextLine()){
String line = in.readLine();
String[] tokens = line.split(",");
String key = tokens[keyField];
String val = tokens[valField];
st.put(key, val);
}
//process lookups with standard I/O
while (!StdIn.isEmpty()){
String s = StdIn.readString();//s是一个key,需要在terminal输入想要查询的key,回车,显示value
if (!st.contains(s)){
StdOut.println("Not found");
}
else{
StdOut.println(st.get(s));//返回key和value
}
}
}
}
3 indexing clients 索引类用例
package Chapter03;
import edu.princeton.cs.algs4.*;
import java.io.File;
//文件索引
public class FileIndex {
public static void main(String[] args) {
//symbol table
ST<String, SET<File>> st = new ST<String, SET<File>>();
//list of file names from command line
for (String filename : args) {//增强for循环 for(声明新局部变量: 要访问/遍历的) --> 遍历所有输入的文件名
File file = new File(filename);
In in = new In(file);
//for each word in file, add file to corresponding set
while (!in.isEmpty()){
String key = in.readString();//读取每个file的具体单词
if (!st.contains(key)){
st.put(key, new SET<File>());//当key原来不被包含,建立一个新的file set
}
SET<File> set = st.get(key);
set.add(file);
}
}
//process queries
while (!StdIn.isEmpty()){
String query = StdIn.readString();
StdOut.println(st.get(query));
}
}
}
- 书籍中的查找功能
- concordance --> 用语索引;著作或作家全集的重要用字索引
package Chapter03;
import edu.princeton.cs.algs4.*;
public class Concordance {
public static void main(String[] args) {
//read text and build index
In in = new In(args[0]);
String[] words = StdIn.readAll().split("\\s+");//根据空白分割
ST<String, SET<Integer>> st = new ST<String, SET<Integer>>();
for (int i = 0; i < words.length; i++) {
String s = words[i];
if (!st.contains(s)){
st.put(s, new SET<Integer>());
}
SET<Integer> set = st.get(s);
set.add(i);
}
//process queries and print concordances
while (!StdIn.isEmpty()){
String query = StdIn.readString();
SET<Integer> set = st.get(query);
for (int k : set) {
//print words[k-4] to words[k+4]
}
}
}
}
4 sparse vectors 稀疏向量
- 当matrix小,这样做可以,但N往往很大
package Chapter03;
import edu.princeton.cs.algs4.ST;
public class SparseVector {
//HashST because order not important
private ST<Integer, Double> v;
//构造器 empty ST represents all 0s vector
public SparseVector(){
v = new ST<Integer, Double>();
}
//a[i] = value
public void put(int i, double x){
v.put(i, x);
}
//return a[i]
public double get(int i){
if (!v.contains(i)) return 0.0;
else return v.get(i);
}
public Iterable<Integer> indices(){//指数
return v.keys();
}
//dot product is constant time for sparse vectors
public double dot(double[] that){
double sum = 0.0;
for (int i : indices()){
sum += that[i]*this.get(i);//this是v
}
return sum;
}
}