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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#![allow(dead_code)]


#[macro_use]
pub extern crate serde_derive;


use std::collections::HashMap;
use std::cmp::Ordering;

#[cfg(test)]
mod tests {

    use MockKDTree;
    use KDTree;
    use MockEntity;

    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }



    #[test]
    fn mock_works() {
        let tree = MockKDTree::build(
            &vec![
                MockEntity {
                    id: 1,
                    txt: "Apple".to_string(),
                },
                MockEntity {
                    id: 2,
                    txt: "Banana".to_string(),
                },
                MockEntity {
                    id: 3,
                    txt: "Lettuce".to_string(),
                },
            ],
            true,
        );

        let first_result = tree.search("an");
        assert_eq!(first_result.len(), 1);
        assert_eq!(first_result[0].id, 2);
        assert_eq!(first_result[0].index, 1);

        let second_result = tree.search("e");
        assert_eq!(second_result.len(), 2);
        assert_eq!(second_result[0].id, 1);
        assert_eq!(second_result[0].index, 4);
        assert_eq!(second_result[1].id, 3);
        assert_eq!(second_result[1].index, 1);
    }


    #[test]
    fn mock_case_insensitive_works() {
        let tree = MockKDTree::build(
            &vec![
                MockEntity {
                    id: 1,
                    txt: "Apple".to_string(),
                },
                MockEntity {
                    id: 2,
                    txt: "Banana".to_string(),
                },
                MockEntity {
                    id: 3,
                    txt: "Lettuce".to_string(),
                },
            ],
            false,
        );

        let first_result = tree.search("A");
        assert_eq!(first_result.len(), 2);
        println!("{:?}", first_result);
        assert_eq!(first_result[0].id, 1);
        assert_eq!(first_result[0].index, 0);
        assert_eq!(first_result[1].id, 2);
        assert_eq!(first_result[1].index, 1);

        let second_result = tree.search("a");
        assert_eq!(second_result.len(), 2);
        println!("{:?}", first_result);
        assert_eq!(second_result[0].id, 1);
        assert_eq!(second_result[0].index, 0);
        assert_eq!(second_result[1].id, 2);
        assert_eq!(second_result[1].index, 1);
    }
}


#[derive(Debug, Serialize, Deserialize)]
pub struct MockEntity {
    pub id: u32,
    pub txt: String,
}

impl SearchableElement for MockEntity {
    fn as_searchable_text(&self) -> String {
        return self.txt.to_string();
    }

    fn get_id(&self) -> u32 {
        return self.id;
    }
}



pub trait SearchableElement {
    fn as_searchable_text(&self) -> String;
    fn get_id(&self) -> u32;
}

#[derive(Debug, Eq, PartialEq, PartialOrd)]
pub struct SearchResult {
    pub id: u32,
    pub index: usize,
}


impl std::cmp::Ord for SearchResult {
    fn cmp(&self, other: &Self) -> Ordering {
        let idcmp = self.id.cmp(&other.id);
        if idcmp == Ordering::Equal {
            return self.index.cmp(&other.index);
        } else {
            return idcmp;
        }
    }
}



pub trait KDTree {
    fn build<T: SearchableElement>(elements: &Vec<T>, case_sensitive: bool) -> Self;
    fn search(&self, query: &str) -> Vec<SearchResult>;
    fn is_case_sensitive(&self) -> bool;
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MockKDTree {
    elements: HashMap<u32, String>,
    case_sensitive: bool,
}

impl KDTree for MockKDTree {
    fn build<T: SearchableElement>(elements: &Vec<T>, case_sensitive: bool) -> Self {
        let mut tree = MockKDTree {
            elements: HashMap::new(),
            case_sensitive: case_sensitive,
        };
        for element in elements {
            tree.elements.insert(
                element.get_id(),
                if case_sensitive {
                    element.as_searchable_text()
                } else {
                    element.as_searchable_text().to_lowercase()
                },
            );
        }
        return tree;
    }

    fn search(&self, query: &str) -> Vec<SearchResult> {
        let mut results: Vec<SearchResult> = Vec::new();

        let query = if self.is_case_sensitive() {
            query.to_string()
        } else {
            query.to_lowercase()
        };

        for (id, string) in &self.elements {
            let index_opt = string.find(&query);

            if let Some(idx) = index_opt {
                results.push(SearchResult {
                    id: *id,
                    index: idx,
                });
            }
        }
        results.sort();
        return results;
    }
    fn is_case_sensitive(&self) -> bool {
        return self.case_sensitive;
    }
}