Socket
Socket
Sign inDemoInstall

github.com/karlseguin/autocomplete

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/karlseguin/autocomplete

Thread-safe autocomplete for english Simple bytepool for Nabu


Version published

Readme

Source

AutoComplete

A trie written in Go optimized for autocomplete of english words/phrases.

The trie uses a coarse read-write mutex to ensure thread-safety.

Each node contains all possible matching ids, making retrieval fast (O(N) where N is the size of the input) but not memory efficient. Nevertheless, 2800 video game titles takes roughly 12MB. Normalization of input is optimized for english words. The sentence

I Like <3Cats<3

will be indexed with the following three values:

ilikecats
likecats
cats

This has proved a simple but effective approach.

Insert performance is nothing to write home about. Delete performance is poor (reducing the coarsness of the lock would help).

Usage

You interact with the trie via the Insert, Find and Remove methods:

titles := map[string]string {
  "16": "3d Body Adventure",
  "409": "Bit Bat",
  "796": "Double Dragon II The Revenge",
  "1455": "M1 Tank Platoon",
  "1776": "Panza Kick Boxing",
  "2021": "Romance Of The Three Kingdoms 3",
  "2455": "Terminal Velocity Cdrom",
  "2688": "Warriors Of Legend",
  "2802": "Yatzy",
}

//100 is the maximum title size we should worry about
ac := autocomplete.New(100)

//insert will update the value when the id has already been seen
for id, title := range titles {
  ac.Insert(id, title)
}

ids := ac.Find("ki")
//ids == ["1776", "2021"] (order might be different)

//remove the item by id
ac.Remove("2802")

FAQs

Last updated on 15 Aug 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc