1 /* 2 * This file is part of d-dazzle. 3 * 4 * d-dazzle is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * d-dazzle is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with d-dazzle; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 module dazzle.FuzzyMutableIndex; 20 21 private import dazzle.c.functions; 22 public import dazzle.c.types; 23 private import glib.ArrayG; 24 private import glib.ConstructionException; 25 private import glib.Str; 26 private import gobject.ObjectG; 27 private import gtkd.Loader; 28 29 30 /** */ 31 public class FuzzyMutableIndex 32 { 33 /** the main Gtk struct */ 34 protected DzlFuzzyMutableIndex* dzlFuzzyMutableIndex; 35 protected bool ownedRef; 36 37 /** Get the main Gtk struct */ 38 public DzlFuzzyMutableIndex* getFuzzyMutableIndexStruct(bool transferOwnership = false) 39 { 40 if (transferOwnership) 41 ownedRef = false; 42 return dzlFuzzyMutableIndex; 43 } 44 45 /** the main Gtk struct as a void* */ 46 protected void* getStruct() 47 { 48 return cast(void*)dzlFuzzyMutableIndex; 49 } 50 51 /** 52 * Sets our main struct and passes it to the parent class. 53 */ 54 public this (DzlFuzzyMutableIndex* dzlFuzzyMutableIndex, bool ownedRef = false) 55 { 56 this.dzlFuzzyMutableIndex = dzlFuzzyMutableIndex; 57 this.ownedRef = ownedRef; 58 } 59 60 ~this () 61 { 62 if ( Linker.isLoaded(LIBRARY_DAZZLE) && ownedRef ) 63 dzl_fuzzy_mutable_index_unref(dzlFuzzyMutableIndex); 64 } 65 66 67 /** */ 68 public static GType getType() 69 { 70 return dzl_fuzzy_mutable_index_get_type(); 71 } 72 73 /** 74 * Create a new #Fuzzy for fuzzy matching strings. 75 * 76 * Params: 77 * caseSensitive = %TRUE if case should be preserved. 78 * 79 * Returns: A newly allocated #Fuzzy that should be freed with dzl_fuzzy_mutable_index_unref(). 80 * 81 * Throws: ConstructionException GTK+ fails to create the object. 82 */ 83 public this(bool caseSensitive) 84 { 85 auto p = dzl_fuzzy_mutable_index_new(caseSensitive); 86 87 if(p is null) 88 { 89 throw new ConstructionException("null returned by new"); 90 } 91 92 this(cast(DzlFuzzyMutableIndex*) p); 93 } 94 95 /** */ 96 public this(bool caseSensitive, GDestroyNotify freeFunc) 97 { 98 auto p = dzl_fuzzy_mutable_index_new_with_free_func(caseSensitive, freeFunc); 99 100 if(p is null) 101 { 102 throw new ConstructionException("null returned by new_with_free_func"); 103 } 104 105 this(cast(DzlFuzzyMutableIndex*) p); 106 } 107 108 /** 109 * Start a bulk insertion. @fuzzy is not ready for searching until 110 * dzl_fuzzy_mutable_index_end_bulk_insert() has been called. 111 * 112 * This allows for inserting large numbers of strings and deferring 113 * the final sort until dzl_fuzzy_mutable_index_end_bulk_insert(). 114 */ 115 public void beginBulkInsert() 116 { 117 dzl_fuzzy_mutable_index_begin_bulk_insert(dzlFuzzyMutableIndex); 118 } 119 120 /** */ 121 public bool contains(string key) 122 { 123 return dzl_fuzzy_mutable_index_contains(dzlFuzzyMutableIndex, Str.toStringz(key)) != 0; 124 } 125 126 /** 127 * Complete a bulk insert and resort the index. 128 */ 129 public void endBulkInsert() 130 { 131 dzl_fuzzy_mutable_index_end_bulk_insert(dzlFuzzyMutableIndex); 132 } 133 134 /** 135 * Inserts a string into the fuzzy matcher. 136 * 137 * Params: 138 * key = A UTF-8 encoded string. 139 * value = A value to associate with key. 140 */ 141 public void insert(string key, void* value) 142 { 143 dzl_fuzzy_mutable_index_insert(dzlFuzzyMutableIndex, Str.toStringz(key), value); 144 } 145 146 /** 147 * DzlFuzzyMutableIndex searches within @fuzzy for strings that fuzzy match @needle. 148 * Only up to @max_matches will be returned. 149 * 150 * TODO: max_matches is not yet respected. 151 * 152 * Params: 153 * needle = The needle to fuzzy search for. 154 * maxMatches = The max number of matches to return. 155 * 156 * Returns: A newly allocated 157 * #GArray containing #FuzzyMatch elements. This should be freed when 158 * the caller is done with it using g_array_unref(). 159 * It is a programming error to keep the structure around longer than 160 * the @fuzzy instance. 161 */ 162 public ArrayG match(string needle, size_t maxMatches) 163 { 164 auto p = dzl_fuzzy_mutable_index_match(dzlFuzzyMutableIndex, Str.toStringz(needle), maxMatches); 165 166 if(p is null) 167 { 168 return null; 169 } 170 171 return new ArrayG(cast(GArray*) p, true); 172 } 173 174 alias doref = ref_; 175 /** */ 176 public FuzzyMutableIndex ref_() 177 { 178 auto p = dzl_fuzzy_mutable_index_ref(dzlFuzzyMutableIndex); 179 180 if(p is null) 181 { 182 return null; 183 } 184 185 return ObjectG.getDObject!(FuzzyMutableIndex)(cast(DzlFuzzyMutableIndex*) p, true); 186 } 187 188 /** */ 189 public void remove(string key) 190 { 191 dzl_fuzzy_mutable_index_remove(dzlFuzzyMutableIndex, Str.toStringz(key)); 192 } 193 194 /** */ 195 public void setFreeFunc(GDestroyNotify freeFunc) 196 { 197 dzl_fuzzy_mutable_index_set_free_func(dzlFuzzyMutableIndex, freeFunc); 198 } 199 200 /** 201 * Decrements the reference count of fuzzy by one. When the reference count 202 * reaches zero, the structure will be freed. 203 */ 204 public void unref() 205 { 206 dzl_fuzzy_mutable_index_unref(dzlFuzzyMutableIndex); 207 } 208 }