1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

move hashmap to its own module for now

This commit is contained in:
Alexander Medvednikov 2019-12-22 00:38:43 +03:00
parent 4fc8842edb
commit 2b9392c46c
4 changed files with 41 additions and 39 deletions

View File

@ -36,7 +36,7 @@ TCCREPO := https://github.com/vlang/tccbin_win
VCFILE := v_win.c VCFILE := v_win.c
endif endif
all: latest_vc latest_tcc all: latest_vc
ifdef WIN32 ifdef WIN32
$(CC) -std=c99 -w -o v0.exe $(TMPVC)/$(VCFILE) $(LDFLAGS) $(CC) -std=c99 -w -o v0.exe $(TMPVC)/$(VCFILE) $(LDFLAGS)
./v0.exe -o v.exe v.v ./v0.exe -o v.exe v.v
@ -70,19 +70,19 @@ fresh_vc:
rm -rf $(TMPVC) rm -rf $(TMPVC)
$(GITFASTCLONE) $(VCREPO) $(TMPVC) $(GITFASTCLONE) $(VCREPO) $(TMPVC)
latest_tcc: $(TMPTCC)/.git/config #latest_tcc: $(TMPTCC)/.git/config
ifndef ANDROID #ifndef ANDROID
cd $(TMPTCC) && $(GITCLEANPULL) #cd $(TMPTCC) && $(GITCLEANPULL)
endif #endif
fresh_tcc: #fresh_tcc:
ifndef ANDROID #ifndef ANDROID
rm -rf $(TMPTCC) #rm -rf $(TMPTCC)
$(GITFASTCLONE) $(TCCREPO) $(TMPTCC) #$(GITFASTCLONE) $(TCCREPO) $(TMPTCC)
endif #endif
$(TMPTCC)/.git/config: #$(TMPTCC)/.git/config:
$(MAKE) fresh_tcc #$(MAKE) fresh_tcc
$(TMPVC)/.git/config: $(TMPVC)/.git/config:
$(MAKE) fresh_vc $(MAKE) fresh_vc

View File

@ -1,10 +1,10 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module builtin module hashmap
/* /*
This is work in progress. This is work in progress.
A very early test version of the hashmap with a fixed size. A very early test version of the Hashmap with a fixed size.
Only works with string keys and int values for now. Only works with string keys and int values for now.
I added this to improve performance of the V compiler, I added this to improve performance of the V compiler,
@ -13,19 +13,20 @@ module builtin
*/ */
struct hashmap { struct Hashmap {
cap int cap int
keys []string keys []string
table []hashmapentry table []Hashmapentry
elm_size int elm_size int
pub: pub mut:
nr_collisions int nr_collisions int
} }
struct hashmapentry { struct Hashmapentry {
mut:
key string key string
val int val int
next &hashmapentry // linked list for collisions next &Hashmapentry // linked list for collisions
} }
const ( const (
@ -35,7 +36,7 @@ const (
const( const(
fnv64_prime = 1099511628211 fnv64_prime = 1099511628211
fnv64_offset_basis = 14695981039346656037 fnv64_offset_basis = u64(14695981039346656037)
) )
const( const(
@ -43,7 +44,7 @@ const(
fnv32_prime = u32(16777619) fnv32_prime = u32(16777619)
) )
pub fn new_hashmap(planned_nr_items int) hashmap { pub fn new_hashmap(planned_nr_items int) Hashmap {
mut cap := planned_nr_items * 5 mut cap := planned_nr_items * 5
if cap < min_cap { if cap < min_cap {
cap = min_cap cap = min_cap
@ -51,14 +52,14 @@ pub fn new_hashmap(planned_nr_items int) hashmap {
if cap > max_cap { if cap > max_cap {
cap = max_cap cap = max_cap
} }
return hashmap{ return Hashmap{
cap: cap cap: cap
elm_size: 4 elm_size: 4
table: make(cap, cap, sizeof(hashmapentry)) table: make(cap, cap, sizeof(Hashmapentry))
} }
} }
pub fn (m mut hashmap) set(key string, val int) { pub fn (m mut Hashmap) set(key string, val int) {
// mut hash := int(b_fabs(key.hash())) // mut hash := int(b_fabs(key.hash()))
// idx := hash % m.cap // idx := hash % m.cap
idx := int(fnv1a32(key) % m.cap) idx := int(fnv1a32(key) % m.cap)
@ -70,16 +71,16 @@ pub fn (m mut hashmap) set(key string, val int) {
for e.next != 0 { for e.next != 0 {
e = e.next e = e.next
} }
e.next = &hashmapentry{ e.next = &Hashmapentry{
key,val,0} key,val,0}
} }
else { else {
m.table[idx] = hashmapentry{ m.table[idx] = Hashmapentry{
key,val,0} key,val,0}
} }
} }
pub fn (m mut hashmap) get(key string) int { pub fn (m &Hashmap) get(key string) int {
// mut hash := int(b_fabs(key.hash())) // mut hash := int(b_fabs(key.hash()))
// idx := hash % m.cap // idx := hash % m.cap
idx := int(fnv1a32(key) % m.cap) idx := int(fnv1a32(key) % m.cap)

View File

@ -1,3 +1,5 @@
module hashmap
import rand import rand
fn test_random_strings() { fn test_random_strings() {

View File

@ -1145,7 +1145,6 @@ pub fn vfmt(args []string) {
println('v fmt can only be used on .v files') println('v fmt can only be used on .v files')
exit(1) exit(1)
} }
println('WIP')
vexe := vexe_path() vexe := vexe_path()
// launch_tool('vfmt', '-d vfmt') // launch_tool('vfmt', '-d vfmt')
vroot := os.dir(vexe) vroot := os.dir(vexe)