| 1 |
|
| #include "keyboardDistance.h" |
| 2 |
|
| #include <stdlib.h> |
| 3 |
|
| #include <math.h> |
| 4 |
|
| #include <string.h> |
| 5 |
|
| |
| 6 |
|
| |
| 7 |
|
| /* sqrt( 13**2 + 3**2 ) */ |
| 8 |
|
| #define MAX_KBD_DISTANCE 13.34166406412633371248 |
| 9 |
|
| |
| 10 |
|
| /* character map for fast lookups... */ |
| 11 |
|
| #define X_POS 0 |
| 12 |
|
| #define Y_POS 0 |
| 13 |
|
| int qwertyMap[0xFF][2]; |
| 14 |
|
| |
| 15 |
|
| char* qwertyGrid[] = { |
| 16 |
|
| "`1234567890-= ", |
| 17 |
|
| "\tqwertyuiop[]\\", |
| 18 |
|
| " asdfghjkl;\' ", |
| 19 |
|
| " zxcvbnm,./ ", |
| 20 |
|
| }; |
| 21 |
|
| |
| 22 |
|
| char* qwertyShiftedGrid[] = { |
| 23 |
|
| "~!@#$%^&*()_+ ", |
| 24 |
|
| "\tQWERTYUIOP{}|", |
| 25 |
|
| " ASDFGHJKL:\" ", |
| 26 |
|
| " ZXCVBNM<>? ", |
| 27 |
|
| }; |
| 28 |
|
| |
| 29 |
|
| #define GRID_DISTANCE(x1,y1,x2,y2) ( ( x1 == x2 && y1 == y2 ) ? 0 : ( 1 == abs(x1-x2) && 1 == abs(y1-y2) ) ? 1 : sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) ) |
| 30 |
|
| |
| 31 |
0 |
| double c_gridDistance( int x1, int y1, int x2, int y2 ) |
| 32 |
|
| { |
| 33 |
0 |
▶ | return ( x1 == x2 && y1 == y2 ) ? 0 |
Branch: 1 branch
| Branch | True | False | Total |
| return ( x1 == x2 && y1 == y2 ) ? 0 |
0 |
0 |
0 |
|
| 34 |
0 |
▶ | : ( 1 == abs(x1-x2) && 1 == abs(y1-y2) ) ? 1 |
Branch: 2 branches
| Branch | True | False | Total |
| : ( 1 == abs(x1-x2) && 1 == abs(y1-y2) ) ? 1 |
0 |
0 |
0 |
| : ( 1 == abs(x1-x2) && 1 == abs(y1-y2) ) ? 1 |
0 |
0 |
0 |
|
| 35 |
0 |
▶ | : sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ); |
Branch: 1 branch
| Branch | True | False | Total |
| : sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ); |
0 |
0 |
0 |
|
| 36 |
|
| |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
5 |
| double c_qwertyKeyboardDistanceMatch( char* left, int llen, char* right, int rlen ) |
| 40 |
|
| { |
| 41 |
|
| char *ptmp; |
| 42 |
|
| int i; |
| 43 |
|
| double maxDist; |
| 44 |
|
| |
| 45 |
5 |
▶ | if( rlen > llen ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( rlen > llen ) { |
1 |
4 |
5 |
|
| 46 |
1 |
| i = llen; |
| 47 |
1 |
| llen = rlen; |
| 48 |
1 |
| rlen = i; |
| 49 |
|
| |
| 50 |
1 |
| ptmp = left; |
| 51 |
1 |
| left = right; |
| 52 |
1 |
| right = ptmp; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
5 |
| maxDist = MAX_KBD_DISTANCE * llen; |
| 56 |
|
| |
| 57 |
5 |
| return (maxDist - c_qwertyKeyboardDistance(left,llen,right,rlen)) / maxDist; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
10 |
| double c_qwertyKeyboardDistance( char* left, int llen, char* right, int rlen ) |
| 61 |
|
| { |
| 62 |
|
| char *ptmp; |
| 63 |
|
| int i; |
| 64 |
10 |
| double distance = 0.0, dist; |
| 65 |
|
| |
| 66 |
10 |
▶ | if( rlen > llen ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( rlen > llen ) { |
1 |
9 |
10 |
|
| 67 |
1 |
| i = llen; |
| 68 |
1 |
| llen = rlen; |
| 69 |
1 |
| rlen = i; |
| 70 |
|
| |
| 71 |
1 |
| ptmp = left; |
| 72 |
1 |
| left = right; |
| 73 |
1 |
| right = ptmp; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
56 |
▶ | for( i = 0; i < rlen; ++i ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for( i = 0; i < rlen; ++i ) { |
46 |
10 |
56 |
|
| 77 |
46 |
| dist = c_qwertyCharDistance( left[i], right[i] ); |
| 78 |
|
| /* printf("%s(%d) dist(%c,%c) = %f\n",__FILE__,__LINE__,left[i],right[i],dist); */ |
| 79 |
46 |
| distance += dist; |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
28 |
▶ | while( i < llen ) { |
Branch: 1 branch
| Branch | True | False | Total |
| while( i < llen ) { |
18 |
10 |
28 |
|
| 83 |
18 |
| distance += MAX_KBD_DISTANCE; |
| 84 |
18 |
| ++i; |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
10 |
| return distance; |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
46 |
| double c_qwertyCharDistance( char c1, char c2 ) |
| 91 |
|
| { |
| 92 |
|
| #if 0 |
| 93 |
|
| int x1,y1,x2,y2; |
| 94 |
|
| if( ! c_qwertyGetCharPos( c1, &x1, &y1 ) ) { |
| 95 |
|
| return MAX_KBD_DISTANCE; |
| 96 |
|
| } |
| 97 |
|
| |
| 98 |
|
| if( ! c_qwertyGetCharPos( c2, &x2, &y2 ) ) { |
| 99 |
|
| return MAX_KBD_DISTANCE; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| return GRID_DISTANCE( x1,y1, x2,y2 ); |
| 103 |
|
| #else |
| 104 |
|
| if( c1 > 0xFF || c2 > 0xFF ) { |
| 105 |
|
| return MAX_KBD_DISTANCE; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
46 |
▶ | return GRID_DISTANCE( qwertyMap[c1][X_POS], qwertyMap[c1][Y_POS], |
Branch: 4 branches
| Branch | True | False | Total |
| return GRID_DISTANCE( qwertyMap[c1][X_POS], qwertyMap[c1][Y_POS], |
20 |
26 |
46 |
| return GRID_DISTANCE( qwertyMap[c1][X_POS], qwertyMap[c1][Y_POS], |
0 |
20 |
20 |
| return GRID_DISTANCE( qwertyMap[c1][X_POS], qwertyMap[c1][Y_POS], |
8 |
18 |
26 |
| return GRID_DISTANCE( qwertyMap[c1][X_POS], qwertyMap[c1][Y_POS], |
0 |
8 |
8 |
|
| 109 |
|
| qwertyMap[c2][X_POS], qwertyMap[c2][Y_POS] ); |
| 110 |
|
| #endif |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
|
| |
| 114 |
0 |
| int c_qwertyGetCharPos( char ch, int* x, int* y ) |
| 115 |
|
| { |
| 116 |
|
| int i,j,gridSize,elementSize; |
| 117 |
0 |
| gridSize = sizeof(qwertyGrid) / sizeof(qwertyGrid[0]); |
| 118 |
0 |
| elementSize = strlen(qwertyGrid[0]); |
| 119 |
|
| |
| 120 |
0 |
▶ | for( i = 0; i < gridSize; ++i ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for( i = 0; i < gridSize; ++i ) { |
0 |
0 |
0 |
|
| 121 |
0 |
▶ | for( j = 0; j < elementSize; ++j ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for( j = 0; j < elementSize; ++j ) { |
0 |
0 |
0 |
|
| 122 |
0 |
▶ | if( ch == qwertyGrid[i][j] || ch == qwertyShiftedGrid[i][j] ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if( ch == qwertyGrid[i][j] || ch == qwertyShiftedGrid[i][j] ) { |
0 |
0 |
0 |
| if( ch == qwertyGrid[i][j] || ch == qwertyShiftedGrid[i][j] ) { |
0 |
0 |
0 |
|
| 123 |
0 |
| *x = i; |
| 124 |
0 |
| *y = j; |
| 125 |
0 |
| return 1; |
| 126 |
|
| } |
| 127 |
|
| } |
| 128 |
|
| } |
| 129 |
0 |
| return 0; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| |
| 133 |
1 |
| int c_initQwertyMap( void ) |
| 134 |
|
| { |
| 135 |
|
| int i,j,gridSize,elementSize; |
| 136 |
|
| char ch; |
| 137 |
|
| |
| 138 |
1 |
| memset( qwertyMap, MAX_KBD_DISTANCE, 0xFF * 2 ); |
| 139 |
1 |
| gridSize = sizeof(qwertyGrid) / sizeof(qwertyGrid[0]); |
| 140 |
1 |
| elementSize = strlen(qwertyGrid[0]); |
| 141 |
5 |
▶ | for( i = 0; i < gridSize; ++i ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for( i = 0; i < gridSize; ++i ) { |
4 |
1 |
5 |
|
| 142 |
60 |
▶ | for( j = 0; j < elementSize; ++j ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for( j = 0; j < elementSize; ++j ) { |
56 |
4 |
60 |
|
| 143 |
56 |
| ch = qwertyGrid[i][j]; |
| 144 |
56 |
| qwertyMap[ (int)ch ][X_POS] = i; |
| 145 |
56 |
| qwertyMap[ (int)ch ][Y_POS] = j; |
| 146 |
|
| |
| 147 |
56 |
| ch = qwertyShiftedGrid[i][j]; |
| 148 |
56 |
| qwertyMap[ (int)ch ][X_POS] = i; |
| 149 |
56 |
| qwertyMap[ (int)ch ][Y_POS] = j; |
| 150 |
|
| } |
| 151 |
|
| } |
| 152 |
1 |
| return 1; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| |