1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-25 13:55:34 -05:00

Fix indenting and header commenting.

This commit is contained in:
Dave Davenport 2017-01-11 09:11:19 +01:00
parent d1edf0dc08
commit bfae111579
5 changed files with 176 additions and 115 deletions

View file

@ -64,15 +64,25 @@
/** The Rofi View log domain */ /** The Rofi View log domain */
#define LOG_DOMAIN "View" #define LOG_DOMAIN "View"
/** Max length of input to score. */
#define FUZZY_SCORER_MAX_LENGTH 256 #define FUZZY_SCORER_MAX_LENGTH 256
/** minimum score */
#define MIN_SCORE ( INT_MIN / 2 ) #define MIN_SCORE ( INT_MIN / 2 )
/** Leading gap score */
#define LEADING_GAP_SCORE -4 #define LEADING_GAP_SCORE -4
/** gap score */
#define GAP_SCORE -5 #define GAP_SCORE -5
/** start of word score */
#define WORD_START_SCORE 50 #define WORD_START_SCORE 50
/** non-word score */
#define NON_WORD_SCORE 40 #define NON_WORD_SCORE 40
/** CamelCase score */
#define CAMEL_SCORE ( WORD_START_SCORE + GAP_SCORE - 1 ) #define CAMEL_SCORE ( WORD_START_SCORE + GAP_SCORE - 1 )
/** Consecutive score */
#define CONSECUTIVE_SCORE ( WORD_START_SCORE + GAP_SCORE ) #define CONSECUTIVE_SCORE ( WORD_START_SCORE + GAP_SCORE )
/** non-start multiplier */
#define PATTERN_NON_START_MULTIPLIER 1 #define PATTERN_NON_START_MULTIPLIER 1
/** start multiplier */
#define PATTERN_START_MULTIPLIER 2 #define PATTERN_START_MULTIPLIER 2
#include "xcb.h" #include "xcb.h"
@ -552,59 +562,107 @@ static void rofi_view_call_thread ( gpointer data, gpointer user_data )
g_mutex_unlock ( t->mutex ); g_mutex_unlock ( t->mutex );
} }
enum CharClass { LOWER, UPPER, DIGIT, NON_WORD }; /**
* Character classification.
*/
enum CharClass
{
/* Lower case */
LOWER,
/* Upper case */
UPPER,
/* Number */
DIGIT,
/* non word character */
NON_WORD
};
/**
* @param c The character to determine class of
*
* @returns the class of the character c.
*/
static enum CharClass rofi_scorer_get_character_class ( gunichar c ) static enum CharClass rofi_scorer_get_character_class ( gunichar c )
{ {
if (g_unichar_islower(c)) if ( g_unichar_islower ( c ) ) {
return LOWER; return LOWER;
if (g_unichar_isupper(c)) }
if ( g_unichar_isupper ( c ) ) {
return UPPER; return UPPER;
if (g_unichar_isdigit(c)) }
if ( g_unichar_isdigit ( c ) ) {
return DIGIT; return DIGIT;
}
return NON_WORD; return NON_WORD;
} }
/**
* @param prev The previous character.
* @param curr The current character
*
* Scrore the transition.
*
* @returns score of the transition.
*/
static int rofi_scorer_get_score_for ( enum CharClass prev, enum CharClass curr ) static int rofi_scorer_get_score_for ( enum CharClass prev, enum CharClass curr )
{ {
if (prev == NON_WORD && curr != NON_WORD) if ( prev == NON_WORD && curr != NON_WORD ) {
return WORD_START_SCORE; return WORD_START_SCORE;
}
if ( ( prev == LOWER && curr == UPPER ) || if ( ( prev == LOWER && curr == UPPER ) ||
(prev != DIGIT && curr == DIGIT)) ( prev != DIGIT && curr == DIGIT ) ) {
return CAMEL_SCORE; return CAMEL_SCORE;
if (curr == NON_WORD) }
if ( curr == NON_WORD ) {
return NON_WORD_SCORE; return NON_WORD_SCORE;
}
return 0; return 0;
} }
/* /**
rofi_scorer_fuzzy_evaluate implements a global sequence alignment algorithm to find the maximum accumulated score by aligning `pattern` to `str`. It applies when `pattern` is a subsequence of `str`. * @param pattern The user input to match against.
* @param plen Pattern length.
Scoring criteria * @param str The input to match against pattern.
- Prefer matches at the start of a word, or the start of subwords in CamelCase/camelCase/camel123 words. See WORD_START_SCORE/CAMEL_SCORE. * @param slen Lenght of str.
- Non-word characters matter. See NON_WORD_SCORE. *
- The first characters of words of `pattern` receive bonus because they usually have more significance than the rest. See PATTERN_START_MULTIPLIER/PATTERN_NON_START_MULTIPLIER. * rofi_scorer_fuzzy_evaluate implements a global sequence alignment algorithm to find the maximum accumulated score by
- Superfluous characters in `str` will reduce the score (gap penalty). See GAP_SCORE. * aligning `pattern` to `str`. It applies when `pattern` is a subsequence of `str`.
- Prefer early occurrence of the first character. See LEADING_GAP_SCORE/GAP_SCORE. *
* Scoring criteria
The recurrence of the dynamic programming: * - Prefer matches at the start of a word, or the start of subwords in CamelCase/camelCase/camel123 words. See WORD_START_SCORE/CAMEL_SCORE.
dp[i][j]: maximum accumulated score by aligning pattern[0..i] to str[0..j] * - Non-word characters matter. See NON_WORD_SCORE.
dp[0][j] = leading_gap_penalty(0, j) + score[j] * - The first characters of words of `pattern` receive bonus because they usually have more significance than the rest.
dp[i][j] = max(dp[i-1][j-1] + CONSECUTIVE_SCORE, max(dp[i-1][k] + gap_penalty(k+1, j) + score[j] : k < j)) * See PATTERN_START_MULTIPLIER/PATTERN_NON_START_MULTIPLIER.
* - Superfluous characters in `str` will reduce the score (gap penalty). See GAP_SCORE.
The first dimension can be suppressed since we do not need a matching scheme, which reduces the space complexity from O(N*M) to O(M) * - Prefer early occurrence of the first character. See LEADING_GAP_SCORE/GAP_SCORE.
*
* The recurrence of the dynamic programming:
* dp[i][j]: maximum accumulated score by aligning pattern[0..i] to str[0..j]
* dp[0][j] = leading_gap_penalty(0, j) + score[j]
* dp[i][j] = max(dp[i-1][j-1] + CONSECUTIVE_SCORE, max(dp[i-1][k] + gap_penalty(k+1, j) + score[j] : k < j))
*
* The first dimension can be suppressed since we do not need a matching scheme, which reduces the space complexity from
* O(N*M) to O(M)
*
* @returns the sorting weight.
*/ */
static int rofi_scorer_fuzzy_evaluate ( const char *pattern, glong plen, const char *str, glong slen ) static int rofi_scorer_fuzzy_evaluate ( const char *pattern, glong plen, const char *str, glong slen )
{ {
if (plen == 5) if ( plen == 5 ) {
plen = plen; plen = plen;
}
glong pi, si; glong pi, si;
gboolean pfirst = TRUE, // whether we are aligning the first character of pattern // whether we are aligning the first character of pattern
pstart = TRUE; // whether the start of a word in pattern gboolean pfirst = TRUE;
int *score = g_malloc_n(slen, sizeof(int)), // score for each position // whether the start of a word in pattern
*dp = g_malloc_n(slen, sizeof(int)), // dp[i]: maximum value by aligning pattern[0..pi] to str[0..si] gboolean pstart = TRUE;
uleft = 0, ulefts = 0, // uleft: value of the upper left cell; ulefts: maximum value of uleft and cells on the left. The arbitrary initial values suppress warnings. // score for each position
left, lefts; // uleft & ulefts for the next row int *score = g_malloc_n ( slen, sizeof ( int ) );
// dp[i]: maximum value by aligning pattern[0..pi] to str[0..si]
int *dp = g_malloc_n ( slen, sizeof ( int ) );
// uleft: value of the upper left cell; ulefts: maximum value of uleft and cells on the left. The arbitrary initial
// values suppress warnings.
int uleft = 0, ulefts = 0, left, lefts;
const gchar *pit = pattern, *sit; const gchar *pit = pattern, *sit;
enum CharClass prev = NON_WORD, cur; enum CharClass prev = NON_WORD, cur;
for ( si = 0, sit = str; si < slen; si++, sit = g_utf8_next_char ( sit ) ) { for ( si = 0, sit = str; si < slen; si++, sit = g_utf8_next_char ( sit ) ) {
@ -631,7 +689,8 @@ static int rofi_scorer_fuzzy_evaluate(const char *pattern, glong plen, const cha
dp[si] = pfirst dp[si] = pfirst
? LEADING_GAP_SCORE * si + t ? LEADING_GAP_SCORE * si + t
: MAX ( uleft + CONSECUTIVE_SCORE, ulefts + t ); : MAX ( uleft + CONSECUTIVE_SCORE, ulefts + t );
} else { }
else {
dp[si] = MIN_SCORE; dp[si] = MIN_SCORE;
} }
uleft = left; uleft = left;
@ -640,8 +699,9 @@ static int rofi_scorer_fuzzy_evaluate(const char *pattern, glong plen, const cha
pfirst = pstart = FALSE; pfirst = pstart = FALSE;
} }
lefts = MIN_SCORE; lefts = MIN_SCORE;
for (si = 0; si < slen; si++) for ( si = 0; si < slen; si++ ) {
lefts = MAX ( lefts + GAP_SCORE, dp[si] ); lefts = MAX ( lefts + GAP_SCORE, dp[si] );
}
g_free ( score ); g_free ( score );
g_free ( dp ); g_free ( dp );
return lefts; return lefts;
@ -650,7 +710,7 @@ static int rofi_scorer_fuzzy_evaluate(const char *pattern, glong plen, const cha
static void filter_elements ( thread_state *t, G_GNUC_UNUSED gpointer user_data ) static void filter_elements ( thread_state *t, G_GNUC_UNUSED gpointer user_data )
{ {
char *pattern = NULL; char *pattern = NULL;
glong plen; glong plen = 0;
if ( config.matching_method == MM_FUZZY || config.levenshtein_sort ) { if ( config.matching_method == MM_FUZZY || config.levenshtein_sort ) {
pattern = mode_preprocess_input ( t->state->sw, t->state->text->text ); pattern = mode_preprocess_input ( t->state->sw, t->state->text->text );
plen = g_utf8_strlen ( pattern, -1 ); plen = g_utf8_strlen ( pattern, -1 );
@ -667,7 +727,8 @@ static void filter_elements ( thread_state *t, G_GNUC_UNUSED gpointer user_data
? -MIN_SCORE ? -MIN_SCORE
: -rofi_scorer_fuzzy_evaluate ( pattern, plen, str, slen ); : -rofi_scorer_fuzzy_evaluate ( pattern, plen, str, slen );
g_free ( str ); g_free ( str );
} else if ( config.levenshtein_sort ) { }
else if ( config.levenshtein_sort ) {
// This is inefficient, need to fix it. // This is inefficient, need to fix it.
char * str = mode_get_completion ( t->state->sw, i ); char * str = mode_get_completion ( t->state->sw, i );
t->state->distance[i] = levenshtein ( pattern, str ); t->state->distance[i] = levenshtein ( pattern, str );