Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
This commit is contained in:
commit
0049a2a388
19 changed files with 326 additions and 586 deletions
|
|
@ -10,6 +10,47 @@
|
|||
## The ``tables`` module implements an efficient hash table that is
|
||||
## a mapping from keys to values.
|
||||
##
|
||||
## If you are using simple standard types like ``int`` or ``string`` for the
|
||||
## keys of the table you won't have any problems, but as soon as you try to use
|
||||
## a more complex object as a key you will be greeted by a strange compiler
|
||||
## error::
|
||||
##
|
||||
## Error: type mismatch: got (Person)
|
||||
## but expected one of:
|
||||
## hashes.hash(x: openarray[A]): THash
|
||||
## hashes.hash(x: int): THash
|
||||
## hashes.hash(x: float): THash
|
||||
## …
|
||||
##
|
||||
## What is happening here is that the types used for table keys require to have
|
||||
## a ``hash()`` proc which will convert them to a `THash <hashes.html#THash>`_
|
||||
## value, and the compiler is listing all the hash functions it knows. After
|
||||
## you add such a proc for your custom type everything will work. See this
|
||||
## example:
|
||||
##
|
||||
## .. code-block:: nimrod
|
||||
## type
|
||||
## Person = object
|
||||
## firstName, lastName: string
|
||||
##
|
||||
## proc hash(x: Person): THash =
|
||||
## ## Piggyback on the already available string hash proc.
|
||||
## ##
|
||||
## ## Without this proc nothing works!
|
||||
## result = hash(x.firstName & x.lastName)
|
||||
##
|
||||
## var
|
||||
## salaries = initTable[Person, int]()
|
||||
## p1, p2: Person
|
||||
##
|
||||
## p1.firstName = "Jon"
|
||||
## p1.lastName = "Ross"
|
||||
## salaries[p1] = 30_000
|
||||
##
|
||||
## p2.firstName = "소진"
|
||||
## p2.lastName = "박"
|
||||
## salaries[p2] = 45_000
|
||||
##
|
||||
## **Note:** The data types declared here have *value semantics*: This means
|
||||
## that ``=`` performs a copy of the hash table.
|
||||
|
||||
|
|
@ -526,3 +567,30 @@ proc sort*[A](t: var TCountTable[A]) =
|
|||
if j < h: break
|
||||
if h == 1: break
|
||||
|
||||
when isMainModule:
|
||||
type
|
||||
Person = object
|
||||
firstName, lastName: string
|
||||
|
||||
proc hash(x: Person): THash =
|
||||
## Piggyback on the already available string hash proc.
|
||||
##
|
||||
## Without this proc nothing works!
|
||||
result = hash(x.firstName & x.lastName)
|
||||
|
||||
var
|
||||
salaries = initTable[Person, int]()
|
||||
p1, p2: Person
|
||||
p1.firstName = "Jon"
|
||||
p1.lastName = "Ross"
|
||||
salaries[p1] = 30_000
|
||||
p2.firstName = "소진"
|
||||
p2.lastName = "박"
|
||||
salaries[p2] = 45_000
|
||||
var
|
||||
s2 = initOrderedTable[Person, int]()
|
||||
s3 = initCountTable[Person]()
|
||||
s2[p1] = 30_000
|
||||
s2[p2] = 45_000
|
||||
s3[p1] = 30_000
|
||||
s3[p2] = 45_000
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
# (c) Copyright 2014 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
|
|
@ -1612,6 +1612,20 @@ when defined(linux) or defined(solaris) or defined(bsd) or defined(aix):
|
|||
len = readlink(procPath, result, len)
|
||||
setLen(result, len)
|
||||
|
||||
when not (defined(windows) or defined(macosx)):
|
||||
proc getApplHeuristic(): string =
|
||||
when defined(paramStr):
|
||||
result = string(paramStr(0))
|
||||
# POSIX guaranties that this contains the executable
|
||||
# as it has been executed by the calling process
|
||||
if len(result) > 0 and result[0] != DirSep: # not an absolute path?
|
||||
# iterate over any path in the $PATH environment variable
|
||||
for p in split(string(getEnv("PATH")), {PathSep}):
|
||||
var x = joinPath(p, result)
|
||||
if existsFile(x): return x
|
||||
else:
|
||||
result = ""
|
||||
|
||||
when defined(macosx):
|
||||
type
|
||||
cuint32* {.importc: "unsigned int", nodecl.} = int
|
||||
|
|
@ -1648,10 +1662,13 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [FReadIO].} =
|
|||
setlen(result, int(len))
|
||||
elif defined(linux) or defined(aix):
|
||||
result = getApplAux("/proc/self/exe")
|
||||
if result.len == 0: result = getApplHeuristic()
|
||||
elif defined(solaris):
|
||||
result = getApplAux("/proc/" & $getpid() & "/path/a.out")
|
||||
if result.len == 0: result = getApplHeuristic()
|
||||
elif defined(freebsd):
|
||||
result = getApplAux("/proc/" & $getpid() & "/file")
|
||||
if result.len == 0: result = getApplHeuristic()
|
||||
elif defined(macosx):
|
||||
var size: cuint32
|
||||
getExecPath1(nil, size)
|
||||
|
|
@ -1663,15 +1680,7 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [FReadIO].} =
|
|||
else:
|
||||
# little heuristic that may work on other POSIX-like systems:
|
||||
result = string(getEnv("_"))
|
||||
if len(result) == 0:
|
||||
result = string(paramStr(0))
|
||||
# POSIX guaranties that this contains the executable
|
||||
# as it has been executed by the calling process
|
||||
if len(result) > 0 and result[0] != DirSep: # not an absolute path?
|
||||
# iterate over any path in the $PATH environment variable
|
||||
for p in split(string(getEnv("PATH")), {PathSep}):
|
||||
var x = joinPath(p, result)
|
||||
if existsFile(x): return x
|
||||
if result.len == 0: result = getApplHeuristic()
|
||||
|
||||
proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} =
|
||||
## Returns the filename of the application's executable.
|
||||
|
|
|
|||
|
|
@ -234,6 +234,11 @@ template `>` * (x, y: expr): expr {.immediate.} =
|
|||
## "is greater" operator. This is the same as ``y < x``.
|
||||
y < x
|
||||
|
||||
const
|
||||
appType* {.magic: "AppType"}: string = ""
|
||||
## a string that describes the application type. Possible values:
|
||||
## "console", "gui", "lib".
|
||||
|
||||
include "system/inclrtl"
|
||||
|
||||
const NoFakeVars* = defined(NimrodVM) ## true if the backend doesn't support \
|
||||
|
|
@ -940,10 +945,6 @@ const
|
|||
## a string that describes the host CPU. Possible values:
|
||||
## "i386", "alpha", "powerpc", "sparc", "amd64", "mips", "arm".
|
||||
|
||||
appType* {.magic: "AppType"}: string = ""
|
||||
## a string that describes the application type. Possible values:
|
||||
## "console", "gui", "lib".
|
||||
|
||||
seqShallowFlag = low(int)
|
||||
|
||||
proc compileOption*(option: string): bool {.
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ proc popCurrentException {.compilerRtl, inl.} =
|
|||
|
||||
# some platforms have native support for stack traces:
|
||||
const
|
||||
nativeStackTraceSupported = (defined(macosx) or defined(linux)) and
|
||||
nativeStackTraceSupported* = (defined(macosx) or defined(linux)) and
|
||||
not nimrodStackTrace
|
||||
hasSomeStackTrace = nimrodStackTrace or
|
||||
defined(nativeStackTrace) and nativeStackTraceSupported
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2013 Andreas Rumpf
|
||||
# (c) Copyright 2014 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
|
|
|
|||
|
|
@ -106,15 +106,15 @@ type
|
|||
Pstmt* = ptr Tstmt
|
||||
Tvalue{.pure, final.} = object
|
||||
Pvalue* = ptr Tvalue
|
||||
PPValue* = ptr Pvalue
|
||||
PValueArg* = array[0..127, Pvalue]
|
||||
|
||||
Tcallback* = proc (para1: pointer, para2: int32, para3,
|
||||
para4: cstringArray): int32{.cdecl.}
|
||||
Tbind_destructor_func* = proc (para1: pointer){.cdecl.}
|
||||
Tcreate_function_step_func* = proc (para1: Pcontext, para2: int32,
|
||||
para3: PPValue){.cdecl.}
|
||||
para3: PValueArg){.cdecl.}
|
||||
Tcreate_function_func_func* = proc (para1: Pcontext, para2: int32,
|
||||
para3: PPValue){.cdecl.}
|
||||
para3: PValueArg){.cdecl.}
|
||||
Tcreate_function_final_func* = proc (para1: Pcontext){.cdecl.}
|
||||
Tresult_func* = proc (para1: pointer){.cdecl.}
|
||||
Tcreate_collation_func* = proc (para1: pointer, para2: int32, para3: pointer,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue