コード例 #1
0
/*
Populates a trie structure with the contents of a text file that
has the provided name
*/
func PopulateTrie(file string) {
	trie = wordTrie.NewTrie()
	// Open the text file
	f, err := os.Open(file)
	//f, err := os.Open(file)
	check(err)

	// Begin scanning the text file line by line, populate the trie
	scanner := bufio.NewScanner(f)

	// Initial available memory read prior to entering the while loop
	runtime.ReadMemStats(&mem)
	currentMemory := mem.Alloc

	// Add entries to the trie as long as there is enough memory
	for scanner.Scan() && currentMemory < memoryLimit {
		runtime.ReadMemStats(&mem)
		currentMemory = mem.TotalAlloc
		trie.AddEntry(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "reading standard input:", err)
	}
	f.Close()

	fmt.Println(trie.GetNumberOfItems()) //TESTING
}
コード例 #2
0
func testEntry(t *testing.T) {
	var trie = wordTrie.NewTrie()
	trie.AddEntry("car")
	trie.AddEntry("cars")
	word := trie.FindEntries("c", uint8(5))
	if string(word) != "[car cars]" {
		t.Errorf("Entry not correct")
	}
}
コード例 #3
0
package readWordFile

import (
	"bufio"
	"fmt"
	"github.com/fridaysedge/info344-class-code/suggestion-service-mod/wordTrie"
	"os"
	"runtime"
	"strconv"
)

var trie = wordTrie.NewTrie()
var mem runtime.MemStats
var memoryLimit = uint64(800000000)

/*
 */
func check(e error) {
	if e != nil {
		panic(e)
	}
}

/*
Populates a trie structure with the contents of a text file that
has the provided name
*/
func PopulateTrie(file string) {
	trie = wordTrie.NewTrie()
	// Open the text file
	f, err := os.Open(file)