OSDN Git Service

Add some lisp functions.
authorvisor <visor@users.sourceforge.jp>
Sun, 13 Dec 2009 10:22:17 +0000 (19:22 +0900)
committervisor <visor@users.sourceforge.jp>
Sun, 13 Dec 2009 10:22:17 +0000 (19:22 +0900)
lib/util_const.cc
lib/util_random.cc
lib/util_random.h
modules/ml-db.cc
modules/ml-db.h
modules/ml-math.cc
modules/ml-math.h
modules/ml-string.cc
modules/ml-string.h

index 72f79c8..5f2dbad 100644 (file)
@@ -35,7 +35,7 @@ ustring  uNil2 (CharConst ("()"));
 ustring  uLF (CharConst ("\n"));
 ustring  uCRLF (CharConst ("\r\n"));
 ustring  uSPC (CharConst (" "));
-ustring  uR (CharConst ("R"));
+ustring  uR (CharConst ("_R"));
 ustring  uDash (CharConst ("-"));
 ustring  uSlash (CharConst ("/"));
 //ustring  uSlash2 (CharConst ("//"));
index dc80947..044d713 100644 (file)
@@ -10,7 +10,7 @@
 
 static int  Inited = 0;
 static unsigned long  Seed;
-static ustring  RChar (CharConst ("ABCDEFGHJKLMNPQRTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"));
+//static ustring  RChar (CharConst ("ABCDEFGHJKLMNPQRTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"));
 static ustring  SaltChar (CharConst ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
 
 static void  init () {
@@ -26,41 +26,16 @@ static void  init () {
 
 static void  enc (unsigned long v, char* b) {
     int  i;
-    int  n = RChar.size ();
+//    int  n = RChar.size ();
+    int  n = SaltChar.size ();
 
     for (i = 0; i < 5; i ++) {
-       b[i] = RChar[v % n];
+//     b[i] = RChar[v % n];
+       b[i] = SaltChar[v % n];
        v /= n;
     }
 }
 
-#if 0
-ustring  randomKey () {
-    char  b[128];
-    ustring  ans;
-    ssize_t  s;
-
-    if (! Inited)
-       init ();
-    s = snprintf (b, 128, "%.8x%.8x%.8x%.8x", random (), random (), random (), random ());
-    ans = ustring (b, s);
-    return ans;
-}
-#endif
-#if 0
-ustring  smallRandomKey () {
-    char  b[128];
-    ustring  ans;
-    ssize_t  s;
-
-    if (! Inited)
-       init ();
-    s = snprintf (b, 128, "%.8x%.8x", random (), random ());
-    ans = ustring (b, s);
-    return ans;
-}
-#endif
-
 ustring  randomKey () {
     char  b[32];
 
@@ -114,3 +89,9 @@ ustring  makeSalt () {
     b[11] = '$';
     return ustring (b, 12);
 }
+
+double  randDouble () { 
+    if (! Inited)
+       init ();
+    return random () / double (0x80000000UL);
+} 
index a57c5e6..c40c7f7 100644 (file)
@@ -7,5 +7,6 @@ ustring  randomKey ();
 ustring  randomKey (unsigned long n);
 ustring  smallRandomKey ();
 ustring  makeSalt ();
+double  randDouble ();
 
 #endif /* UTIL_RANDOM_H */
index fcc72a5..91d3309 100644 (file)
@@ -205,6 +205,29 @@ MNode*  ml_db_x_write (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     return NULL;
 }
 
+/*DOC:
+====delete====
+ (delete KEY) -> NIL
+
+*/
+//#SFUNC       delete  ml_db_x_delete  $db-rw
+MNode*  ml_db_x_delete (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
+    MNode*  arg = cell->cdr ();
+    MLDb*  obj = MObjRef<MLDb> (mobj, cMLDbID);
+    ustring  key;
+
+    if (!arg) 
+       throw (uErrorWrongNumber);
+    key = eval_str (arg->car (), mlenv);
+    nextNode (arg);
+    if (arg) 
+       throw (uErrorWrongNumber);
+
+    obj->db.del (key);
+
+    return NULL;
+}
+
 static void  set_read_vars (ustring& val, std::vector<ustring>& vars, std::vector<int>& type, MlEnv* mlenv) {
     if (vars.size () > 0) {
        uiterator  b = val.begin ();
index ce1b47c..cfdbfea 100644 (file)
@@ -33,6 +33,7 @@ MNode*  ml_db_read (MNode* cell, MlEnv* mlenv);
 MNode*  ml_db_rw (MNode* cell, MlEnv* mlenv);
 MNode*  ml_db_x_read (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
 MNode*  ml_db_x_write (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
+MNode*  ml_db_x_delete (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
 MNode*  ml_db_x_read_array (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
 MNode*  ml_db_x_write_array (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
 MNode*  ml_db_x_select (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
index cc87250..52ca4aa 100644 (file)
@@ -4,6 +4,7 @@
 #include "motorenv.h"
 #include "expr.h"
 #include "util_const.h"
+#include "util_random.h"
 #include <exception>
 #include <math.h>
 
@@ -180,3 +181,19 @@ MNode*  ml_fraction (MNode* cell, MlEnv* mlenv) {
     double  v = arg1_double (cell, mlenv);
     return newMNode_num (v - floor (v));
 }
+
+/*DOC:
+===random===
+ (random) -> NUMBER
+
+*/
+//#AFUNC       random  ml_random
+//#WIKIFUNC    random  ml_random
+MNode*  ml_random (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+
+    if (arg)
+       throw (uErrorWrongNumber);
+
+    return newMNode_num (randDouble ());
+}
index 31f99a4..fa3f05c 100644 (file)
@@ -12,5 +12,6 @@ MNode*  ml_mod (MNode* cell, MlEnv* mlenv);
 MNode*  ml_ceil (MNode* cell, MlEnv* mlenv);
 MNode*  ml_floor (MNode* cell, MlEnv* mlenv);
 MNode*  ml_fraction (MNode* cell, MlEnv* mlenv);
+MNode*  ml_random (MNode* cell, MlEnv* mlenv);
 
 #endif /* ML_MATH_H */
index 67663ef..5215425 100644 (file)
@@ -4,6 +4,7 @@
 #include "motorenv.h"
 #include "motoroutput.h"
 #include "util_const.h"
+#include "util_random.h"
 #include "util_string.h"
 #include "expr.h"
 #include "utf8.h"
@@ -591,3 +592,19 @@ MNode*  ml_string_format (MNode* cell, MlEnv* mlenv) {
 
     return newMNode_str (new ustring (formatString (format, par)));
 }
+
+/*DOC:
+===random-key===
+ (random-key) -> STRING
+
+*/
+//#AFUNC       random-key      ml_random_key
+//#WIKIFUNC    random-key      ml_random_key
+MNode*  ml_random_key (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+
+    if (arg)
+       throw (uErrorWrongNumber);
+
+    return newMNode_str (new ustring (randomKey ()));
+}
index eee2d3d..2ed1101 100644 (file)
@@ -21,5 +21,6 @@ MNode*  ml_length (MNode* cell, MlEnv* mlenv);
 MNode*  ml_pad0 (MNode* cell, MlEnv* mlenv);
 MNode*  ml_ellipsis (MNode* cell, MlEnv* mlenv);
 MNode*  ml_string_format (MNode* cell, MlEnv* mlenv);
+MNode*  ml_random_key (MNode* cell, MlEnv* mlenv);
 
 #endif /* ML_STRING_H */