-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
murmur hash and time_now modules are added
- Loading branch information
Pavel Kraynyukhov
committed
Jul 8, 2018
1 parent
5ea66f8
commit c29ae09
Showing
2 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* Copyright 2017-2018, Pavel Kraynyukhov <pavel.kraynyukhov@gmail.com> | ||
* | ||
* This file is a part of LAppS (Lua Application Server). | ||
* | ||
* LAppS is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LAppS is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with LAppS. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* $Id: murmur.h July 9, 2018 1:38 AM $ | ||
* | ||
**/ | ||
|
||
|
||
#ifndef __MURMUR_H__ | ||
# define __MURMUR_H__ | ||
|
||
#include <functional> | ||
#include <string> | ||
|
||
extern "C" { | ||
#include <lua.h> | ||
#include <lualib.h> | ||
#include <lauxlib.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
LUA_API int hashstr(lua_State *L) | ||
{ | ||
int argc=lua_gettop(L); | ||
if(argc != 1) | ||
{ | ||
lua_pushfstring(L,"Usage: murmur.hashstr(number|string); returns string. args now: ",argc); | ||
lua_error(L); | ||
} | ||
int type=lua_type(L,argc); | ||
switch(type) | ||
{ | ||
case LUA_TSTRING: | ||
{ | ||
std::hash<std::string> hash; | ||
std::string result; | ||
std::string tmp(lua_tostring(L,argc)); | ||
|
||
long long unsigned int digest=hash(std::move(tmp)); | ||
result.resize(16); | ||
snprintf((char*)(result.c_str()),16,"%016llx",digest); | ||
lua_pushlstring(L,result.c_str(),result.length()); | ||
} | ||
break; | ||
case LUA_TNUMBER: | ||
{ | ||
std::hash<std::size_t> hash; | ||
std::string result; | ||
size_t tmp=static_cast<size_t>(lua_tonumber(L,argc)); | ||
long long unsigned int digest=hash(tmp); | ||
result.resize(16); | ||
snprintf((char*)(result.c_str()),16,"%016llx",digest); | ||
lua_pushlstring(L,result.c_str(),result.length()); | ||
} | ||
break; | ||
default: | ||
lua_pushstring(L,"Usage: murmur.hashstr(number|string); returns string"); | ||
lua_error(L); | ||
} | ||
return 1; | ||
} | ||
|
||
LUA_API int hashnum(lua_State *L) | ||
{ | ||
int argc=lua_gettop(L); | ||
if(argc != 1) | ||
{ | ||
lua_pushfstring(L,"Usage: murmur.hashnum(number|string); returns size_t. args now: ",argc); | ||
lua_error(L); | ||
} | ||
int type=lua_type(L,argc); | ||
switch(type) | ||
{ | ||
case LUA_TSTRING: | ||
{ | ||
std::hash<std::string> hash; | ||
std::string tmp(lua_tostring(L,argc)); | ||
|
||
long long unsigned int digest=hash(std::move(tmp)); | ||
lua_pushinteger(L,digest); | ||
} | ||
break; | ||
case LUA_TNUMBER: | ||
{ | ||
std::hash<std::size_t> hash; | ||
size_t tmp=static_cast<size_t>(lua_tonumber(L,argc)); | ||
long long unsigned int digest=hash(tmp); | ||
lua_pushinteger(L,digest); | ||
} | ||
break; | ||
default: | ||
lua_pushstring(L,"Usage: murmur.hashnum(number|string); returns size_t"); | ||
lua_error(L); | ||
} | ||
return 1; | ||
} | ||
|
||
LUA_API int luaopen_murmur(lua_State *L) | ||
{ | ||
luaL_Reg functions[]= { | ||
{"hashstr", hashstr }, | ||
{"hashnum", hashnum }, | ||
{nullptr,nullptr} | ||
}; | ||
luaL_register(L,"murmur",functions); | ||
return 1; | ||
} | ||
} | ||
|
||
|
||
#endif /* __MURMUR_H__ */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright 2017-2018, Pavel Kraynyukhov <pavel.kraynyukhov@gmail.com> | ||
* | ||
* This file is a part of LAppS (Lua Application Server). | ||
* | ||
* LAppS is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LAppS is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with LAppS. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* $Id: time_now.h July 9, 2018 1:51 AM $ | ||
* | ||
**/ | ||
|
||
|
||
#ifndef __TIME_NOW_H__ | ||
# define __TIME_NOW_H__ | ||
|
||
|
||
#include <chrono> | ||
|
||
extern "C" { | ||
#include <lua.h> | ||
#include <lualib.h> | ||
#include <lauxlib.h> | ||
|
||
LUA_API int now(lua_State *L) | ||
{ | ||
|
||
uint64_t now=std::chrono::duration_cast<std::chrono::milliseconds>( | ||
std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch() | ||
).count(); | ||
|
||
lua_pushinteger(L,now); | ||
return 1; | ||
} | ||
|
||
LUA_API int luaopen_time(lua_State *L) | ||
{ | ||
luaL_Reg functions[]= { | ||
{"now", now }, | ||
{nullptr,nullptr} | ||
}; | ||
luaL_register(L,"time",functions); | ||
return 1; | ||
} | ||
} | ||
|
||
|
||
#endif /* __TIME_NOW_H__ */ | ||
|