File: VDictProxy.java

package info (click to toggle)
sikuli 1.0~x~rc3.tesseract3-dfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 38,612 kB
  • ctags: 4,643
  • sloc: java: 18,414; cpp: 4,780; python: 2,007; xml: 206; makefile: 169; sh: 29
file content (117 lines) | stat: -rw-r--r-- 4,008 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright 2010-2011, Sikuli.org
 * Released under the MIT License.
 *
 */
package org.sikuli.script;

import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Vector;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

public class VDictProxy<T> {
   private long _instance;
   private Map<Integer, T> _i2obj = new HashMap<Integer, T>();

   static {
      // load libScreenMatchProxy.{so,jnilib}
      //System.loadLibrary("ScreenMatchProxy");
      try{
         System.loadLibrary("VDictProxy");
         Debug.info("VDictProxy loaded.");
      }
      catch(Exception e){
         e.printStackTrace();
      }
   }

   public VDictProxy(){
      _instance = getInstance();
   }

   private native long getInstance();

   private String getAbsolutePath(String filename) throws FileNotFoundException{
      if(new File(filename).exists())
         return filename;
      filename = Settings.BundlePath + File.separator + filename;
      if(new File(filename).exists())
         return filename;
      throw new FileNotFoundException("No such file: " + filename);
   }

   // insert an (key,value) entry using an image key
   public void insert(String imagekey_filename, T value) throws FileNotFoundException {
      imagekey_filename = getAbsolutePath(imagekey_filename);
      int hash = value.hashCode();
      while(true){
         if( hash != -1 && !_i2obj.containsKey(hash) ){
            _i2obj.put(hash, value);
            break;
         }
         else{
            hash += (int)(Math.random()*100);
         }
      }
      _insert(_instance, imagekey_filename, hash);
   }

   public native void _insert(long instance, String imagekey_filename, int value);

   // lookup the entry using an image key (exact match)
   public T lookup(String imagekey_filename) throws FileNotFoundException{
      imagekey_filename = getAbsolutePath(imagekey_filename);
      int hash = _lookup(_instance, imagekey_filename);
      if(hash==-1) return null;
      return _i2obj.get(hash);
   }

   private native int _lookup(long instance, String imagekey_filename);

   // lookup the first entry with a similar image key
   public T lookup_similar(String imagekey_filename, double similarity_threshold) throws FileNotFoundException{
      imagekey_filename = getAbsolutePath(imagekey_filename);
      int hash = _lookup_similar(_instance, imagekey_filename, similarity_threshold);
      if(hash==-1) return null;
      return _i2obj.get(hash);
   }

   private native int _lookup_similar(long instance, String imagekey_filename, double similarity_threshold);

   // lookup at most n entries with keys similar to the given image (n = 0 : all)
   public List<T> lookup_similar_n(String imagekey_filename, double similarity_threshold, int n) throws FileNotFoundException{
      imagekey_filename = getAbsolutePath(imagekey_filename);
      int h[] = _lookup_similar_n(_instance, imagekey_filename, similarity_threshold, n);
      List<T> ret = new Vector<T>(h.length);
      for(int i=0;i<h.length;i++){
         if(h[i] == -1)
            ret.add(i, null);
         else
            ret.add(i, _i2obj.get(h[i]));
      }
      return ret;
   }

   private native int[] _lookup_similar_n(long instance, String imagekey_filename, double similarity_threshold, int n);

   // erase the entry associated with the image
   public void erase(String imagekey_filename) throws FileNotFoundException{
      imagekey_filename = getAbsolutePath(imagekey_filename);
      int h = _lookup(_instance, imagekey_filename);
      if(h!=-1)   _i2obj.remove(h);
      _erase(_instance, imagekey_filename);
   }

   private native void _erase(long _instance, String imagekey_filename);

   public int size(){   return _size(_instance); }
   private native int _size(long instance);  // return the number of image keys stored

   public boolean empty(){ return _empty(_instance);  }
   private native boolean _empty(long instance); // test whether it is empty
}