From 122f67c45252c4a5ff492e6dbaaee33990360d06 Mon Sep 17 00:00:00 2001 From: Jim Hester Date: Tue, 17 Jul 2018 07:26:59 -0400 Subject: [PATCH 1/7] Remove snap --- inst/include/Credits.txt | 60 - inst/include/Makefile | 20 - inst/include/Makefile.config | 61 - inst/include/RcppSNAP.h | 14 - inst/include/Version | 1 - inst/include/glib-core/base.cpp | 110 - inst/include/glib-core/base.h | 217 - inst/include/glib-core/bd.cpp | 139 - inst/include/glib-core/bd.h | 605 -- inst/include/glib-core/bits.cpp | 208 - inst/include/glib-core/bits.h | 400 -- inst/include/glib-core/blobbs.cpp | 580 -- inst/include/glib-core/blobbs.h | 223 - inst/include/glib-core/doc/bd.h.txt | 8 - inst/include/glib-core/doc/ds.h.txt | 209 - inst/include/glib-core/doc/ss.h.txt | 32 - inst/include/glib-core/doc/ssmp.h.txt | 32 - inst/include/glib-core/ds.h | 2821 --------- inst/include/glib-core/dt.cpp | 2333 ------- inst/include/glib-core/dt.h | 1617 ----- inst/include/glib-core/env.cpp | 298 - inst/include/glib-core/env.h | 74 - inst/include/glib-core/fl.cpp | 1417 ----- inst/include/glib-core/fl.h | 660 -- inst/include/glib-core/gnuplot.cpp | 894 --- inst/include/glib-core/gnuplot.h | 666 -- inst/include/glib-core/hash.cpp | 137 - inst/include/glib-core/hash.h | 1285 ---- inst/include/glib-core/hashmp.h | 571 -- inst/include/glib-core/html.cpp | 1325 ---- inst/include/glib-core/html.h | 383 -- inst/include/glib-core/http.cpp | 865 --- inst/include/glib-core/http.h | 212 - inst/include/glib-core/json.cpp | 285 - inst/include/glib-core/json.h | 118 - inst/include/glib-core/linalg.cpp | 1813 ------ inst/include/glib-core/linalg.h | 568 -- inst/include/glib-core/lx.cpp | 947 --- inst/include/glib-core/lx.h | 392 -- inst/include/glib-core/md5.cpp | 286 - inst/include/glib-core/md5.h | 116 - inst/include/glib-core/os.cpp | 867 --- inst/include/glib-core/os.h | 264 - inst/include/glib-core/priorityqueue.h | 129 - inst/include/glib-core/shash.h | 1475 ----- inst/include/glib-core/ss.cpp | 516 -- inst/include/glib-core/ss.h | 140 - inst/include/glib-core/ssmp.cpp | 189 - inst/include/glib-core/ssmp.h | 97 - inst/include/glib-core/stdafx.h | 1 - inst/include/glib-core/tm.cpp | 1316 ---- inst/include/glib-core/tm.h | 446 -- inst/include/glib-core/unicode.cpp | 1730 ------ inst/include/glib-core/unicode.h | 3323 ---------- inst/include/glib-core/unicodestring.cpp | 161 - inst/include/glib-core/unicodestring.h | 91 - inst/include/glib-core/url.cpp | 470 -- inst/include/glib-core/url.h | 172 - inst/include/glib-core/ut.cpp | 116 - inst/include/glib-core/ut.h | 206 - inst/include/glib-core/wch.cpp | 50 - inst/include/glib-core/wch.h | 117 - inst/include/glib-core/xfl.cpp | 293 - inst/include/glib-core/xfl.h | 102 - inst/include/glib-core/xmath.cpp | 1314 ---- inst/include/glib-core/xmath.h | 482 -- inst/include/glib-core/xml.cpp | 1564 ----- inst/include/glib-core/xml.h | 423 -- inst/include/glib-core/xmlser.h | 136 - inst/include/glib-core/zipfl.cpp | 426 -- inst/include/glib-core/zipfl.h | 111 - inst/include/snap-core/Makefile | 24 - inst/include/snap-core/Snap.cpp | 50 - inst/include/snap-core/Snap.h | 58 - inst/include/snap-core/alg.cpp | 0 inst/include/snap-core/alg.h | 544 -- inst/include/snap-core/anf.cpp | 61 - inst/include/snap-core/anf.h | 296 - inst/include/snap-core/attr.cpp | 485 -- inst/include/snap-core/attr.h | 177 - inst/include/snap-core/bfsdfs.h | 570 -- inst/include/snap-core/bignet.h | 1098 ---- inst/include/snap-core/casc.cpp | 263 - inst/include/snap-core/casc.h | 14 - inst/include/snap-core/centr.cpp | 926 --- inst/include/snap-core/centr.h | 662 -- inst/include/snap-core/cmty.cpp | 1451 ----- inst/include/snap-core/cmty.h | 93 - inst/include/snap-core/cncom.cpp | 142 - inst/include/snap-core/cncom.h | 468 -- inst/include/snap-core/conv.cpp | 128 - inst/include/snap-core/conv.h | 2525 -------- inst/include/snap-core/coreper.cpp | 226 - inst/include/snap-core/coreper.h | 8 - inst/include/snap-core/doc/Snap.h.txt | 44 - inst/include/snap-core/doc/bfsdfs.h.txt | 52 - inst/include/snap-core/doc/bignet.h.txt | 25 - inst/include/snap-core/doc/cncom.h.txt | 71 - inst/include/snap-core/doc/flow.cpp.txt | 43 - inst/include/snap-core/doc/flow.h.txt | 10 - inst/include/snap-core/doc/gbase.h.txt | 17 - inst/include/snap-core/doc/ghash.h.txt | 150 - inst/include/snap-core/doc/gio.h.txt | 59 - inst/include/snap-core/doc/graph.h.txt | 337 - inst/include/snap-core/doc/graphmp.h.txt | 303 - inst/include/snap-core/doc/gviz.h.txt | 14 - inst/include/snap-core/doc/mmnet.h.txt | 15 - inst/include/snap-core/doc/network.h.txt | 580 -- inst/include/snap-core/doc/networkmp.h.txt | 99 - inst/include/snap-core/doc/subgraph.h.txt | 130 - inst/include/snap-core/doc/table.h.txt | 205 - inst/include/snap-core/doc/triad.h.txt | 78 - inst/include/snap-core/doc/util.h.txt | 10 - inst/include/snap-core/ff.cpp | 815 --- inst/include/snap-core/ff.h | 148 - inst/include/snap-core/flow.cpp | 428 -- inst/include/snap-core/flow.h | 11 - inst/include/snap-core/gbase.cpp | 59 - inst/include/snap-core/gbase.h | 337 - inst/include/snap-core/ggen.cpp | 551 -- inst/include/snap-core/ggen.h | 267 - inst/include/snap-core/ghash.cpp | 432 -- inst/include/snap-core/ghash.h | 489 -- inst/include/snap-core/gio.cpp | 348 -- inst/include/snap-core/gio.h | 419 -- inst/include/snap-core/graph.cpp | 882 --- inst/include/snap-core/graph.h | 950 --- inst/include/snap-core/graph.txt | 14 - inst/include/snap-core/graphmp.cpp | 290 - inst/include/snap-core/graphmp.h | 241 - inst/include/snap-core/gstat.cpp | 605 -- inst/include/snap-core/gstat.h | 471 -- inst/include/snap-core/gsvd.cpp | 411 -- inst/include/snap-core/gsvd.h | 75 - inst/include/snap-core/gviz.cpp | 44 - inst/include/snap-core/gviz.h | 39 - inst/include/snap-core/kcore.h | 136 - inst/include/snap-core/mmnet.cpp | 1362 ---- inst/include/snap-core/mmnet.h | 683 -- inst/include/snap-core/network.cpp | 2790 --------- inst/include/snap-core/network.h | 3874 ------------ inst/include/snap-core/networkmp.cpp | 1057 ---- inst/include/snap-core/networkmp.h | 675 -- inst/include/snap-core/numpy.cpp | 38 - inst/include/snap-core/numpy.h | 15 - inst/include/snap-core/randwalk.h | 149 - inst/include/snap-core/sim.cpp | 266 - inst/include/snap-core/sim.h | 19 - .../snap-core.xcodeproj/project.pbxproj | 1900 ------ inst/include/snap-core/statplot.cpp | 95 - inst/include/snap-core/statplot.h | 189 - inst/include/snap-core/stdafx.h | 1 - inst/include/snap-core/subgraph.cpp | 143 - inst/include/snap-core/subgraph.h | 376 -- inst/include/snap-core/table.cpp | 5531 ----------------- inst/include/snap-core/table.h | 1588 ----- inst/include/snap-core/testSnap.cpp | 78 - inst/include/snap-core/timenet.cpp | 1422 ----- inst/include/snap-core/timenet.h | 136 - inst/include/snap-core/triad.cpp | 101 - inst/include/snap-core/triad.h | 893 --- inst/include/snap-core/util.cpp | 772 --- inst/include/snap-core/util.h | 197 - 163 files changed, 85052 deletions(-) delete mode 100644 inst/include/Credits.txt delete mode 100644 inst/include/Makefile delete mode 100644 inst/include/Makefile.config delete mode 100644 inst/include/RcppSNAP.h delete mode 100644 inst/include/Version delete mode 100644 inst/include/glib-core/base.cpp delete mode 100644 inst/include/glib-core/base.h delete mode 100644 inst/include/glib-core/bd.cpp delete mode 100644 inst/include/glib-core/bd.h delete mode 100644 inst/include/glib-core/bits.cpp delete mode 100644 inst/include/glib-core/bits.h delete mode 100644 inst/include/glib-core/blobbs.cpp delete mode 100644 inst/include/glib-core/blobbs.h delete mode 100644 inst/include/glib-core/doc/bd.h.txt delete mode 100644 inst/include/glib-core/doc/ds.h.txt delete mode 100644 inst/include/glib-core/doc/ss.h.txt delete mode 100644 inst/include/glib-core/doc/ssmp.h.txt delete mode 100644 inst/include/glib-core/ds.h delete mode 100644 inst/include/glib-core/dt.cpp delete mode 100644 inst/include/glib-core/dt.h delete mode 100644 inst/include/glib-core/env.cpp delete mode 100644 inst/include/glib-core/env.h delete mode 100644 inst/include/glib-core/fl.cpp delete mode 100644 inst/include/glib-core/fl.h delete mode 100644 inst/include/glib-core/gnuplot.cpp delete mode 100644 inst/include/glib-core/gnuplot.h delete mode 100644 inst/include/glib-core/hash.cpp delete mode 100644 inst/include/glib-core/hash.h delete mode 100644 inst/include/glib-core/hashmp.h delete mode 100644 inst/include/glib-core/html.cpp delete mode 100644 inst/include/glib-core/html.h delete mode 100644 inst/include/glib-core/http.cpp delete mode 100644 inst/include/glib-core/http.h delete mode 100644 inst/include/glib-core/json.cpp delete mode 100644 inst/include/glib-core/json.h delete mode 100644 inst/include/glib-core/linalg.cpp delete mode 100644 inst/include/glib-core/linalg.h delete mode 100644 inst/include/glib-core/lx.cpp delete mode 100644 inst/include/glib-core/lx.h delete mode 100644 inst/include/glib-core/md5.cpp delete mode 100644 inst/include/glib-core/md5.h delete mode 100644 inst/include/glib-core/os.cpp delete mode 100644 inst/include/glib-core/os.h delete mode 100644 inst/include/glib-core/priorityqueue.h delete mode 100644 inst/include/glib-core/shash.h delete mode 100644 inst/include/glib-core/ss.cpp delete mode 100644 inst/include/glib-core/ss.h delete mode 100644 inst/include/glib-core/ssmp.cpp delete mode 100644 inst/include/glib-core/ssmp.h delete mode 100644 inst/include/glib-core/stdafx.h delete mode 100644 inst/include/glib-core/tm.cpp delete mode 100644 inst/include/glib-core/tm.h delete mode 100644 inst/include/glib-core/unicode.cpp delete mode 100644 inst/include/glib-core/unicode.h delete mode 100644 inst/include/glib-core/unicodestring.cpp delete mode 100644 inst/include/glib-core/unicodestring.h delete mode 100644 inst/include/glib-core/url.cpp delete mode 100644 inst/include/glib-core/url.h delete mode 100644 inst/include/glib-core/ut.cpp delete mode 100644 inst/include/glib-core/ut.h delete mode 100644 inst/include/glib-core/wch.cpp delete mode 100644 inst/include/glib-core/wch.h delete mode 100644 inst/include/glib-core/xfl.cpp delete mode 100644 inst/include/glib-core/xfl.h delete mode 100644 inst/include/glib-core/xmath.cpp delete mode 100644 inst/include/glib-core/xmath.h delete mode 100644 inst/include/glib-core/xml.cpp delete mode 100644 inst/include/glib-core/xml.h delete mode 100644 inst/include/glib-core/xmlser.h delete mode 100644 inst/include/glib-core/zipfl.cpp delete mode 100644 inst/include/glib-core/zipfl.h delete mode 100644 inst/include/snap-core/Makefile delete mode 100644 inst/include/snap-core/Snap.cpp delete mode 100644 inst/include/snap-core/Snap.h delete mode 100644 inst/include/snap-core/alg.cpp delete mode 100644 inst/include/snap-core/alg.h delete mode 100644 inst/include/snap-core/anf.cpp delete mode 100644 inst/include/snap-core/anf.h delete mode 100644 inst/include/snap-core/attr.cpp delete mode 100644 inst/include/snap-core/attr.h delete mode 100644 inst/include/snap-core/bfsdfs.h delete mode 100644 inst/include/snap-core/bignet.h delete mode 100644 inst/include/snap-core/casc.cpp delete mode 100644 inst/include/snap-core/casc.h delete mode 100644 inst/include/snap-core/centr.cpp delete mode 100644 inst/include/snap-core/centr.h delete mode 100644 inst/include/snap-core/cmty.cpp delete mode 100644 inst/include/snap-core/cmty.h delete mode 100644 inst/include/snap-core/cncom.cpp delete mode 100644 inst/include/snap-core/cncom.h delete mode 100644 inst/include/snap-core/conv.cpp delete mode 100644 inst/include/snap-core/conv.h delete mode 100644 inst/include/snap-core/coreper.cpp delete mode 100644 inst/include/snap-core/coreper.h delete mode 100644 inst/include/snap-core/doc/Snap.h.txt delete mode 100644 inst/include/snap-core/doc/bfsdfs.h.txt delete mode 100644 inst/include/snap-core/doc/bignet.h.txt delete mode 100644 inst/include/snap-core/doc/cncom.h.txt delete mode 100644 inst/include/snap-core/doc/flow.cpp.txt delete mode 100644 inst/include/snap-core/doc/flow.h.txt delete mode 100644 inst/include/snap-core/doc/gbase.h.txt delete mode 100644 inst/include/snap-core/doc/ghash.h.txt delete mode 100644 inst/include/snap-core/doc/gio.h.txt delete mode 100644 inst/include/snap-core/doc/graph.h.txt delete mode 100644 inst/include/snap-core/doc/graphmp.h.txt delete mode 100644 inst/include/snap-core/doc/gviz.h.txt delete mode 100644 inst/include/snap-core/doc/mmnet.h.txt delete mode 100644 inst/include/snap-core/doc/network.h.txt delete mode 100644 inst/include/snap-core/doc/networkmp.h.txt delete mode 100644 inst/include/snap-core/doc/subgraph.h.txt delete mode 100644 inst/include/snap-core/doc/table.h.txt delete mode 100644 inst/include/snap-core/doc/triad.h.txt delete mode 100644 inst/include/snap-core/doc/util.h.txt delete mode 100644 inst/include/snap-core/ff.cpp delete mode 100644 inst/include/snap-core/ff.h delete mode 100644 inst/include/snap-core/flow.cpp delete mode 100644 inst/include/snap-core/flow.h delete mode 100644 inst/include/snap-core/gbase.cpp delete mode 100644 inst/include/snap-core/gbase.h delete mode 100644 inst/include/snap-core/ggen.cpp delete mode 100644 inst/include/snap-core/ggen.h delete mode 100644 inst/include/snap-core/ghash.cpp delete mode 100644 inst/include/snap-core/ghash.h delete mode 100644 inst/include/snap-core/gio.cpp delete mode 100644 inst/include/snap-core/gio.h delete mode 100644 inst/include/snap-core/graph.cpp delete mode 100644 inst/include/snap-core/graph.h delete mode 100644 inst/include/snap-core/graph.txt delete mode 100644 inst/include/snap-core/graphmp.cpp delete mode 100644 inst/include/snap-core/graphmp.h delete mode 100644 inst/include/snap-core/gstat.cpp delete mode 100644 inst/include/snap-core/gstat.h delete mode 100644 inst/include/snap-core/gsvd.cpp delete mode 100644 inst/include/snap-core/gsvd.h delete mode 100644 inst/include/snap-core/gviz.cpp delete mode 100644 inst/include/snap-core/gviz.h delete mode 100644 inst/include/snap-core/kcore.h delete mode 100644 inst/include/snap-core/mmnet.cpp delete mode 100644 inst/include/snap-core/mmnet.h delete mode 100644 inst/include/snap-core/network.cpp delete mode 100644 inst/include/snap-core/network.h delete mode 100644 inst/include/snap-core/networkmp.cpp delete mode 100644 inst/include/snap-core/networkmp.h delete mode 100644 inst/include/snap-core/numpy.cpp delete mode 100644 inst/include/snap-core/numpy.h delete mode 100644 inst/include/snap-core/randwalk.h delete mode 100644 inst/include/snap-core/sim.cpp delete mode 100644 inst/include/snap-core/sim.h delete mode 100644 inst/include/snap-core/snap-core.xcodeproj/project.pbxproj delete mode 100644 inst/include/snap-core/statplot.cpp delete mode 100644 inst/include/snap-core/statplot.h delete mode 100644 inst/include/snap-core/stdafx.h delete mode 100644 inst/include/snap-core/subgraph.cpp delete mode 100644 inst/include/snap-core/subgraph.h delete mode 100644 inst/include/snap-core/table.cpp delete mode 100644 inst/include/snap-core/table.h delete mode 100644 inst/include/snap-core/testSnap.cpp delete mode 100644 inst/include/snap-core/timenet.cpp delete mode 100644 inst/include/snap-core/timenet.h delete mode 100644 inst/include/snap-core/triad.cpp delete mode 100644 inst/include/snap-core/triad.h delete mode 100644 inst/include/snap-core/util.cpp delete mode 100644 inst/include/snap-core/util.h diff --git a/inst/include/Credits.txt b/inst/include/Credits.txt deleted file mode 100644 index 02c6700..0000000 --- a/inst/include/Credits.txt +++ /dev/null @@ -1,60 +0,0 @@ -Stanford Network Analysis Platform (SNAP) is a general purpose, high -performance system for graph and network manipulation and analysis. - -Jure Leskovec started developing SNAP during his PhD studies in 2005. -Rok Sosic has been maintaining the code since 2012. GLib, which is used -extensively by SNAP, was started by Marko Grobelnik in 1996. - -The following people also contributed to the development of SNAP -(in alphabetical order). - -Arijit Banerjee -Austin Benson -Justin Cheng -Royce Cheng-Yue -Blaz Fortuna -Florian Geigl -Stephen Dean Guo -Agrim Gupta -Shubham Gupta -Tim Hsieh -Richard Hsu -Chentai Kao -Mario Karlovcec -Nikhil Khadke -Vikesh Khanna -Emmanouil Kiagias -Myunghwan Kim -Vid Kocijan -Saachi Jain -Jason Jong -Peter Lofgren -Stephen Macke -Carolina Mattsson -Julian McAuley -Morteza Milani -Riyad Parvez -Yonathan Perez -Mohana Prasad -Rohan Puttagunta -Martin Raison -Sheila Ramaswamy -Manuel Gomez Rodriguez -Kenny Root -Davide Sardina -Christoph Schaefer -Pararth Shah -Nicholas Shelly -Klemen Simonic -Guo Tai -Ming Han Teh -Ba-Quan Truong -Utkarsh Upadhyay -Theodore Vasiloudis -Viswajith Venugopal -Jaewon Yang -Hao Yin -David Zeng -Chenguang Zhu -Ziga Zupanec - diff --git a/inst/include/Makefile b/inst/include/Makefile deleted file mode 100644 index 35b8ece..0000000 --- a/inst/include/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -# -# Makefile for non-Microsoft compilers -# - -all: MakeAll - -test: TestAll - -MakeAll: - $(MAKE) -C snap-core - $(MAKE) -C examples - -TestAll: - $(MAKE) run -C test - -clean: - $(MAKE) clean -C snap-core - $(MAKE) clean -C examples - $(MAKE) clean -C test - $(MAKE) clean -C tutorials diff --git a/inst/include/Makefile.config b/inst/include/Makefile.config deleted file mode 100644 index cfbb92b..0000000 --- a/inst/include/Makefile.config +++ /dev/null @@ -1,61 +0,0 @@ -# -# Global configuration for SNAP makefiles -# - -GLIB = glib-core -SNAP = snap-core -GLIBADV = glib-adv -SNAPADV = snap-adv -SNAPEXP = snap-exp - -CGLIB = ../$(GLIB) -CSNAP = ../$(SNAP) - -EXGLIB = ../../$(GLIB) -EXSNAP = ../../$(SNAP) -EXGLIBADV = ../../$(GLIBADV) -EXSNAPADV = ../../$(SNAPADV) -EXSNAPEXP = ../../$(SNAPEXP) - -UNAME := $(shell uname) - -ifeq ($(UNAME), Linux) - # Linux flags - CC = g++ - CXXFLAGS += -std=c++98 -Wall - CXXFLAGS += -O3 -DNDEBUG -fopenmp - # turn on for crash debugging, get symbols with 2>&1 | c++filt - #CXXFLAGS += -g -rdynamic - #CXXFLAGS += -ggdb - # turn on for OpenMP - CXXOPENMP = - LDFLAGS += - LIBS += -lrt - -else ifeq ($(UNAME), Darwin) - # OS X flags - CC = g++ - CXXFLAGS += -std=c++98 -Wall -Wno-unknown-pragmas - CXXFLAGS += -O3 -DNDEBUG - CLANG := $(shell g++ -v 2>&1 | grep clang | cut -d " " -f 2) - ifneq ($(CLANG), LLVM) - CXXFLAGS += -fopenmp - #CXXOPENMP += -fopenmp - else - CXXFLAGS += -DNOMP - CXXOPENMP = - endif - LDFLAGS += - LIBS += - -else ifeq ($(shell uname -o), Cygwin) - # Cygwin flags - CC = g++ - CXXFLAGS += -Wall -D__STDC_LIMIT_MACROS - CXXFLAGS += -O3 -DNDEBUG - CXXOPENMP = -fopenmp - LDFLAGS += - LIBS += - -endif - diff --git a/inst/include/RcppSNAP.h b/inst/include/RcppSNAP.h deleted file mode 100644 index 48f556c..0000000 --- a/inst/include/RcppSNAP.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef RcppSNAP__RcppSNAP__h -#define RcppSNAP__RcppSNAP__h - -#if defined(Rcpp_hpp) && !defined(COMPILING_RCPPSNAP) -#error "The file 'Rcpp.h' should not be included. Please correct to include only 'RcppSNAP.h'." -#endif - -#include "Snap.h" - -// Now automatically include Rcpp as well -#include - -#endif - diff --git a/inst/include/Version b/inst/include/Version deleted file mode 100644 index fc898f7..0000000 --- a/inst/include/Version +++ /dev/null @@ -1 +0,0 @@ -Snap-4.0-20170727-131804 diff --git a/inst/include/glib-core/base.cpp b/inst/include/glib-core/base.cpp deleted file mode 100644 index 410e560..0000000 --- a/inst/include/glib-core/base.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include "stdafx.h" - -#include "base.h" - -void BaseTralala(){ - printf("Active defines:\n"); - #ifdef GLib_WIN - printf(" GLib_WIN\n"); - #endif - #ifdef GLib_WIN32 - printf(" GLib_WIN32\n"); - #endif - #ifdef GLib_WIN64 - printf(" GLib_WIN64\n"); - #endif - #ifdef GLib_UNIX - printf(" GLib_UNIX\n"); - #endif - #ifdef GLib_LINUX - printf(" GLib_LINUX\n"); - #endif - #ifdef GLib_SOLARIS - printf(" GLib_SOLARIS\n"); - #endif - #ifdef GLib_MSC - printf(" GLib_MSC\n"); - #endif - #ifdef GLib_CYGWIN - printf(" GLib_CYGWIN\n"); - #endif - #ifdef GLib_BCB - printf(" GLib_BCB\n"); - #endif - #ifdef GLib_GCC - printf(" GLib_GCC\n"); - #endif - #ifdef GLib_MACOSX - printf(" GLib_MACOSX\n"); - #endif - #ifdef GLib_64Bit - printf(" GLib_64Bit\n"); - #endif - #ifdef GLib_32Bit - printf(" GLib_32Bit\n"); - #endif - #ifdef GLib_GLIBC - printf(" GLib_GLIBC\n"); - #endif - #ifdef GLib_POSIX_1j - printf(" GLib_POSIX_1j\n"); - #endif -} - -#if defined(GLib_UNIX) && ! defined(GLib_CYGWIN) -int _daylight = 0; -#endif - -#if defined(WIN32_LEAN_AND_MEAN) -int gettimeofday(struct timeval * tp, struct timezone * tzp) -{ - // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's - static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL); - - SYSTEMTIME system_time; - FILETIME file_time; - uint64_t time; - - GetSystemTime( &system_time ); - SystemTimeToFileTime( &system_time, &file_time ); - time = ((uint64_t)file_time.dwLowDateTime ) ; - time += ((uint64_t)file_time.dwHighDateTime) << 32; - - tp->tv_sec = (long) ((time - EPOCH) / 10000000L); - tp->tv_usec = (long) (system_time.wMilliseconds * 1000); - return 0; -} -#endif - -#include "bd.cpp" -#include "fl.cpp" -#include "dt.cpp" -#include "ut.cpp" -#include "hash.cpp" - -#include "unicode.cpp" -#include "unicodestring.cpp" -#include "tm.cpp" -#include "os.cpp" - -#include "bits.cpp" -#include "env.cpp" -#include "wch.cpp" -#include "xfl.cpp" -#include "xmath.cpp" - -#include "blobbs.cpp" -#include "lx.cpp" -#include "url.cpp" - -#include "http.cpp" -#include "html.cpp" -#include "md5.cpp" -#include "ss.cpp" -#include "ssmp.cpp" -#include "xml.cpp" -#include "json.cpp" -//#include "prolog.cpp" - -#include "zipfl.cpp" - diff --git a/inst/include/glib-core/base.h b/inst/include/glib-core/base.h deleted file mode 100644 index 3d83e44..0000000 --- a/inst/include/glib-core/base.h +++ /dev/null @@ -1,217 +0,0 @@ -#ifndef base_h -#define base_h - -///////////////////////////////////////////////// -// Environment defines -#if defined (_WIN32) - #define GLib_WIN - #define GLib_WIN32 -#elif defined (__WIN64) - #define GLib_WIN - #define GLib_WIN64 -#elif defined(__linux__) - #define GLib_UNIX - #define GLib_LINUX __linux__ -#elif defined(__sun__) - #define GLib_UNIX - #define GLib_SOLARIS __sun__ - // !bn: defined in ctype_iso.h but apears as a parameter in GLib - #undef _C -#elif defined(__FreeBSD__) - #define GLib_UNIX - #define GLib_BSD __FreeBSD__ -#elif defined(__CYGWIN__) - #define GLib_UNIX - #define GLib_CYGWIN -#elif (defined(__APPLE__) && defined(__MACH__)) - #define GLib_UNIX - #define GLib_MACOSX -#endif - -#define _CMPWARN -// compiler -#if defined (_MSC_VER) - #define GLib_MSC _MSC_VER -#endif -#if defined (__BCPLUSPLUS__) - #define GLib_BCB __BCPLUSPLUS__ -#endif -#if defined (__GNUC__) - #define GLib_GCC __GNUC__ -// !bn: to bo not samo dokler ne ugotovim kje so primerjave problematicne - #ifdef FLTWARN - #undef _CMPWARN - #define _CMPWARN __attribute__ ((deprecated)) - #endif -#endif - -// includes -#if defined (GLib_WIN32) - #define WIN32_LEAN_AND_MEAN - #include - #if !defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_) - #include - #endif - #include - #include -#endif - -#if defined(GLib_UNIX) - #undef __STDC_LIMIT_MACROS - #define __STDC_LIMIT_MACROS - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include -#endif - -#if defined(WIN32_LEAN_AND_MEAN) - #include - - int gettimeofday(struct timeval * tp, struct timezone * tzp); -#endif - -// word size -#if __WORDSIZE == 32 || defined(GLib_WIN32) || defined(__CYGWIN32__) - #define GLib_32Bit -#elif __WORDSIZE == 64 || defined(GLib_WIN64) || defined(__CYGWIN64__) - #define GLib_64Bit -#else - #error "Undefined word size" -#endif - -#if defined(GLib_UNIX) - #ifndef _environ - #if defined(GLib_MACOSX) - #include - #define _environ (*_NSGetEnviron()) - #elif !defined(GLib_CYGWIN) - #define _environ __environ - extern int _daylight; - #else - #define _environ environ - #endif - #endif - #ifndef __stdcall - #define __stdcall - #endif -#endif - -#if defined(GLib_LINUX) - #include -#endif - -#if defined(GLib_SOLARIS) - #include -#endif - -#if defined (__GLIBC__) - #define GLib_GLIBC __GLIBC__ -#endif - -#if defined (__USE_XOPEN2K) - #define GLib_POSIX_1j "1003.1j" -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef GLib_CYGWIN - #define timezone _timezone -#endif - -//#ifdef GLib_MACOSX -// #undef _POSIX_MONOTONIC_CLOCK -//#endif - -// -// OpenMP directives -// - -#if defined(_OPENMP) && !defined(GLib_WIN) - // main OpenMP flag - #define USE_OPENMP - #define CHUNKS_PER_THREAD 10 - #include - #if defined(GLib_GCC) - // use of __sync_... GCC atomic primitves - #define GCC_ATOMIC - #endif -#endif - -// for Snapworld, switch is defined to include util.h:WriteN() -//#if (defined(GLib_UNIX) && !defined(SWIG)) || (defined(SWIG_SW)) -#if defined(SWIG_SW) - #define SW_WRITEN -#endif - -// for backtrace dump in G++, change SW_TRACE to 1 -#define SW_TRACE 0 - -// for Snap.py, switch 'no abort' is defined and NDEBUG is turned off -#if defined(SW_SNAPPY) - #define SW_NOABORT - #undef NDEBUG -#endif - -#include "bd.h" -#include "fl.h" -#include "dt.h" -#include "ut.h" -#include "ds.h" -#include "bits.h" -#include "hash.h" -#include "hashmp.h" -#include "xml.h" - -#include "xmath.h" -#include "xmlser.h" - -#include "unicode.h" -#include "unicodestring.h" -#include "tm.h" -#include "shash.h" -#include "os.h" - -#include "env.h" -#include "wch.h" -#include "xfl.h" - -#include "blobbs.h" -#include "lx.h" -#include "url.h" - -#include "http.h" -#include "html.h" -#include "md5.h" -#include "ss.h" -#include "ssmp.h" -#include "json.h" -//#include "prolog.h" - -#include "zipfl.h" - -void BaseTralala(); - -#endif diff --git a/inst/include/glib-core/bd.cpp b/inst/include/glib-core/bd.cpp deleted file mode 100644 index c363a93..0000000 --- a/inst/include/glib-core/bd.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#if SW_TRACE -#include -#endif - -///////////////////////////////////////////////// -// Mathmatical-Errors -#if defined(__BCPLUSPLUS__) && (__BCPLUSPLUS__==0x0530) -int std::_matherr(struct math_exception* e){ - e->retval=0; - return 1; -} -#elif defined(GLib_GLIBC) || defined(GLib_BSD) -int _matherr(struct __exception* e){ - e->retval=0; - return 1; -} -#elif defined(GLib_SOLARIS) -int _matherr(struct __math_exception* e){ - e->retval=0; - return 1; -} -#elif defined(GLib_CYGWIN) -int matherr(struct __exception *e){ - e->retval=0; - return 1; -} -#elif defined(GLib_MACOSX) -//int matherr(struct exception *e) { -// e->retval=0; -// return 1; -//} -#else -int _matherr(struct _exception* e){ - e->retval=0; - return 1; -} -#endif - -///////////////////////////////////////////////// -// Messages -void WrNotify(const char* CaptionCStr, const char* NotifyCStr){ -#if defined(__CONSOLE__) || defined(_CONSOLE) - printf("*** %s: %s\n", CaptionCStr, NotifyCStr); -#else - MessageBox(NULL, NotifyCStr, CaptionCStr, MB_OK); -#endif -} - -void SaveToErrLog(const char* MsgCStr){ - int MxFNmLen=1000; - char* FNm=new char[MxFNmLen]; if (FNm==NULL){return;} - int FNmLen=GetModuleFileName(NULL, FNm, MxFNmLen); if (FNmLen==0){return;} - FNm[FNmLen++]='.'; FNm[FNmLen++]='E'; FNm[FNmLen++]='r'; FNm[FNmLen++]='r'; - FNm[FNmLen++]=char(0); - time_t Time=time(NULL); - FILE* fOut=fopen(FNm, "a+b"); if (fOut==NULL){return;} - fprintf(fOut, "--------\r\n%s\r\n%s%s\r\n--------\r\n", - FNm, ctime(&Time), MsgCStr); - fclose(fOut); - delete[] FNm; -} - -#if SW_TRACE -void PrintBacktrace() { - // stack dump, works for g++ - void *array[20]; - size_t size; - - // flush stdout - fflush(0); - - // get the trace and print it to stdout - size = backtrace(array, 20); - backtrace_symbols_fd(array, size, 1); -} - -void Crash() { - int *p; - p = (int *) 0; - *p = 1234; -} -#endif - -///////////////////////////////////////////////// -// Assertions -TOnExeStop::TOnExeStopF TOnExeStop::OnExeStopF=NULL; - -void ExeStop( - const char* MsgCStr, const char* ReasonCStr, - const char* CondCStr, const char* FNm, const int& LnN){ - char ReasonMsgCStr[1000]; - -#if SW_TRACE - PrintBacktrace(); - Crash(); -#endif - - // construct reason message - if (ReasonCStr==NULL){ReasonMsgCStr[0]=0;} - else {sprintf(ReasonMsgCStr, " [Reason:'%s']", ReasonCStr);} - // construct full message - char FullMsgCStr[1000]; - if (MsgCStr==NULL){ - if (CondCStr==NULL){ - sprintf(FullMsgCStr, "Execution stopped%s!", ReasonMsgCStr); - } else { - sprintf(FullMsgCStr, "Execution stopped: %s%s, file %s, line %d", - CondCStr, ReasonMsgCStr, FNm, LnN); - } - } else { - if (CondCStr==NULL){ - sprintf(FullMsgCStr, "%s\nExecution stopped!", MsgCStr); - } else { - sprintf(FullMsgCStr, "Message: %s%s\nExecution stopped: %s, file %s, line %d", - MsgCStr, ReasonMsgCStr, CondCStr, FNm, LnN); - } - } - // report full message to log file - SaveToErrLog(FullMsgCStr); - -#if defined(SW_NOABORT) - TExcept::Throw(FullMsgCStr); -#endif - - // report to screen & stop execution - bool Continue=false; - // call handler - if (TOnExeStop::IsOnExeStopF()){ - Continue=!((*TOnExeStop::GetOnExeStopF())(FullMsgCStr));} - if (!Continue){ - ErrNotify(FullMsgCStr); -#ifdef GLib_WIN32 - abort(); - //ExitProcess(1); -#else - exit(1); -#endif - } -} diff --git a/inst/include/glib-core/bd.h b/inst/include/glib-core/bd.h deleted file mode 100644 index 4b63d25..0000000 --- a/inst/include/glib-core/bd.h +++ /dev/null @@ -1,605 +0,0 @@ -#ifndef bd_h -#define bd_h - -///////////////////////////////////////////////// -// Basic-Macro-Definitions -#define forever for(;;) - -///////////////////////////////////////////////// -// Basic-Type-Definitions -typedef unsigned char uchar; -typedef unsigned int uint; -typedef unsigned long ulong; -typedef unsigned short ushort; - -typedef float sdouble; -typedef long double ldouble; -typedef FILE* TFileId; - -typedef char int8; -typedef short int16; -typedef int int32; -#ifdef GLib_WIN32 -typedef __int64 int64; -#elif defined(GLib_GLIBC) -typedef int64_t int64; -#else -typedef long long int64; -#endif - -typedef unsigned char uint8; -typedef unsigned short uint16; -typedef unsigned int uint32; -#ifdef GLib_WIN32 -typedef unsigned __int64 uint64; -#elif defined(GLib_GLIBC) -typedef u_int64_t uint64; -#else -typedef unsigned long long uint64; -#endif - -#if (!defined(__ssize_t_defined) && !defined(GLib_MACOSX)) -typedef ptrdiff_t ssize_t; -#endif - -#if defined(GLib_UNIX) -#define _isnan(x) isnan(x) -#if defined(GLib_MACOSX) - #define _finite(x) isfinite(x) -#else - #define _finite(x) finite(x) -#endif -#endif - -#if defined(GLib_WIN32) -#define _vsnprintf vsnprintf -#endif - -typedef size_t TSize; -#define TSizeMx SIZE_MAX - -///////////////////////////////////////////////// -// Localization -typedef enum {lUndef, lUs, lSi} TLoc; - -///////////////////////////////////////////////// -// Messages -void WrNotify(const char* CaptionCStr, const char* NotifyCStr); -void SaveToErrLog(const char* MsgCStr); - -inline void InfoNotify(const char* NotifyCStr){ - WrNotify("Information", NotifyCStr);} -inline void WarnNotify(const char* NotifyCStr){ - WrNotify("Warning", NotifyCStr);} -inline void ErrNotify(const char* NotifyCStr){ - WrNotify("Error", NotifyCStr);} -inline void StatNotify(const char* NotifyCStr){ - WrNotify("Status", NotifyCStr);} - -///////////////////////////////////////////////// -// Compiler-Options -#if defined (GLib_WIN) - #define snprintf _snprintf - #define vsnprintf _vsnprintf - // disable warning 'debug symbol greater than 255 chars' - #pragma warning(disable: 4786) - // disable warning 'return type for 'identifier::operator->' - // is not a UDT or reference to a UDT. - //#pragma warning(disable: 4284) - // 'declaration' : function differs from 'declaration' - // only by calling convention - //#pragma warning(disable: 4666) - #pragma warning(disable: 4996) -#endif - -#if defined (GLib_GCC) - #define _CONSOLE -#endif - -// use console output if console already exists, otherwise -// define GLib_COUT to use console (class COut) -#if defined(_CONSOLE)||defined(__CONSOLE__) - #define GLib_Console -#endif - -// create console if want to output but there is no console allocated -#if defined(GLib_Console) && (! (defined(__CONSOLE__)||defined(_CONSOLE))) - #define GLib_CreateConsole -#endif - -///////////////////////////////////////////////// -// Class-Definition -#define ClassT(TNm) \ -class TNm{ - -#define ClassTV(TNm, TNmV) \ -class TNm; \ -typedef TVec TNmV; \ -class TNm{ - -#define ClassTVQ(TNm, TNmV, TNmQ) \ -class TNm; \ -typedef TVec TNmV; \ -typedef TQQueue TNmQ; \ -class TNm{ - -#define ClassTP(TNm, PNm) \ -class TNm; \ -typedef TPt PNm; \ -class TNm{ \ -private: \ - TCRef CRef; \ -public: \ - friend class TPt; - -#define ClassHdTP(TNm, PNm) \ -class TNm; \ -typedef TPt PNm; - -#define ClassTPE(TNm, PNm, ENm) \ -class TNm; \ -typedef TPt PNm; \ -class TNm: public ENm{ \ -private: \ - TCRef CRef; \ -public: \ - friend class TPt; - -#define ClassTPEE(TNm, PNm, ENm1, ENm2) \ -class TNm; \ -typedef TPt PNm; \ -class TNm: public ENm1, public ENm2{ \ -private: \ - TCRef CRef; \ -public: \ - friend class TPt; - -#define ClassTE(TNm, ENm) \ -class TNm; \ -class TNm: public ENm{ \ -public: \ - -#define ClassTPV(TNm, PNm, TNmV) \ -class TNm; \ -typedef TPt PNm; \ -typedef TVec TNmV; \ -class TNm{ \ -private: \ - TCRef CRef; \ -public: \ - friend class TPt; - -#define ClassHdTPV(TNm, PNm, TNmV) \ -class TNm; \ -typedef TPt PNm; \ -typedef TVec TNmV; - -#define ClassTPVL(TNm, PNm, TNmV, TNmL, TNmLN) \ -class TNm; \ -typedef TPt PNm; \ -typedef TVec TNmV; \ -typedef TLst TNmL; \ -typedef TLstNd* TNmLN; \ -class TNm{ \ -private: \ - TCRef CRef; \ -public: \ - friend class TPt; - -///////////////////////////////////////////////// -// Forward-Definitions -class TSIn; -class TSOut; -class TStr; -class TXmlObjSer; -class TXmlObjSerTagNm; -template class TPt; -ClassHdTP(TXmlTok, PXmlTok) -ClassHdTP(TExcept, PExcept) -ClassHdTP(TXmlDoc, PXmlDoc) - -///////////////////////////////////////////////// -// DefaultConstructor/CopyConstructor/AssignmentOperator-UnDefinition -#define UndefDefaultCopyAssign(TNm) \ -private: \ - TNm(){} \ - TNm(const TNm&); \ - TNm& operator=(const TNm&) - -#define UndefCopyAssign(TNm) \ -private: \ - TNm(const TNm&); \ - TNm& operator=(const TNm&) - -#define UndefCopy(TNm) \ -private: \ - TNm(const TNm&) - -#define UndefAssign(TNm) \ -private: \ - TNm& operator=(const TNm&) - -///////////////////////////////////////////////// -// Assertions -class TOnExeStop{ -private: - typedef bool (*TOnExeStopF)(char* MsgCStr); - static TOnExeStopF OnExeStopF; -public: - static bool IsOnExeStopF(){return OnExeStopF!=NULL;} - static void PutOnExeStopF(TOnExeStopF _OnExeStopF){OnExeStopF=_OnExeStopF;} - static TOnExeStopF GetOnExeStopF(){return OnExeStopF;} -}; - -void ExeStop( - const char* MsgStr, const char* ReasonStr, - const char* CondStr, const char* FNm, const int& LnN); - -#define Fail ExeStop(NULL, NULL, "Fail", __FILE__, __LINE__) - -#define FailR(Reason) ExeStop((Reason), NULL, "Fail", __FILE__, __LINE__) -#define FailRA(Reason, ArgStr) \ - ExeStop((TStr(Reason)+" ("+ArgStr+")").CStr(), NULL, "Fail", __FILE__, __LINE__) - -#define EFail TExcept::ThrowFull("", TStr("[")+ TStr(__FILE__) + " line " + TInt::GetStr(__LINE__) + "]") - -#define EFailR(Reason) TExcept::ThrowFull(Reason, TStr("[")+TStr(__FILE__)+" line "+TInt::GetStr(__LINE__)+"]") - -#ifdef NDEBUG -#define Assert(Cond) -#else -#define Assert(Cond) \ - ((Cond) ? static_cast(0) : ExeStop(NULL, NULL, #Cond, __FILE__, __LINE__)) -#endif - -#ifdef NDEBUG -#define AssertR(Cond, Reason) -#else -#define AssertR(Cond, Reason) ((Cond) ? static_cast(0) : \ - ExeStop(NULL, TStr(Reason).CStr(), #Cond, __FILE__, __LINE__)) -#endif - -#define IAssert(Cond) \ - ((Cond) ? static_cast(0) : ExeStop(NULL, NULL, #Cond, __FILE__, __LINE__)) - -#define IAssertR(Cond, Reason) \ - ((Cond) ? static_cast(0) : ExeStop(NULL, TStr(Reason).CStr(), #Cond, __FILE__, __LINE__)) - -#define WAssert(Cond, MsgCStr) \ - ((Cond) ? static_cast(0) : WarnNotify(MsgCStr)) - -#define SAssert(Cond) \ - ((Cond) ? static_cast(0) : ExeStop(TSysStr::GetLastMsgCStr(), NULL, #Cond, __FILE__, __LINE__)) - -#define FAssert(Cond, MsgCStr) \ - ((Cond) ? static_cast(0) : ExeStop(TStr(MsgCStr).CStr(), NULL, NULL, __FILE__, __LINE__)) - -#define FSAssert(Cond) \ - ((Cond) ? static_cast(0) : ExeStop(TSysStr::GetLastMsgCStr(), NULL, NULL, __FILE__, __LINE__)) - -#define EAssert(Cond) \ - ((Cond) ? static_cast(0) : TExcept::ThrowFull(#Cond, TStr(__FILE__) + " line " + TInt::GetStr(__LINE__) +": "+ TStr(#Cond))) - -#define EAssertR(Cond, MsgStr) \ - ((Cond) ? static_cast(0) : TExcept::ThrowFull(MsgStr, TStr(__FILE__)+" line "+TInt::GetStr(__LINE__)+": "+TStr(#Cond))) - -#define EAssertRA(Cond, MsgStr, ArgStr) \ - ((Cond) ? static_cast(0) : TExcept::Throw(MsgStr, ArgStr)) - -#define EAssertRAA(Cond, MsgStr, ArgStr1, ArgStr2) \ - ((Cond) ? static_cast(0) : TExcept::Throw(MsgStr, ArgStr1, ArgStr2)) - -#define ESAssert(Cond) \ - ((Cond) ? static_cast(0) : TExcept::Throw(TSysStr::GetLastMsgCStr(), \ - TStr(__FILE__) + " line " + TInt::GetStr(__LINE__) +": "+ TStr(#Cond))) - -// compile time assert -// #define STATIC_ASSERT(x) { const char temp[ (((x) == 0) ? 0 : 1) ] = {'\0'}; } -template struct TStaticAssert; -template <> struct TStaticAssert { enum { value = 1 }; }; -template struct TStaticAssertTest{}; - -#define CAssert(Cond) \ - /* typedef TStaticAssertTest)> TestStaticAssert; */ - -///////////////////////////////////////////////// -// Xml-Object-Serialization -template -bool IsXLoadFromFileOk(const TStr& FNm, const TStr& Nm, TRec& Rec, TStr& MsgStr); -template -void XLoadFromFile(const TStr& FNm, const TStr& Nm, TRec& Rec); - -#define XLoadHd(Nm) \ - {TStr TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \ - TXmlObjSer::AssertXmlHd(XmlTok, Nm, TypeNm);} -#define XLoad(Nm) \ - Nm.LoadXml(XmlTok->GetTagTok(#Nm), #Nm); - -#define XSaveHd(Nm) \ - TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \ - TXmlObjSerTagNm XmlObjSerTagNm(SOut, false, Nm, _TypeNm); -#define XSaveHdArg(Nm, ArgNm, ArgVal) \ - TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \ - TXmlObjSerTagNm XmlObjSerTagNm(SOut, false, Nm, _TypeNm, ArgNm, ArgVal); -#define XSaveBETag(Nm) \ - TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \ - TXmlObjSerTagNm XmlObjSerTagNm(SOut, true, Nm, _TypeNm); -#define XSaveBETagArg(Nm, ArgNm, ArgVal) \ - TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \ - TXmlObjSerTagNm XmlObjSerTagNm(SOut, true, Nm, _TypeNm, ArgNm, ArgVal); -#define XSaveBETagArg4(Nm, ArgNm1, ArgVal1, ArgNm2, ArgVal2, ArgNm3, ArgVal3, ArgNm4, ArgVal4) \ - TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \ - TXmlObjSerTagNm XmlObjSerTagNm(SOut, true, Nm, _TypeNm, ArgNm1, ArgVal1, ArgNm2, ArgVal2, ArgNm3, ArgVal3, ArgNm4, ArgVal4); -#define XSave(Nm) \ - Nm.SaveXml(SOut, #Nm) -#define XSaveToFile(Nm, FNm) \ - {TFOut SOut(FNm); Nm.SaveXml(SOut, #Nm);} - -///////////////////////////////////////////////// -// Basic-Operators -typedef enum {loUndef, loNot, loAnd, loOr} TLogOp; -typedef enum {roUndef, roLs, roLEq, roEq, roNEq, roGEq, roGt} TRelOp; - -///////////////////////////////////////////////// -// Comparation-Macros -#ifndef MIN - #define MIN(a,b) ((a)<(b)?(a):(b)) -#endif - -#ifndef MAX - #define MAX(a,b) ((a)>(b)?(a):(b)) -#endif - -///////////////////////////////////////////////// -// Comparator-Definitions -template -class TEq{ -public: - bool operator()(const T& x, const T& y) const {return x==y;} -}; - -template -class TNEq{ -public: - bool operator()(const T& x, const T& y) const {return x!=y;} -}; - -template -class TLss{ -public: - bool operator()(const T& x, const T& y) const {return x -class TLEq{ -public: - bool operator()(const T& x, const T& y) const {return x<=y;} -}; - -template -class TGtr{ -public: - bool operator() (const T& x, const T& y) const { return x>y; } - int Cmp(const T& d1, const T& d2) const { //** vprasaj Janeza za uporabo - if (d1 -class TGEq{ -public: - bool operator()(const T& x, const T& y) const {return x>=y;} -}; - -template -class TCmp{ -public: - int operator()(const T& x, const T& y) const { - if (x < y){return -1;} - else if (x > y){return 1;} - else {return 0;} - } -}; - -///////////////////////////////////////////////// -// Operator-Definitions -template -bool operator!=(const TRec& Rec1, const TRec& Rec2){return !(Rec1==Rec2);} - -template -bool operator>(const TRec& Rec1, const TRec& Rec2){return Rec2 -bool operator<=(const TRec& Rec1, const TRec& Rec2){return !(Rec2 -bool operator>=(const TRec& Rec1, const TRec& Rec2){return !(Rec1 -bool Cmp(const int& RelOp, const TRec& Rec1, const TRec& Rec2){ - switch (RelOp){ - case roLs: return Rec1=Rec2; - case roGt: return Rec1>Rec2; - default: Fail; return false; - } -} - -///////////////////////////////////////////////// -// Reference-Count -class TCRef{ -private: - int Refs; -private: - TCRef& operator=(const TCRef&); - TCRef(const TCRef&); -public: - TCRef(): Refs(0){} - ~TCRef(){Assert(Refs==0);} - - void MkRef(){Refs++;} - void UnRef(){Assert(Refs>0); Refs--;} - bool NoRef() const {return Refs==0;} - int GetRefs() const {return Refs;} -}; - -///////////////////////////////////////////////// -// Smart-Pointer-With-Reference-Count -template -class TPt{ -public: - typedef TRec TObj; -private: - TRec* Addr; - void MkRef() const { - if (Addr!=NULL){ - Addr->CRef.MkRef(); - } - } - void UnRef() const { - if (Addr!=NULL){ - Addr->CRef.UnRef(); - if (Addr->CRef.NoRef()){delete Addr;} - } - } -public: - TPt(): Addr(NULL){} - TPt(const TPt& Pt): Addr(Pt.Addr){MkRef();} - TPt(TRec* _Addr): Addr(_Addr){MkRef();} - static TPt New(){return TObj::New();} - ~TPt(){UnRef();} - explicit TPt(TSIn& SIn); - explicit TPt(TSIn& SIn, void* ThisPt); - void Save(TSOut& SOut) const; - void LoadXml(const TPt& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TPt& operator=(const TPt& Pt){ - if (this!=&Pt){Pt.MkRef(); UnRef(); Addr=Pt.Addr;} return *this;} - bool operator==(const TPt& Pt) const {return *Addr==*Pt.Addr;} - bool operator!=(const TPt& Pt) const {return *Addr!=*Pt.Addr;} - bool operator<(const TPt& Pt) const {return *Addr<*Pt.Addr;} - - TRec* operator->() const {Assert(Addr!=NULL); return Addr;} - TRec& operator*() const {Assert(Addr!=NULL); return *Addr;} - TRec& operator[](const int& RecN) const { - Assert(Addr!=NULL); return Addr[RecN];} - TRec* operator()() const {return Addr;} - //const TRec* operator()() const {return Addr;} - //TRec* operator()() {return Addr;} - - bool Empty() const { return Addr==NULL;} - void Clr(){UnRef(); Addr=NULL;} - int GetRefs() const { - if (Addr==NULL){return -1;} else {return Addr->CRef.GetRefs();}} - - int GetPrimHashCd() const {return Addr->GetPrimHashCd();} - int GetSecHashCd() const {return Addr->GetSecHashCd();} - - TPt Clone(){return MkClone(*this);} -}; - -///////////////////////////////////////////////// -// Simple-String -class TSStr{ -private: - char* Bf; -public: - TSStr(): Bf(NULL){ - Bf=new char[0+1]; Bf[0]=0;} - TSStr(const TSStr& SStr): Bf(NULL){ - Bf=new char[strlen(SStr.Bf)+1]; strcpy(Bf, SStr.Bf);} - TSStr(const char* _Bf): Bf(NULL){ - Bf=new char[strlen(_Bf)+1]; strcpy(Bf, _Bf);} - ~TSStr(){delete[] Bf;} - - TSStr& operator=(const TSStr& SStr){ - if (this!=&SStr){ - delete[] Bf; Bf=new char[strlen(SStr.Bf)+1]; strcpy(Bf, SStr.Bf);} - return *this;} - - char* CStr() {return Bf;} - const char* CStr() const {return Bf;} - bool Empty() const {return Bf[0]==0;} - int Len() const {return int(strlen(Bf));} -}; - -///////////////////////////////////////////////// -// Conversion-Pointer64-To-Integers32 -class TConv_Pt64Ints32{ -private: - union{ - void* Pt; - uint64 UInt64; - struct {uint Ls; uint Ms;} UInt32; - } Val; - UndefCopyAssign(TConv_Pt64Ints32); -public: - TConv_Pt64Ints32(){ - Val.Pt=0; Val.UInt32.Ms=0; Val.UInt32.Ls=0;} - TConv_Pt64Ints32(void* Pt){ - Val.UInt32.Ms=0; Val.UInt32.Ls=0; Val.Pt=Pt;} - TConv_Pt64Ints32(const uint& Ms, const uint& Ls){ - Val.Pt=0; Val.UInt32.Ms=Ms; Val.UInt32.Ls=Ls;} - - void PutPt(void* Pt){Val.Pt=Pt;} - void* GetPt() const {return Val.Pt;} - void PutUInt64(const uint64& _UInt64){Val.UInt64=_UInt64;} - uint64 GetUInt64() const {return Val.UInt64;} - void PutMsUInt32(const uint& Ms){Val.UInt32.Ms=Ms;} - uint GetMsUInt32() const {return Val.UInt32.Ms;} - void PutLsUInt32(const uint& Ls){Val.UInt32.Ls=Ls;} - uint GetLsUInt32() const {return Val.UInt32.Ls;} -}; - -///////////////////////////////////////////////// -// Swap -template -void Swap(TRec& Rec1, TRec& Rec2){ - TRec Rec=Rec1; Rec1=Rec2; Rec2=Rec; -} - -///////////////////////////////////////////////// - -/// Computes a hash code from a pair of hash codes. ##TPairHashImpl1 -class TPairHashImpl1 { -public: - static inline int GetHashCd(const int hc1, const int hc2) { - unsigned long long sum = ((unsigned long long) hc1) + ((unsigned long long) hc2); - unsigned long long c = ((sum * (sum + 1)) >> 1) + hc1; - return (int) (c % 0x7fffffffULL); } -}; - -/// Computes a hash code from a pair of hash codes. ##TPairHashImpl2 -class TPairHashImpl2 { -public: - static inline int GetHashCd(const int hc1, const int hc2) { - unsigned long long sum = ((unsigned long long) hc1) + ((unsigned long long) hc2); - unsigned long long c = ((sum * (sum + 1)) >> 1) + hc1; - unsigned int R = (unsigned int) (c >> 31), Q = (unsigned int) (c & 0x7fffffffULL); - if ((R & 0x80000000U) != 0) R -= 0x7fffffffU; - unsigned int RQ = R + Q; - if (RQ < 0x7fffffffU) return (int) RQ; - RQ -= 0x7fffffffU; - return (RQ == 0x7fffffffU) ? 0 : (int) RQ; } -}; - -// Depending on the platform and compiler settings choose the faster implementation (of the same hash function) -#if (defined(GLib_64Bit)) && ! (defined(DEBUG) || defined(_DEBUG)) - typedef TPairHashImpl1 TPairHashImpl; -#else - typedef TPairHashImpl2 TPairHashImpl; -#endif - - -#endif diff --git a/inst/include/glib-core/bits.cpp b/inst/include/glib-core/bits.cpp deleted file mode 100644 index bbb45f8..0000000 --- a/inst/include/glib-core/bits.cpp +++ /dev/null @@ -1,208 +0,0 @@ -///////////////////////////////////////////////// -// One byte -const int TB1Def::B1Bits=8; -const int TB1Def::MxP2Exp=TB1Def::B1Bits-1; -const TB1Def::TB1 TB1Def::MxB1=0xFF; - -TB1Def::TB1Def(){ - B1P2T=new TB1[B1Bits+1]; B1P2T[0]=1; - for (int BitN=1; BitN=B1Def.B1P2T[L2])){L2++;} - return L2-1; -} - -bool TB1Def::GetBit(const int& BitN, const uchar& Val){ - IAssert((0<=BitN)&&(BitN<=TB1Def::MxP2Exp)); - return (Val & B1Def.B1P2T[BitN])!=0; -} - -const TB1Def TB1Def::B1Def; - -///////////////////////////////////////////////// -// Two bytes -const int TB2Def::B2Bits=16; -const int TB2Def::MxP2Exp=TB2Def::B2Bits-1; -const TB2Def::TB2 TB2Def::MxB2=0xFFFF; - -TB2Def::TB2Def(){ - B2P2T=new TB2[B2Bits+1]; B2P2T[0]=1; - for (int BitN=1; BitN>(0*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+ - TB1Def::B1Def.B1BitsT[(B2>>(1*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]; -} - -uint TB2Def::GetP2(const int& P2Exp){ - IAssert((0<=P2Exp)&&(P2Exp<=TB2Def::MxP2Exp)); - return B2Def.B2P2T[P2Exp]; -} - -int TB2Def::GetL2(const TB2& Val){ - int L2=0; - while ((L2=B2Def.B2P2T[L2])){L2++;} - return L2-1; -} - -const TB2Def TB2Def::B2Def; - -///////////////////////////////////////////////// -// Four bytes -const int TB4Def::B4Bits=32; -const int TB4Def::MxP2Exp=TB4Def::B4Bits-1; -const TB4Def::TB4 TB4Def::MxB4=0xFFFFFFFF; - -TB4Def::TB4Def(){ - B4P2T=new TB4[B4Bits+1]; B4P2T[0]=1; - for (int BitN=1; BitN>(0*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+ - TB1Def::B1Def.B1BitsT[(B4>>(1*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+ - TB1Def::B1Def.B1BitsT[(B4>>(2*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+ - TB1Def::B1Def.B1BitsT[(B4>>(3*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]; -} - -uint TB4Def::GetP2(const int& P2Exp){ - IAssert((0<=P2Exp)&&(P2Exp<=TB4Def::MxP2Exp)); - return B4Def.B4P2T[P2Exp]; -} - -int TB4Def::GetL2(const uint& Val){ - int L2=0; - while ((L2=B4Def.B4P2T[L2])){L2++;} - return L2-1; -} - -const TB4Def TB4Def::B4Def; - -///////////////////////////////////////////////// -// Flag-Set -const int TFSet::B4s=4; -const int TFSet::Bits=TFSet::B4s*TB4Def::B4Bits; - -TFSet::TFSet( - const int& FlagN1, const int& FlagN2, const int& FlagN3, - const int& FlagN4, const int& FlagN5, const int& FlagN6, - const int& FlagN7, const int& FlagN8, const int& FlagN9): - B4V(4, 4){ - if (FlagN1!=-1){Incl(FlagN1);} - if (FlagN2!=-1){Incl(FlagN2);} - if (FlagN3!=-1){Incl(FlagN3);} - if (FlagN4!=-1){Incl(FlagN4);} - if (FlagN5!=-1){Incl(FlagN5);} - if (FlagN6!=-1){Incl(FlagN6);} - if (FlagN7!=-1){Incl(FlagN7);} - if (FlagN8!=-1){Incl(FlagN8);} - if (FlagN9!=-1){Incl(FlagN9);} -} - -const TFSet TFSet::EmptyFSet; - -///////////////////////////////////////////////// -// Bit8-Set -const int TB8Set::Bits=8; - -TStr TB8Set::GetStr() const { - TChA ChA; - for (int BitN=0; BitN0)&&((B4T[B4s-1]&LastB4Mask)==(BSet.B4T[B4s-1]&BSet.LastB4Mask)); -} - -void TBSet::Gen(const int& _Bits){ - if (B4T!=NULL){delete[] B4T;} - Bits=_Bits; - B4T=new TB4Def::TB4[B4s=(Bits-1)/TB4Def::B4Def.B4Bits+1]; - LastB4Mask=TB4Def::B4Def.MxB4; - for (int BitN=Bits; BitN>MnBitN)==(BSet.B1>>MnBitN);} - uchar GetUCh() const {return B1;} - - void Incl(const int& BitN){ - Assert((0<=BitN)&&(BitN>MnBitN) & (TB1Def::B1Def.B1P2T[MxBitN-MnBitN+1]-1);} - - TB8Set& operator~(){B1=TB1Def::TB1(~B1); return *this;} - TB8Set& operator&=(const TB8Set& BSet){B1&=BSet.B1; return *this;} - TB8Set& operator|=(const TB8Set& BSet){B1|=BSet.B1; return *this;} - TB8Set& operator|=(const int& BitN){Incl(BitN); return *this;} - TB8Set& operator^=(const TB8Set& BSet){B1^=BSet.B1; return *this;} - TB8Set& operator>>=(const int& ShiftBits){B1>>=ShiftBits; return *this;} - TB8Set& operator<<=(const int& ShiftBits){B1<<=ShiftBits; return *this;} - - friend TB8Set operator~(const TB8Set& BSet){ - return ~TB8Set(BSet);} - friend TB8Set operator&(const TB8Set& LBSet, const TB8Set& RBSet){ - return TB8Set(LBSet)&=RBSet;} - friend TB8Set operator|(const TB8Set& LBSet, const TB8Set& RBSet){ - return TB8Set(LBSet)|=RBSet;} - friend TB8Set operator^(const TB8Set& LBSet, const TB8Set& RBSet){ - return TB8Set(LBSet)^=RBSet;} -}; -typedef TVec TB8SetV; - -///////////////////////////////////////////////// -// Bit32-Set -class TB32Set{ -private: - static const int Bits; - TB4Def::TB4 B4; -public: - TB32Set(): B4(0){} - TB32Set(const TB32Set& B32Set): B4(B32Set.B4){} - TB32Set(const uint& _B4): B4(_B4){} - TB32Set(TSIn& SIn){SIn.LoadBf(&B4, sizeof(TB4Def::TB4));} - void Save(TSOut& SOut) const {SOut.SaveBf(&B4, sizeof(TB4Def::TB4));} - - TB32Set& operator=(const TB32Set& BSet){B4=BSet.B4; return *this;} - bool operator==(const TB32Set& BSet) const {return B4==BSet.B4;} - bool operator<(const TB32Set& BSet) const {return B4>MnBitN)==(BSet.B4>>MnBitN);} - uint GetUInt() const {return B4;} - - void Incl(const int& BitN){ - Assert((0<=BitN)&&(BitN>MnBitN) & (TB4Def::B4Def.B4P2T[MxBitN-MnBitN+1]-1);} - - TB32Set& operator~(){B4=~B4; return *this;} - TB32Set& operator&=(const TB32Set& BSet){B4&=BSet.B4; return *this;} - TB32Set& operator|=(const TB32Set& BSet){B4|=BSet.B4; return *this;} - //TB32Set& operator|=(const int& BitN){Incl(BitN); return *this;} - TB32Set& operator^=(const TB32Set& BSet){B4^=BSet.B4; return *this;} - TB32Set& operator>>=(const int& ShiftBits){B4>>=ShiftBits; return *this;} - TB32Set& operator<<=(const int& ShiftBits){B4<<=ShiftBits; return *this;} - - friend TB32Set operator~(const TB32Set& BSet){ - return ~TB32Set(BSet);} - friend TB32Set operator&(const TB32Set& LBSet, const TB32Set& RBSet){ - return TB32Set(LBSet)&=RBSet;} - friend TB32Set operator|(const TB32Set& LBSet, const TB32Set& RBSet){ - return TB32Set(LBSet)|=RBSet;} - friend TB32Set operator^(const TB32Set& LBSet, const TB32Set& RBSet){ - return TB32Set(LBSet)^=RBSet;} -}; -typedef TVec TB32SetV; - -///////////////////////////////////////////////// -// Bit-Set -ClassTPV(TBSet, PBSet, TBSetV)//{ -private: - int B4s, Bits; - TB4Def::TB4 LastB4Mask; - TB4Def::TB4* B4T; - void SetLastB4(){B4T[B4s-1]&=LastB4Mask;} -public: - TBSet(): B4s(0), Bits(0), LastB4Mask(0), B4T(NULL){} - TBSet(const TBSet& BSet); - PBSet Clone() const {return PBSet(new TBSet(*this));} - TBSet(const int& _Bits): - B4s(0), Bits(0), LastB4Mask(0), B4T(NULL){Gen(_Bits);} - static PBSet New(const int& Bits){return PBSet(new TBSet(Bits));} - ~TBSet(){delete[] B4T;} - TBSet(TSIn& SIn){ - SIn.Load(B4s); SIn.Load(Bits); - SIn.LoadBf(&LastB4Mask, sizeof(TB4Def::TB4)); - B4T=(TB4Def::TB4*)SIn.LoadNewBf(B4s*sizeof(TB4Def::TB4));} - static PBSet Load(TSIn& SIn){return new TBSet(SIn);} - void Save(TSOut& SOut) const { - SOut.Save(B4s); SOut.Save(Bits); - SOut.SaveBf(&LastB4Mask, sizeof(TB4Def::TB4)); - SOut.SaveBf(B4T, B4s*sizeof(TB4Def::TB4));} - - TBSet& operator=(const TBSet& BSet); - bool operator==(const TBSet& BSet) const; - - void Gen(const int& _Bits); - void Clr(); - void Fill(); - - void Incl(const int& BitN){ - Assert((0<=BitN)&&(BitN=64); uint64 Val; memcpy(&Val, B4T, 8); return Val;} - void Wr(); - - TBSet& operator~(){ - for (int B4N=0; B4NPutStr(GetVersionStr()); -} - -void TBlobBs::AssertVersionStr(const PFRnd& FBlobBs){ - TStr CorrVersionStr=GetVersionStr(); - bool IsOk=false; - TStr TestVersionStr=FBlobBs->GetStr(CorrVersionStr.Len(), IsOk); - EAssert(IsOk && (CorrVersionStr==TestVersionStr)); -} - -TStr TBlobBs::GetBlobBsStateStr(const TBlobBsState& BlobBsState){ - TStr StateStr; - switch (BlobBsState){ - case bbsOpened: StateStr="Opened"; break; - case bbsClosed: StateStr="Closed"; break; - default: Fail; return TStr(); - } - EAssert(StateStr.Len()==GetStateStrLen()); - return StateStr; -} - -void TBlobBs::PutBlobBsStateStr(const PFRnd& FBlobBs, const TBlobBsState& State){ - FBlobBs->PutStr(GetBlobBsStateStr(State)); -} - -void TBlobBs::AssertBlobBsStateStr( - const PFRnd& FBlobBs, const TBlobBsState& State){ - TStr CorrStateStr=GetBlobBsStateStr(State); - bool IsOk; - TStr TestStateStr=FBlobBs->GetStr(GetStateStrLen(), IsOk); - if (!(IsOk && (CorrStateStr==TestStateStr))) { - TExcept::ThrowFull("Error in AssertBlobBsStateStr!", TStr(__FILE__)+" line "+TInt::GetStr(__LINE__)); - } -} - -const TStr TBlobBs::MxSegLenVNm="MxSegLen"; - -void TBlobBs::PutMxSegLen(const PFRnd& FBlobBs, const int& MxSegLen){ - FBlobBs->PutStr(MxSegLenVNm); - FBlobBs->PutInt(MxSegLen); -} - -int TBlobBs::GetMxSegLen(const PFRnd& FBlobBs){ - EAssert(FBlobBs->GetStr(MxSegLenVNm.Len())==MxSegLenVNm); - return FBlobBs->GetInt(); -} - -const TStr TBlobBs::BlockLenVNm="BlockLenV"; - -void TBlobBs::GenBlockLenV(TIntV& BlockLenV){ - BlockLenV.Clr(); - for (int P2Exp=0; P2ExpPutStr(BlockLenVNm); - FBlobBs->PutInt(BlockLenV.Len()); - for (int BlockLenN=0; BlockLenNPutInt(BlockLenV[BlockLenN]);} - FBlobBs->PutInt(-1); -} - -void TBlobBs::GetBlockLenV(const PFRnd& FBlobBs, TIntV& BlockLenV){ - EAssert(FBlobBs->GetStr(BlockLenVNm.Len())==BlockLenVNm); - BlockLenV.Gen(FBlobBs->GetInt()); - for (int BlockLenN=0; BlockLenNGetInt();} - EAssert(FBlobBs->GetInt()==-1); -} - -const TStr TBlobBs::FFreeBlobPtVNm="FFreeBlobPtV"; - -void TBlobBs::GenFFreeBlobPtV(const TIntV& BlockLenV, TBlobPtV& FFreeBlobPtV){ - FFreeBlobPtV.Gen(BlockLenV.Len()+1); -} - -void TBlobBs::PutFFreeBlobPtV(const PFRnd& FBlobBs, const TBlobPtV& FFreeBlobPtV){ - FBlobBs->PutStr(FFreeBlobPtVNm); - FBlobBs->PutInt(FFreeBlobPtV.Len()); - for (int BlockLenN=0; BlockLenNPutInt(-1); -} - -void TBlobBs::GetFFreeBlobPtV(const PFRnd& FBlobBs, TBlobPtV& FFreeBlobPtV){ - EAssert(FBlobBs->GetStr(FFreeBlobPtVNm.Len())==FFreeBlobPtVNm); - FFreeBlobPtV.Gen(FBlobBs->GetInt()); - for (int FFreeBlobPtN=0; FFreeBlobPtNGetInt()==-1); -} - -void TBlobBs::GetAllocInfo( - const int& BfL, const TIntV& BlockLenV, int& MxBfL, int& FFreeBlobPtN){ - int BlockLenN=0; - while ((BlockLenNBlockLenV[BlockLenN])){ - BlockLenN++;} - EAssert(BlockLenNPutUInt(GetBeginBlobTag()); break; - case btEnd: FBlobBs->PutUInt(GetEndBlobTag()); break; - default: Fail; - } -} - -void TBlobBs::AssertBlobTag(const PFRnd& FBlobBs, const TBlobTag& BlobTag){ - switch (BlobTag){ - case btBegin: EAssert(FBlobBs->GetUInt()==GetBeginBlobTag()); break; - case btEnd: EAssert(FBlobBs->GetUInt()==GetEndBlobTag()); break; - default: TExcept::Throw("Error asserting BlobTag"); - } -} - -void TBlobBs::PutBlobState(const PFRnd& FBlobBs, const TBlobState& State){ - FBlobBs->PutCh(char(State)); -} - -TBlobState TBlobBs::GetBlobState(const PFRnd& FBlobBs){ - return TBlobState(int(FBlobBs->GetCh())); -} - -void TBlobBs::AssertBlobState(const PFRnd& FBlobBs, const TBlobState& State){ - EAssert(TBlobState(FBlobBs->GetCh())==State); -} - -void TBlobBs::AssertBfCsEqFlCs(const TCs& BfCs, const TCs& FCs){ - if (BfCs!=FCs){ - printf("[%d:%d]\n", BfCs.Get(), FCs.Get());} - //EAssert(BfCs==FCs); -} - -///////////////////////////////////////////////// -// General-Blob-Base -TStr TGBlobBs::GetNrBlobBsFNm(const TStr& BlobBsFNm){ - TStr NrBlobBsFNm=BlobBsFNm; - if (NrBlobBsFNm.GetFExt().Empty()){ - NrBlobBsFNm=NrBlobBsFNm+".gbb";} - return NrBlobBsFNm; -} - -TGBlobBs::TGBlobBs( - const TStr& BlobBsFNm, const TFAccess& _Access, const int& _MxSegLen): - TBlobBs(), FBlobBs(), Access(_Access), MxSegLen(_MxSegLen), - BlockLenV(), FFreeBlobPtV(TB4Def::B4Bits), FirstBlobPt(){ - if (MxSegLen==-1){MxSegLen=MxBlobFLen;} - TStr NrBlobBsFNm=GetNrBlobBsFNm(BlobBsFNm); - switch (Access){ - case faCreate: - FBlobBs=TFRnd::New(NrBlobBsFNm, faCreate, true); break; - case faUpdate: - case faRdOnly: - case faRestore: - FBlobBs=TFRnd::New(NrBlobBsFNm, faUpdate, true); break; - default: Fail; - } - if (FBlobBs->Empty()){ - FBlobBs->SetFPos(0); - PutVersionStr(FBlobBs); - PutBlobBsStateStr(FBlobBs, bbsOpened); - PutMxSegLen(FBlobBs, MxSegLen); - GenBlockLenV(BlockLenV); - PutBlockLenV(FBlobBs, BlockLenV); - GenFFreeBlobPtV(BlockLenV, FFreeBlobPtV); - PutFFreeBlobPtV(FBlobBs, FFreeBlobPtV); - } else { - FBlobBs->SetFPos(0); - AssertVersionStr(FBlobBs); - int FPos=FBlobBs->GetFPos(); - if (Access!=faRestore){ - AssertBlobBsStateStr(FBlobBs, bbsClosed);} - if (Access!=faRdOnly){ - FBlobBs->SetFPos(FPos); - PutBlobBsStateStr(FBlobBs, bbsOpened); - } - MxSegLen=GetMxSegLen(FBlobBs); - GetBlockLenV(FBlobBs, BlockLenV); - GetFFreeBlobPtV(FBlobBs, FFreeBlobPtV); - } - FirstBlobPt=TBlobPt(FBlobBs->GetFPos()); - FBlobBs->Flush(); -} - -TGBlobBs::~TGBlobBs(){ - if (Access!=faRdOnly){ - FBlobBs->SetFPos(0); - PutVersionStr(FBlobBs); - PutBlobBsStateStr(FBlobBs, bbsClosed); - PutMxSegLen(FBlobBs, MxSegLen); - PutBlockLenV(FBlobBs, BlockLenV); - PutFFreeBlobPtV(FBlobBs, FFreeBlobPtV); - } - FBlobBs->Flush(); - FBlobBs=NULL; -} - -TBlobPt TGBlobBs::PutBlob(const PSIn& SIn){ - EAssert((Access==faCreate)||(Access==faUpdate)||(Access==faRestore)); - int BfL=SIn->Len(); - int MxBfL; int FFreeBlobPtN; - GetAllocInfo(BfL, BlockLenV, MxBfL, FFreeBlobPtN); - TBlobPt BlobPt; TCs Cs; - if (FFreeBlobPtV[FFreeBlobPtN].Empty()){ - int FLen=FBlobBs->GetFLen(); - if (FLen<=MxSegLen){ - EAssert(FLen<=MxBlobFLen); - BlobPt=TBlobPt(FLen); - FBlobBs->SetFPos(BlobPt.GetAddr()); - PutBlobTag(FBlobBs, btBegin); - FBlobBs->PutInt(MxBfL); - PutBlobState(FBlobBs, bsActive); - FBlobBs->PutInt(BfL); - FBlobBs->PutSIn(SIn, Cs); - FBlobBs->PutCh(TCh::NullCh, MxBfL-BfL); - FBlobBs->PutCs(Cs); - PutBlobTag(FBlobBs, btEnd); - } - } else { - BlobPt=FFreeBlobPtV[FFreeBlobPtN]; - FBlobBs->SetFPos(BlobPt.GetAddr()); - AssertBlobTag(FBlobBs, btBegin); - int MxBfL=FBlobBs->GetInt(); - int FPos=FBlobBs->GetFPos(); - AssertBlobState(FBlobBs, bsFree); - FFreeBlobPtV[FFreeBlobPtN]=TBlobPt::LoadAddr(FBlobBs); - FBlobBs->SetFPos(FPos); - PutBlobState(FBlobBs, bsActive); - FBlobBs->PutInt(BfL); - FBlobBs->PutSIn(SIn, Cs); - FBlobBs->PutCh(TCh::NullCh, MxBfL-BfL); - FBlobBs->PutCs(Cs); - AssertBlobTag(FBlobBs, btEnd); - } - FBlobBs->Flush(); - return BlobPt; -} - -TBlobPt TGBlobBs::PutBlob(const TBlobPt& BlobPt, const PSIn& SIn){ - EAssert((Access==faCreate)||(Access==faUpdate)||(Access==faRestore)); - int BfL=SIn->Len(); - - FBlobBs->SetFPos(BlobPt.GetAddr()); - AssertBlobTag(FBlobBs, btBegin); - int MxBfL=FBlobBs->GetInt(); - AssertBlobState(FBlobBs, bsActive); - if (BfL>MxBfL){ - DelBlob(BlobPt); - return PutBlob(SIn); - } else { - TCs Cs; - FBlobBs->PutInt(BfL); - FBlobBs->PutSIn(SIn, Cs); - FBlobBs->PutCh(TCh::NullCh, MxBfL-BfL); - FBlobBs->PutCs(Cs); - PutBlobTag(FBlobBs, btEnd); - FBlobBs->Flush(); - return BlobPt; - } -} - -PSIn TGBlobBs::GetBlob(const TBlobPt& BlobPt){ - FBlobBs->SetFPos(BlobPt.GetAddr()); - AssertBlobTag(FBlobBs, btBegin); - int MxBfL=FBlobBs->GetInt(); - AssertBlobState(FBlobBs, bsActive); - int BfL=FBlobBs->GetInt(); - TCs BfCs; PSIn SIn=FBlobBs->GetSIn(BfL, BfCs); - FBlobBs->MoveFPos(MxBfL-BfL); - TCs FCs=FBlobBs->GetCs(); - AssertBlobTag(FBlobBs, btEnd); - AssertBfCsEqFlCs(BfCs, FCs); - return SIn; -} - -void TGBlobBs::DelBlob(const TBlobPt& BlobPt){ - EAssert((Access==faCreate)||(Access==faUpdate)||(Access==faRestore)); - FBlobBs->SetFPos(BlobPt.GetAddr()); - AssertBlobTag(FBlobBs, btBegin); - int MxBfL=FBlobBs->GetInt(); - int FPos=FBlobBs->GetFPos(); - AssertBlobState(FBlobBs, bsActive); - /*int BfL=*/FBlobBs->GetInt(); - FBlobBs->SetFPos(FPos); - PutBlobState(FBlobBs, bsFree); - int _MxBfL; int FFreeBlobPtN; - GetAllocInfo(MxBfL, BlockLenV, _MxBfL, FFreeBlobPtN); - EAssert(MxBfL==_MxBfL); - FFreeBlobPtV[FFreeBlobPtN].SaveAddr(FBlobBs); - FFreeBlobPtV[FFreeBlobPtN]=BlobPt; - FBlobBs->PutCh(TCh::NullCh, MxBfL+sizeof(TCs)); - AssertBlobTag(FBlobBs, btEnd); - FBlobBs->Flush(); -} - -TBlobPt TGBlobBs::FFirstBlobPt(){ - return FirstBlobPt; -} - -bool TGBlobBs::FNextBlobPt(TBlobPt& TrvBlobPt, TBlobPt& BlobPt, PSIn& BlobSIn){ - forever { - uint TrvBlobAddr=TrvBlobPt.GetAddr(); - if (TrvBlobAddr>=uint(FBlobBs->GetFLen())){ - TrvBlobPt.Clr(); BlobPt.Clr(); BlobSIn=NULL; - return false; - } else { - FBlobBs->SetFPos(TrvBlobAddr); - AssertBlobTag(FBlobBs, btBegin); - int MxBfL=FBlobBs->GetInt(); - TBlobState BlobState=GetBlobState(FBlobBs); - switch (BlobState){ - case bsActive:{ - int BfL=FBlobBs->GetInt(); - TCs BfCs; BlobSIn=FBlobBs->GetSIn(BfL, BfCs); - FBlobBs->MoveFPos(MxBfL-BfL); - TCs FCs=FBlobBs->GetCs(); - AssertBlobTag(FBlobBs, btEnd); - AssertBfCsEqFlCs(BfCs, FCs); - BlobPt=TrvBlobPt; - TrvBlobPt=TBlobPt(FBlobBs->GetFPos()); - return true;} - case bsFree: - FBlobBs->MoveFPos(sizeof(uint)+MxBfL+sizeof(TCs)); - AssertBlobTag(FBlobBs, btEnd); - TrvBlobPt=TBlobPt(FBlobBs->GetFPos()); - break; - default: Fail; return false; - } - } - } -} - -bool TGBlobBs::Exists(const TStr& BlobBsFNm){ - TStr NrBlobBsFNm=GetNrBlobBsFNm(BlobBsFNm); - return TFile::Exists(NrBlobBsFNm); -} - -///////////////////////////////////////////////// -// Multiple-File-Blob-Base -void TMBlobBs::GetNrFPathFMid( - const TStr& BlobBsFNm, TStr& NrFPath, TStr& NrFMid){ - NrFPath=TStr::GetNrFPath(BlobBsFNm.GetFPath()); - NrFMid=TStr::GetNrFMid(BlobBsFNm.GetFMid()); -} - -TStr TMBlobBs::GetMainFNm( - const TStr& NrFPath, const TStr& NrFMid){ - return NrFPath+NrFMid+".mbb"; -} - -TStr TMBlobBs::GetSegFNm( - const TStr& NrFPath, const TStr& NrFMid, const int& SegN){ - return NrFPath+NrFMid+".mbb"+""+TStr::GetNrNumFExt(SegN); -} - -void TMBlobBs::LoadMain(int& Segs){ - PSIn SIn=TFIn::New(GetMainFNm(NrFPath, NrFMid)); - TILx Lx(SIn, TFSet()|oloFrcEoln|oloSigNum|oloCsSens); - EAssert(Lx.GetVarStr("Version")==GetVersionStr()); - MxSegLen=Lx.GetVarInt("MxSegLen"); - Segs=Lx.GetVarInt("Segments"); -} - -void TMBlobBs::SaveMain() const { - PSOut SOut=TFOut::New(GetMainFNm(NrFPath, NrFMid)); - TOLx Lx(SOut, TFSet()|oloFrcEoln|oloSigNum|oloCsSens); - Lx.PutVarStr("Version", GetVersionStr()); - Lx.PutVarInt("MxSegLen", MxSegLen); - Lx.PutVarInt("Segments", SegV.Len()); -} - -TMBlobBs::TMBlobBs( - const TStr& BlobBsFNm, const TFAccess& _Access, const int& _MxSegLen): - TBlobBs(), Access(_Access), MxSegLen(_MxSegLen), - NrFPath(), NrFMid(), SegV(), CurSegN(0){ - if (MxSegLen==-1){MxSegLen=MxBlobFLen;} - GetNrFPathFMid(BlobBsFNm, NrFPath, NrFMid); - switch (Access){ - case faCreate:{ - TFile::DelWc(BlobBsFNm+".*"); - TStr SegFNm=GetSegFNm(NrFPath, NrFMid, 0); - PBlobBs Seg=TGBlobBs::New(SegFNm, faCreate, MxSegLen); - SegV.Add(Seg); - SaveMain(); break;} - case faUpdate: - case faRdOnly:{ - int Segs; LoadMain(Segs); - for (int SegN=0; SegNPutBlob(SIn); - if (BlobPt.Empty()){ - for (uchar SegN=0; SegNPutBlob(SIn); - if (!BlobPt.Empty()){break;} - } - if (BlobPt.Empty()){ - TStr SegFNm=GetSegFNm(NrFPath, NrFMid, SegV.Len()); - PBlobBs Seg=TGBlobBs::New(SegFNm, faCreate, MxSegLen); - CurSegN=SegV.Add(Seg); EAssert(CurSegN<=255); - BlobPt=SegV[CurSegN]->PutBlob(SIn); - } - } - if (!BlobPt.Empty()){ - BlobPt.PutSeg(uchar(CurSegN));} - return BlobPt; -} - -TBlobPt TMBlobBs::PutBlob(const TBlobPt& BlobPt, const PSIn& SIn){ - EAssert((Access==faCreate)||(Access==faUpdate)||(Access==faRestore)); - int SegN=BlobPt.GetSeg(); - TBlobPt NewBlobPt=SegV[SegN]->PutBlob(BlobPt, SIn); - if (NewBlobPt.Empty()){ - NewBlobPt=PutBlob(SIn); - } else { - NewBlobPt.PutSeg(BlobPt.GetSeg()); - } - return NewBlobPt; -} - -PSIn TMBlobBs::GetBlob(const TBlobPt& BlobPt){ - int SegN=BlobPt.GetSeg(); - return SegV[SegN]->GetBlob(BlobPt); -} - -void TMBlobBs::DelBlob(const TBlobPt& BlobPt){ - int SegN=BlobPt.GetSeg(); - SegV[SegN]->DelBlob(BlobPt); -} - -TBlobPt TMBlobBs::GetFirstBlobPt(){ - return SegV[0]->GetFirstBlobPt(); -} - -TBlobPt TMBlobBs::FFirstBlobPt(){ - return SegV[0]->FFirstBlobPt(); -} - -bool TMBlobBs::FNextBlobPt(TBlobPt& TrvBlobPt, TBlobPt& BlobPt, PSIn& BlobSIn){ - uchar SegN=TrvBlobPt.GetSeg(); - if (SegV[SegN]->FNextBlobPt(TrvBlobPt, BlobPt, BlobSIn)){ - TrvBlobPt.PutSeg(SegN); - BlobPt.PutSeg(SegN); - return true; - } else - if (SegN==SegV.Len()-1){ - return false; - } else { - SegN++; - TrvBlobPt=SegV[SegN]->FFirstBlobPt(); - TrvBlobPt.PutSeg(SegN); - return FNextBlobPt(TrvBlobPt, BlobPt, BlobSIn); - } -} - -bool TMBlobBs::Exists(const TStr& BlobBsFNm){ - TStr NrFPath; TStr NrFMid; GetNrFPathFMid(BlobBsFNm, NrFPath, NrFMid); - TStr MainFNm=GetMainFNm(NrFPath, NrFMid); - return TFile::Exists(MainFNm); -} diff --git a/inst/include/glib-core/blobbs.h b/inst/include/glib-core/blobbs.h deleted file mode 100644 index 39fdbfa..0000000 --- a/inst/include/glib-core/blobbs.h +++ /dev/null @@ -1,223 +0,0 @@ -#include "bd.h" - -///////////////////////////////////////////////// -// Blob-Pointer -ClassTVQ(TBlobPt, TBlobPtV, TBlobPtQ)//{ -public: - static const int MnBlobBfL; - static const int Flags; - uchar Seg; - uint Addr; - TB8Set FSet1, FSet2, FSet3; -public: - TBlobPt(): - Seg(0), Addr(TUInt::Mx), FSet1(), FSet2(), FSet3(){} - TBlobPt(const TBlobPt& Pt): - Seg(Pt.Seg), Addr(Pt.Addr), - FSet1(Pt.FSet1), FSet2(Pt.FSet2), FSet3(Pt.FSet3){} - TBlobPt(const uchar& _Seg, const uint& _Addr, - const TB8Set& _FSet1, const TB8Set& _FSet2, const TB8Set& _FSet3): - Seg(_Seg), Addr(_Addr), FSet1(_FSet1), FSet2(_FSet2), FSet3(_FSet3){} - TBlobPt(const uchar& _Seg, const uint& _Addr): - Seg(_Seg), Addr(_Addr), FSet1(), FSet2(), FSet3(){} - TBlobPt(const uint& _Addr): - Seg(0), Addr(_Addr), FSet1(), FSet2(), FSet3(){} - TBlobPt(const int& _Addr): - Seg(0), Addr(uint(_Addr)), FSet1(), FSet2(), FSet3(){IAssert(_Addr>=0);} - ~TBlobPt(){} - TBlobPt(TSIn& SIn){ - SIn.Load(Seg); SIn.Load(Addr); - FSet1=TB8Set(SIn); FSet2=TB8Set(SIn); FSet3=TB8Set(SIn);} - void Save(TSOut& SOut) const { - SOut.Save(Seg); SOut.Save(Addr); - FSet1.Save(SOut); FSet2.Save(SOut); FSet3.Save(SOut);} - - TBlobPt& operator=(const TBlobPt& Pt){ - if (this!=&Pt){ - Seg=Pt.Seg; Addr=Pt.Addr; - FSet1=Pt.FSet1; FSet2=Pt.FSet2; FSet3=Pt.FSet3;} - return *this;} - bool operator==(const TBlobPt& Pt) const { - return (Seg==Pt.Seg)&&(Addr==Pt.Addr);} - bool operator<(const TBlobPt& Pt) const { - return (SegGetUCh(); uint Addr=FRnd->GetUInt(); - TB8Set B8Set1(FRnd->GetUCh()); TB8Set B8Set2(FRnd->GetUCh()); - TB8Set B8Set3(FRnd->GetUCh()); - return TBlobPt(Seg, Addr, B8Set1, B8Set2, B8Set3);} - void Save(const PFRnd& FRnd) const { - FRnd->PutUCh(Seg); FRnd->PutUInt(Addr); - FRnd->PutUCh(FSet1.GetUCh()); FRnd->PutUCh(FSet2.GetUCh()); - FRnd->PutUCh(FSet3.GetUCh());} - static TBlobPt LoadAddr(const PFRnd& FRnd, const uchar& Seg=0){ - return TBlobPt(Seg, FRnd->GetUInt());} - void SaveAddr(const PFRnd& FRnd) const { - FRnd->PutUInt(Addr);} - - TStr GetAddrStr() const { - TChA AddrChA; AddrChA+=TInt::GetStr(Seg); AddrChA+=':'; - AddrChA+=TUInt::GetStr(Addr); return AddrChA;} - - TStr GetStr() const; -}; - -///////////////////////////////////////////////// -// Blob-Base -typedef enum {bbsUndef, bbsOpened, bbsClosed} TBlobBsState; -typedef enum {btUndef, btBegin, btEnd} TBlobTag; -typedef enum {bsUndef, bsActive, bsFree} TBlobState; - -ClassTPV(TBlobBs, PBlobBs, TBlobBsV)//{ -public: - static const int MnBlobBfL; - static const int MxBlobFLen; - UndefCopyAssign(TBlobBs); -public: - TBlobBs(){} - virtual ~TBlobBs(){} - TBlobBs(TSIn&){Fail;} - static PBlobBs Load(TSIn&){Fail; return NULL;} - void Save(TSOut&) const {Fail;} - - virtual TStr GetVersionStr() const=0; - void PutVersionStr(const PFRnd& FBlobBs); - void AssertVersionStr(const PFRnd& FBlobBs); - - TStr GetBlobBsStateStr(const TBlobBsState& BlobBsState); - int GetStateStrLen(){return 6;} - void PutBlobBsStateStr(const PFRnd& FBlobBs, const TBlobBsState& State); - void AssertBlobBsStateStr(const PFRnd& FBlobBs, const TBlobBsState& State); - - static const TStr MxSegLenVNm; - void PutMxSegLen(const PFRnd& FBlobBs, const int& MxSegLen); - int GetMxSegLen(const PFRnd& FBlobBs); - - static const TStr BlockLenVNm; - void GenBlockLenV(TIntV& BlockLenV); - void PutBlockLenV(const PFRnd& FBlobBs, const TIntV& BlockLenV); - void GetBlockLenV(const PFRnd& FBlobBs, TIntV& BlockLenV); - - static const TStr FFreeBlobPtVNm; - void GenFFreeBlobPtV(const TIntV& BlockLenV, TBlobPtV& FFreeBlobPtV); - void PutFFreeBlobPtV(const PFRnd& FBlobBs, const TBlobPtV& FFreeBlobPtV); - void GetFFreeBlobPtV(const PFRnd& FBlobBs, TBlobPtV& FFreeBlobPtV); - - void GetAllocInfo( - const int& BfL, const TIntV& BlockLenV, int& MxBfL, int& FFreeBlobPtN); - - uint GetBeginBlobTag(){return 0xABCDEFFF;} - uint GetEndBlobTag(){return 0xFFFEDCBA;} - void PutBlobTag(const PFRnd& FBlobBs, const TBlobTag& BlobTag); - void AssertBlobTag(const PFRnd& FBlobBs, const TBlobTag& BlobTag); - - void PutBlobState(const PFRnd& FBlobBs, const TBlobState& State); - TBlobState GetBlobState(const PFRnd& FBlobBs); - void AssertBlobState(const PFRnd& FBlobBs, const TBlobState& State); - - void AssertBfCsEqFlCs(const TCs& BfCs, const TCs& FCs); - - virtual TBlobPt PutBlob(const PSIn& SIn)=0; - TBlobPt PutBlob(const TStr& Str){ - PSIn SIn=TStrIn::New(Str); return PutBlob(SIn);} - virtual TBlobPt PutBlob(const TBlobPt& BlobPt, const PSIn& SIn)=0; - virtual PSIn GetBlob(const TBlobPt& BlobPt)=0; - virtual void DelBlob(const TBlobPt& BlobPt)=0; - - virtual TBlobPt GetFirstBlobPt()=0; - virtual TBlobPt FFirstBlobPt()=0; - virtual bool FNextBlobPt(TBlobPt& TrvBlobPt, TBlobPt& BlobPt, PSIn& BlobSIn)=0; - bool FNextBlobPt(TBlobPt& TrvBlobPt, PSIn& BlobSIn){ - TBlobPt BlobPt; return FNextBlobPt(TrvBlobPt, BlobPt, BlobSIn);} -}; - -///////////////////////////////////////////////// -// General-Blob-Base -class TGBlobBs: public TBlobBs{ -private: - PFRnd FBlobBs; - TFAccess Access; - int MxSegLen; - TIntV BlockLenV; - TBlobPtV FFreeBlobPtV; - TBlobPt FirstBlobPt; - static TStr GetNrBlobBsFNm(const TStr& BlobBsFNm); -public: - TGBlobBs(const TStr& BlobBsFNm, const TFAccess& _Access=faRdOnly, - const int& _MxSegLen=-1); - static PBlobBs New(const TStr& BlobBsFNm, const TFAccess& Access=faRdOnly, - const int& MxSegLen=-1){ - return PBlobBs(new TGBlobBs(BlobBsFNm, Access, MxSegLen));} - ~TGBlobBs(); - - TGBlobBs& operator=(const TGBlobBs&){Fail; return *this;} - - TStr GetVersionStr() const {return TStr("General Blob Base Format 1.0");} - TBlobPt PutBlob(const PSIn& SIn); - TBlobPt PutBlob(const TBlobPt& BlobPt, const PSIn& SIn); - PSIn GetBlob(const TBlobPt& BlobPt); - void DelBlob(const TBlobPt& BlobPt); - - TBlobPt GetFirstBlobPt(){return FirstBlobPt;} - TBlobPt FFirstBlobPt(); - bool FNextBlobPt(TBlobPt& TrvBlobPt, TBlobPt& BlobPt, PSIn& BlobSIn); - - static bool Exists(const TStr& BlobBsFNm); -}; - -///////////////////////////////////////////////// -// Multiple-File-Blob-Base -class TMBlobBs: public TBlobBs{ -private: - TFAccess Access; - int MxSegLen; - TStr NrFPath, NrFMid; - TBlobBsV SegV; - int CurSegN; - static void GetNrFPathFMid(const TStr& BlobBsFNm, TStr& NrFPath, TStr& NrFMid); - static TStr GetMainFNm(const TStr& NrFPath, const TStr& NrFMid); - static TStr GetSegFNm(const TStr& NrFPath, const TStr& NrFMid, const int& SegN); - void LoadMain(int& Segs); - void SaveMain() const; -public: - TMBlobBs(const TStr& BlobBsFNm, const TFAccess& _Access=faRdOnly, - const int& _MxSegLen=-1); - static PBlobBs New(const TStr& BlobBsFNm, const TFAccess& Access=faRdOnly, - const int& MxSegLen=-1){ - return PBlobBs(new TMBlobBs(BlobBsFNm, Access, MxSegLen));} - ~TMBlobBs(); - - TMBlobBs& operator=(const TMBlobBs&){Fail; return *this;} - - TStr GetVersionStr() const { - return TStr("Multiple-File Blob Base Format 1.0");} - TBlobPt PutBlob(const PSIn& SIn); - TBlobPt PutBlob(const TBlobPt& BlobPt, const PSIn& SIn); - PSIn GetBlob(const TBlobPt& BlobPt); - void DelBlob(const TBlobPt& BlobPt); - - TBlobPt GetFirstBlobPt(); - TBlobPt FFirstBlobPt(); - bool FNextBlobPt(TBlobPt& TrvBlobPt, TBlobPt& BlobPt, PSIn& BlobSIn); - - static bool Exists(const TStr& BlobBsFNm); -}; - diff --git a/inst/include/glib-core/doc/bd.h.txt b/inst/include/glib-core/doc/bd.h.txt deleted file mode 100644 index 5af79d5..0000000 --- a/inst/include/glib-core/doc/bd.h.txt +++ /dev/null @@ -1,8 +0,0 @@ -/// TPairHashImpl1 -Both \c TPairHashImpl1 and \c TPairHashImpl2 implement the same function: ((a + b) * (a + b + 1) / 2) + a. -/// - -/// TPairHashImpl2 -Both \c TPairHashImpl1 and \c TPairHashImpl2 implement the same function: ((a + b) * (a + b + 1) / 2) + a. -/// - diff --git a/inst/include/glib-core/doc/ds.h.txt b/inst/include/glib-core/doc/ds.h.txt deleted file mode 100644 index 5a7b870..0000000 --- a/inst/include/glib-core/doc/ds.h.txt +++ /dev/null @@ -1,209 +0,0 @@ -/// TVec -Internally, vectors use a dynamically allocated array to store their elements. -This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. -This is a relatively expensive task in terms of processing time. -Vectors may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). -The reallocations only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity. -Use TSizeTy=int for vectors of maximum size of 2 billion (2^31) and TSizeTy=int64 for vectors that can store up to 2^61 elements. -/// - -/// TVec::TVec -The data is not copied and the vector does not own the memory (i.e., the -vector won't free the memory at destruction). -/// - -/// TVec::LoadShM -The buffer is mapped straight to memory, so this method should not -be used if initialization of the elements is needed. Disallowed operations -for shared memory vectors include editing values in the vector directly, -copying into the vector, and truncating the vector -/// - -/// TVec::Less -For example, (a,b) < (a',b') if and only if a < a' or (a = a' and b < b'). -/// - -/// TVec::GenExt -The data is not copied and the vector does not own the memory (the vector -won't free the memory at destruction). -/// - -/// TVec::IsExt -In this case the vector does not own the memory (and won't free the memory at -destruction). -/// - -/// TVec::Clr -Vector's memory gets deallocated only if DoDel=true or if vector's -capacity is greater than \c NoDelLim. -/// - -/// TVec::Trunc -If _Vals=-1 then the capacity is reduced to match vector's length. -/// - -/// TVec::MoveFrom -No memory gets copied and \c Vec gets destroyed. -/// - -/// TVec::Empty -This means the vector contains zero elements (while its capacity may be greater than zero). -/// - -/// TVec::Len -This is the number of actual objects held in the vector, which is not -necessarily equal to its storage capacity. -/// - -/// TVec::Add -This increases the vector size by one. -/// - -/// TVec::Add1 -The content of \c Val is copied to the new element. -/// - -/// TVec::Add2 -If vector's capacity needs to be increased, it is increased by \c ResizeLen elements. -/// - -/// TVec::AddMP -The method assumes that the space has been preallocated. It does not perform any checks and it does not resize the vector space. The method is thread-safe. -/// - -/// TVec::MoveLastMP -The method assumes that the space has been preallocated. It does not perform any checks and it does not resize the vector space. The method is thread-safe. -/// - -/// TVec::AddSorted -@param Asc Adds the element so that ascending (if \c true) or descending (if \c false) order is maintained. -/// - -/// TVec::AddBackSorted -@param Asc Adds the element so that ascending (if \c true) or descending (if \c false) order is maintained. -/// - -/// TVec::AddMerged -Uses binary search to check whether an element is already in the vector. -/// - -/// TVec::AddMerged1 -Uses binary search to check whether an element is already in the vector. -/// - -/// TVec::AddUnique -Does not assume the vector to be sorted and thus uses linear search to check -whether \c Val is already in the vector. -/// - -/// TVec::NextPerm -Assuming we started with a sorted vector repeated calls to \c NextPerm() will -generate all permutations of the elements of the vector. Returns \c false when -the last permutation is reached. -/// - -/// TVec::PrevPerm -Returns \c false when the first permutation is reached. -/// - -/// TVec::BSort -@param Asc Sorts the elements in ascending (if \c true) or descending (if \c false) order. -/// - -/// TVec::ISort -@param Asc Sorts the elements in ascending (if \c true) or descending (if \c false) order. -/// - -/// TVec::Partition -Helper function used by \c QSort(). -@param Asc Sorts the elements in ascending (if \c true) or descending (if \c false) order. -/// - -/// TVec::QSort -Helper function used by \c Sort(). -@param Asc Sorts the elements in ascending (if \c true) or descending (if \c false) order. -/// - -/// TVec::Sort -Use a combination if quicksort and insertion sort. -@param Asc Sorts the elements in ascending (if \c true) or descending (if \c false) order. -/// - -/// TVec::Diff -This vector keeps only elements that do not appear in \c ValV. -/// - -/// TVec::Diff1 -\c DstValV has all the elements of this vector that do not appear in \c ValV. -/// - -/// TVec::SearchBin -If the element is not found return value is -1. Uses binary search and thus -assumes the vector is sorted. -/// - -/// TVec::SearchBin1 -If the element is not found return value is -1. Uses binary search and thus assumes the vector is sorted. -/// - -/// TVec::SearchBinLeft -If the element is not found return value is -1. Uses binary search and thus assumes the vector is sorted. -/// - -/// TVec::SearchForw -If the element is not found return value is -1. Uses linear search starting at -position \c BValN. -/// - -/// TVec::SearchBack -If the element is not found return value is -1. Uses backward linear search. -/// - -/// TVec::SearchVForw -If the vector is not found return value is -1. -/// - -/// TVec::IsIn -Position of \c Val is returned in \c ValN. -/// - -/// TVec::IsInBin -Uses binary search and thus assumes the vector is sorted. -/// - -/// TVec::GetAddDat -If the element does not exist, we add it at the end of the vector. -/// - -/// TVecPool -Used for storing a large number of small vectors. -The pool can store up to 2G different vectors, each with up to 2G elements. Each vector in the pool gets a consecutive integer ID. IDs range 0...GetVecs(). Once a vector is added to the pool, the vector can modify the values of its elements (e.g., be sorted), but the vector is not allowed to change its length --- it cannot grow or shrink. -/// - -/// TVecPool::TVecPool -@param ExpectVals At creation the pool allocates enough memory for storing the total of \c ExpectVals elements (not vectors). -@param _GrowBy Whenever the size of the pool needs to be expanded, it will be expanded to be able to store additional \c _GrowBy elements. -@param _FastCopy If \c true, then vectors are copied using \c memcpy(), otherwise the assignment operator is used for copying. This option is slower but useful for complex objects where assignment operator is non-trivial. -@param _EmptyVal Empty (not yet used) elements in the pool are assigned to this value. By default _EmptyVal = TVal(). -/// - -/// TVecPool::AddEmptyV -Elements of the vector are initialized to \c EmptyVal. -/// - -/// TVecPool::GetV -No data is copied. Elements of the vector \c ValV can be modified but the vector cannot change its size. -/// - -/// TVecPool::CompactPool -Empty space is left at the end of the pool. -/// - -/// TVecPool::ShuffleAll -It does not respect vector boundaries! -/// - -/// TVecPool::Clr -If DoDel=true memory is freed, otherwise all vectors are deleted and all element values in the pool are set to \c EmptyVal. -/// - diff --git a/inst/include/glib-core/doc/ss.h.txt b/inst/include/glib-core/doc/ss.h.txt deleted file mode 100644 index 62db748..0000000 --- a/inst/include/glib-core/doc/ss.h.txt +++ /dev/null @@ -1,32 +0,0 @@ -/// TSs -Useful for parsing small spreadsheets that represent tables of X columns and Y rows. The whole spreadsheet is read directly into memory. -/// - -/// TSsParser -Useful for fast parsing large files that contain variable number of fields separated by a particular character. See \v TSsFmt for the definitions of separators (space, tab, comma, etc.) supported by the class. The class reads the input file one line at a time. Each line is parsed and a vector of fields is returned. The class can process raw text files as well as compressed files (.gz, .7z, .zip, .7z). This means there is no need to first uncompress a file and load it. Refer to \v TZipIn for documentation on how to directly load compressed files. Lines starting with '#' can be considered as comments and the parser can skip them. -/// - -/// TSsParser::TSsParser1 -@param FNm Input filename. Can be a text file or a compressed file. -@param _SsFmt Spread-sheet separator format. Each line will be broken in a set of fields, where the boundary between the fields is defined by the \c _SsFmt. -@param _SkipLeadBlanks If \c true leading/trailing white-spaces of the line will be ignored. -@param _SkipCmt If \c true lines starting with '#' will be considered as comments and will be skipped. -@param _SkipEmptyFld If \c true then empty fields (consecutive occurrences of the separator) will be ignored. -/// - -/// TSsParser::TSsParser2 -@param FNm Input filename. Can be a text file or a compressed file. -@param Separator Spread-sheet separator character. Each line will be broken in a set of fields, where the boundary between the fields is the \c Separator character. -@param _SkipLeadBlanks If \c true leading/trailing white-spaces of the line will be ignored. -@param _SkipCmt If \c true lines starting with '#' will be considered as comments and will be skipped. -@param _SkipEmptyFld If \c true then empty fields (consecutive occurrences of the separator) will be ignored. -/// - -/// TSsParser::Next -If end of file is reached, return value is \c false. -/// - -/// TSsParser::NextSlow -If end of file is reached, return value is \c false. -This function is deprecated, use Next instead. -/// diff --git a/inst/include/glib-core/doc/ssmp.h.txt b/inst/include/glib-core/doc/ssmp.h.txt deleted file mode 100644 index 66a2207..0000000 --- a/inst/include/glib-core/doc/ssmp.h.txt +++ /dev/null @@ -1,32 +0,0 @@ -/// TSs -Useful for parsing small spreadsheets that represent tables of X columns and Y rows. The whole spreadsheet is read directly into memory. -/// - -/// TSsParserMP -Useful for fast parsing large files that contain variable number of fields separated by a particular character. See \v TSsFmt for the definitions of separators (space, tab, comma, etc.) supported by the class. The class reads the input file one line at a time. Each line is parsed and a vector of fields is returned. The class can process raw text files as well as compressed files (.gz, .7z, .zip, .7z). This means there is no need to first uncompress a file and load it. Refer to \v TZipIn for documentation on how to directly load compressed files. Lines starting with '#' can be considered as comments and the parser can skip them. -/// - -/// TSsParserMP::TSsParserMP1 -@param FNm Input filename. Can be a text file or a compressed file. -@param _SsFmt Spread-sheet separator format. Each line will be broken in a set of fields, where the boundary between the fields is defined by the \c _SsFmt. -@param _SkipLeadBlanks If \c true leading/trailing white-spaces of the line will be ignored. -@param _SkipCmt If \c true lines starting with '#' will be considered as comments and will be skipped. -@param _SkipEmptyFld If \c true then empty fields (consecutive occurrences of the separator) will be ignored. -/// - -/// TSsParserMP::TSsParserMP2 -@param FNm Input filename. Can be a text file or a compressed file. -@param Separator Spread-sheet separator character. Each line will be broken in a set of fields, where the boundary between the fields is the \c Separator character. -@param _SkipLeadBlanks If \c true leading/trailing white-spaces of the line will be ignored. -@param _SkipCmt If \c true lines starting with '#' will be considered as comments and will be skipped. -@param _SkipEmptyFld If \c true then empty fields (consecutive occurrences of the separator) will be ignored. -/// - -/// TSsParserMP::Next -If end of file is reached, return value is \c false. -/// - -/// TSsParserMP::NextSlow -If end of file is reached, return value is \c false. -This function is deprecated, use Next instead. -/// diff --git a/inst/include/glib-core/ds.h b/inst/include/glib-core/ds.h deleted file mode 100644 index eb3224d..0000000 --- a/inst/include/glib-core/ds.h +++ /dev/null @@ -1,2821 +0,0 @@ -///////////////////////////////////////////////// -// Address-Pointer -template -class TAPt{ -private: - TRec* Addr; -public: - TAPt(): Addr(NULL){} - TAPt(const TAPt& Pt): Addr(Pt.Addr){} - TAPt(TRec* _Addr): Addr(_Addr){} - TAPt(TSIn&){Fail;} - void Save(TSOut&) const {Fail;} - - TAPt& operator=(const TAPt& Pt){Addr=Pt.Addr; return *this;} - TAPt& operator=(TRec* _Addr){Addr=_Addr; return *this;} - bool operator==(const TAPt& Pt) const {return *Addr==*Pt.Addr;} - bool operator!=(const TAPt& Pt) const {return *Addr!=*Pt.Addr;} - bool operator<(const TAPt& Pt) const {return *Addr<*Pt.Addr;} - - TRec* operator->() const {Assert(Addr!=NULL); return Addr;} - TRec& operator*() const {Assert(Addr!=NULL); return *Addr;} - TRec& operator[](const int& RecN) const { - Assert(Addr!=NULL); return Addr[RecN];} - TRec* operator()() const {return Addr;} - - bool Empty() const {return Addr==NULL;} -}; - -///////////////////////////////////////////////// -// Pair -template -class TPair{ -public: - TVal1 Val1; - TVal2 Val2; -public: - TPair(): Val1(), Val2(){} - TPair(const TPair& Pair): Val1(Pair.Val1), Val2(Pair.Val2){} - TPair(const TVal1& _Val1, const TVal2& _Val2): Val1(_Val1), Val2(_Val2){} - explicit TPair(TSIn& SIn): Val1(SIn), Val2(SIn){} - void Save(TSOut& SOut) const { - Val1.Save(SOut); Val2.Save(SOut);} - void Load(TSIn& SIn) {Val1.Load(SIn); Val2.Load(SIn);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TPair& operator=(const TPair& Pair){ - if (this!=&Pair){Val1=Pair.Val1; Val2=Pair.Val2;} return *this;} - bool operator==(const TPair& Pair) const { - return (Val1==Pair.Val1)&&(Val2==Pair.Val2);} - bool operator<(const TPair& Pair) const { - return (Val1 -void GetSwitchedPrV(const TVec, TSizeTy>& SrcPrV, TVec, TSizeTy>& DstPrV){ - const TSizeTy Prs = SrcPrV.Len(); - DstPrV.Gen(Prs, 0); - for (TSizeTy PrN=0; PrN& SrcPr=SrcPrV[PrN]; - DstPrV.Add(TPair(SrcPr.Val2, SrcPr.Val1)); - } -} - -typedef TPair TBoolChPr; -typedef TPair TBoolFltPr; -typedef TPair TUChIntPr; -typedef TPair TUChUInt64Pr; -typedef TPair TUChStrPr; -typedef TPair TIntBoolPr; -typedef TPair TIntChPr; -typedef TPair TIntPr; -typedef TPair TIntUInt64Pr; -typedef TPair TIntIntPrPr; -typedef TPair > TIntIntVPr; -typedef TPair TIntFltPr; -typedef TPair TIntStrPr; -typedef TPair TIntStrVPr; -typedef TPair TIntPrIntPr; -typedef TPair TUIntUIntPr; -typedef TPair TUIntIntPr; -typedef TPair TUInt64IntPr; -typedef TPair TUInt64Pr; -typedef TPair TUInt64FltPr; -typedef TPair TUInt64StrPr; -typedef TPair TFltIntPr; -typedef TPair TFltUInt64Pr; -typedef TPair TFltPr; -typedef TPair TFltStrPr; -typedef TPair TAscFltIntPr; -typedef TPair TAscFltPr; -typedef TPair TFltStrPr; -typedef TPair TAscFltStrPr; -typedef TPair TStrIntPr; -typedef TPair TStrFltPr; -typedef TPair TStrPr; -typedef TPair TStrStrVPr; -typedef TPair TStrVIntPr; -typedef TPair TIntIntPrPr; -typedef TPair TIntStrPrPr; -typedef TPair TFltStrPrPr; -typedef TPair, TVec > TIntVFltVPr; - -/// Compares the pair by the second value. -template -class TCmpPairByVal2 { -private: - bool IsAsc; -public: - TCmpPairByVal2(const bool& AscSort=true) : IsAsc(AscSort) { } - bool operator () (const TPair& P1, const TPair& P2) const { - if (IsAsc) { return P1.Val2 < P2.Val2; } else { return P2.Val2 < P1.Val2; } - } -}; - -///////////////////////////////////////////////// -// Triple -template -class TTriple{ -public: - TVal1 Val1; - TVal2 Val2; - TVal3 Val3; -public: - TTriple(): Val1(), Val2(), Val3(){} - TTriple(const TTriple& Triple): - Val1(Triple.Val1), Val2(Triple.Val2), Val3(Triple.Val3){} - TTriple(const TVal1& _Val1, const TVal2& _Val2, const TVal3& _Val3): - Val1(_Val1), Val2(_Val2), Val3(_Val3){} - explicit TTriple(TSIn& SIn): Val1(SIn), Val2(SIn), Val3(SIn){} - void Save(TSOut& SOut) const { - Val1.Save(SOut); Val2.Save(SOut); Val3.Save(SOut);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TTriple& operator=(const TTriple& Triple){ - if (this!=&Triple){Val1=Triple.Val1; Val2=Triple.Val2; Val3=Triple.Val3;} - return *this;} - bool operator==(const TTriple& Triple) const { - return (Val1==Triple.Val1)&&(Val2==Triple.Val2)&&(Val3==Triple.Val3);} - bool operator<(const TTriple& Triple) const { - return (Val1 TChTr; -typedef TTriple TChIntIntTr; -typedef TTriple TUChIntIntTr; -typedef TTriple TIntTr; -typedef TTriple TUInt64Tr; -typedef TTriple TIntStrIntTr; -typedef TTriple TIntIntStrTr; -typedef TTriple TIntIntFltTr; -typedef TTriple TIntFltIntTr; -typedef TTriple TIntFltFltTr; -typedef TTriple TIntStrStrTr; -typedef TTriple, TInt> TIntIntVIntTr; -typedef TTriple > TIntIntIntVTr; -typedef TTriple TFltTr; -typedef TTriple TFltIntIntTr; -typedef TTriple TFltFltIntTr; -typedef TTriple TFltFltStrTr; -typedef TTriple TChATr; -typedef TTriple TStrTr; -typedef TTriple TStrIntIntTr; -typedef TTriple TStrFltFltTr; -typedef TTriple TStrStrIntTr; -typedef TTriple TStrIntStrVTr; - -/// Compares the triple by the second value. -template -class TCmpTripleByVal2 { -private: - bool IsAsc; -public: - TCmpTripleByVal2(const bool& AscSort=true) : IsAsc(AscSort) { } - bool operator () (const TTriple& T1, const TTriple& T2) const { - if (IsAsc) { return T1.Val2 < T2.Val2; } else { return T2.Val2 < T1.Val2; } - } -}; - -/// Compares the triple by the third value. -template -class TCmpTripleByVal3 { -private: - bool IsAsc; -public: - TCmpTripleByVal3(const bool& AscSort=true) : IsAsc(AscSort) { } - bool operator () (const TTriple& T1, const TTriple& T2) const { - if (IsAsc) { return T1.Val3 < T2.Val3; } else { return T2.Val3 < T1.Val3; } - } -}; - -///////////////////////////////////////////////// -// Quad -template -class TQuad{ -public: - TVal1 Val1; - TVal2 Val2; - TVal3 Val3; - TVal4 Val4; -public: - TQuad(): - Val1(), Val2(), Val3(), Val4(){} - TQuad(const TQuad& Quad): - Val1(Quad.Val1), Val2(Quad.Val2), Val3(Quad.Val3), Val4(Quad.Val4){} - TQuad(const TVal1& _Val1, const TVal2& _Val2, const TVal3& _Val3, const TVal4& _Val4): - Val1(_Val1), Val2(_Val2), Val3(_Val3), Val4(_Val4){} - explicit TQuad(TSIn& SIn): - Val1(SIn), Val2(SIn), Val3(SIn), Val4(SIn){} - void Save(TSOut& SOut) const { - Val1.Save(SOut); Val2.Save(SOut); Val3.Save(SOut); Val4.Save(SOut);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TQuad& operator=(const TQuad& Quad){ - if (this!=&Quad){ - Val1=Quad.Val1; Val2=Quad.Val2; Val3=Quad.Val3; Val4=Quad.Val4;} - return *this;} - bool operator==(const TQuad& Quad) const { - return (Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3==Quad.Val3)&&(Val4==Quad.Val4);} - bool operator<(const TQuad& Quad) const { - return (Val1 TStrStrIntIntQu; -typedef TQuad TStrQu; -typedef TQuad TIntQu; -typedef TQuad TFltQu; -typedef TQuad TFltIntIntIntQu; -typedef TQuad TIntStrIntIntQu; -typedef TQuad TIntIntFltFltQu; - -///////////////////////////////////////////////// -// Tuple -template -class TTuple { -private: - TVal ValV [NVals]; -public: - TTuple(){} - TTuple(const TVal& InitVal) { for (int i=0; iTup[i]){return false;} } return false; } - else { return Len() < Tup.Len(); } } - void Sort(const bool& Asc=true); - int FindMx() const; - int FindMn() const; - int GetPrimHashCd() const { int hc = 0; - for (int i = 0; i < NVals; i++) { hc = TPairHashImpl::GetHashCd(hc, ValV[i].GetPrimHashCd()); } - return hc; } - int GetSecHashCd() const { int hc = 0; - for (int i = 1; i < NVals; i++) { hc = TPairHashImpl::GetHashCd(hc, ValV[i].GetSecHashCd()); } - if (NVals > 0) { hc = TPairHashImpl::GetHashCd(hc, ValV[0].GetSecHashCd()); } - return hc; } - - TStr GetStr() const { TChA ValsStr; - for (int i=0; i -void TTuple::Sort(const bool& Asc) { - TVec V(NVals); - for (int i=0; i -int TTuple::FindMx() const { - TVal MxVal = ValV[0]; - int ValN = 0; - for (int i = 1; i < NVals; i++) { - if (MxVal -int TTuple::FindMn() const { - TVal MnVal = ValV[0]; - int ValN = 0; - for (int i = 1; i < NVals; i++) { - if (MnVal>ValV[i]) { - MnVal=ValV[i]; ValN=i; - } - } - return ValN; -} - -///////////////////////////////////////////////// -// Key-Data -template -class TKeyDat{ -public: - TKey Key; - TDat Dat; -public: - TKeyDat(): Key(), Dat(){} - TKeyDat(const TKeyDat& KeyDat): Key(KeyDat.Key), Dat(KeyDat.Dat){} - explicit TKeyDat(const TKey& _Key): Key(_Key), Dat(){} - TKeyDat(const TKey& _Key, const TDat& _Dat): Key(_Key), Dat(_Dat){} - explicit TKeyDat(TSIn& SIn): Key(SIn), Dat(SIn){} - void Save(TSOut& SOut) const {Key.Save(SOut); Dat.Save(SOut);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TKeyDat& operator=(const TKeyDat& KeyDat){ - if (this!=&KeyDat){Key=KeyDat.Key; Dat=KeyDat.Dat;} return *this;} - bool operator==(const TKeyDat& KeyDat) const {return Key==KeyDat.Key;} - bool operator<(const TKeyDat& KeyDat) const {return Key -void GetSwitchedKdV(const TVec, int>& SrcKdV, TVec, int>& DstKdV){ - const int Kds=SrcKdV.Len(); - DstKdV.Gen(Kds, 0); - for (int KdN=0; KdN& SrcKd=SrcKdV[KdN]; - DstKdV.Add(TKeyDat(SrcKd.Dat, SrcKd.Key)); - } -} - -typedef TKeyDat TIntKd; -typedef TKeyDat TIntUInt64Kd; -typedef TKeyDat TIntFltKd; -typedef TKeyDat TIntPrFltKd; -typedef TKeyDat TIntFltPrKd; -typedef TKeyDat TIntSFltKd; -typedef TKeyDat TIntStrKd; -typedef TKeyDat TUIntIntKd; -typedef TKeyDat TUIntKd; -typedef TKeyDat TUInt64IntKd; -typedef TKeyDat TUInt64FltKd; -typedef TKeyDat TUInt64StrKd; -typedef TKeyDat TFltBoolKd; -typedef TKeyDat TFltIntKd; -typedef TKeyDat TFltUInt64Kd; -typedef TKeyDat TFltIntPrKd; -typedef TKeyDat TFltUIntKd; -typedef TKeyDat TFltKd; -typedef TKeyDat TFltStrKd; -typedef TKeyDat TFltBoolKd; -typedef TKeyDat TFltIntBoolPrKd; -typedef TKeyDat TAscFltIntKd; -typedef TKeyDat TStrBoolKd; -typedef TKeyDat TStrIntKd; -typedef TKeyDat TStrFltKd; -typedef TKeyDat TStrAscFltKd; -typedef TKeyDat TStrKd; - -// Key-Data comparator - -template -class TCmpKeyDatByDat { -private: - bool IsAsc; -public: - TCmpKeyDatByDat(const bool& AscSort=true) : IsAsc(AscSort) { } - bool operator () (const TKeyDat& P1, const TKeyDat& P2) const { - if (IsAsc) { return P1.Dat < P2.Dat; } else { return P2.Dat < P1.Dat; } - } -}; - -//#////////////////////////////////////////////// -/// Vector is a sequence \c TVal objects representing an array that can change in size. ##TVec -template -class TVec{ -public: - typedef TVal* TIter; //!< Random access iterator to \c TVal. -protected: - TSizeTy MxVals; //!< Vector capacity. Capacity is the size of allocated storage. If MxVals==-1, then \c ValT is not owned by the vector, and it won't free it at destruction. - TSizeTy Vals; //!< Vector length. Length is the number of elements stored in the vector. - TVal* ValT; //!< Pointer to the memory where the elements of the vector are stored. - bool IsShM; //!< True if the vector array is in shared memory - /// Resizes the vector so that it can store at least \c _MxVals. - void Resize(const TSizeTy& _MxVals=-1); - /// Constructs the out of bounds error message. - TStr GetXOutOfBoundsErrMsg(const TSizeTy& ValN) const; -public: - TVec(): MxVals(0), Vals(0), ValT(NULL), IsShM(false) {} - TVec(const TVec& Vec); - /// Constructs a vector (an array) of length \c _Vals. - explicit TVec(const TSizeTy& _Vals){ - IsShM = false; - IAssert(0<=_Vals); MxVals=Vals=_Vals; - if (_Vals==0){ValT=NULL;} else {ValT=new TVal[_Vals];}} - /// Constructs a vector (an array) of length \c _Vals, while reserving enough memory to store \c _MxVals elements. - TVec(const TSizeTy& _MxVals, const TSizeTy& _Vals){ - IsShM = false; - IAssert((0<=_Vals)&&(_Vals<=_MxVals)); MxVals=_MxVals; Vals=_Vals; - if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}} - /// Constructs a vector of \c _Vals elements of memory array \c _ValT. ##TVec::TVec - explicit TVec(TVal *_ValT, const TSizeTy& _Vals): - MxVals(-1), Vals(_Vals), ValT(_ValT), IsShM(false){} - ~TVec() {if ((ValT!=NULL) && (MxVals!=-1)) {delete[] ValT;}} - explicit TVec(TSIn& SIn): MxVals(0), Vals(0), ValT(NULL), IsShM(false) {Load(SIn);} - /// Constructs the vector from a shared memory input ##TVec::LoadShM - void LoadShM(TShMIn& ShMIn); - /// Constructs vector from shared memory input passing in functor to initialize elements - template - void LoadShM(TShMIn& ShMIn, TLoadShMElem LoadFromShMFn) { - if ((ValT!=NULL) && (MxVals!=-1)) {delete[] ValT;} - ShMIn.Load(MxVals); - ShMIn.Load(Vals); - if (MxVals == 0) { - ValT = NULL; - } else { - ValT=new TVal[MxVals]; - for (TSizeTy ValN=0; ValN& operator=(const TVec& Vec); - /// Appends value \c Val to the vector. - TVec& operator+(const TVal& Val){Add(Val); return *this;} - /// Checks that the two vectors have the same contents. - bool operator==(const TVec& Vec) const; - /// Lexicographically compares two vectors. ##TVec::Less - bool operator<(const TVec& Vec) const; - /// Returns a reference to the element at position \c ValN in the vector. - const TVal& operator[](const TSizeTy& ValN) const { - AssertR((0<=ValN)&&(ValN& Vec); - /// Copy \c Sz values from \c Vec starting at \c Offset. - void CopyUniqueFrom(TVec& Vec, TInt Offset, TInt Sz); - /// Swaps the contents of the vector with \c Vec. - void Swap(TVec& Vec); - /// Tests whether the vector is empty. ##TVec::Empty - bool Empty() const {return Vals==0;} - /// Returns the number of elements in the vector. ##TVec::Len - TSizeTy Len() const {return Vals;} - /// Returns the size of allocated storage capacity. - TSizeTy Reserved() const {return MxVals;} - /// Returns a reference to the last element of the vector. - const TVal& Last() const {return GetVal(Len()-1);} - /// Returns a reference to the last element of the vector. - TVal& Last(){return GetVal(Len()-1);} - /// Returns the position of the last element. - TSizeTy LastValN() const {return Len()-1;} - /// Returns a reference to the one before last element of the vector. - const TVal& LastLast() const { AssertR(1& ValV); - /// Adds element \c Val to a sorted vector. ##TVec::AddSorted - TSizeTy AddSorted(const TVal& Val, const bool& Asc=true, const TSizeTy& _MxVals=-1); - /// Adds element \c Val to a sorted vector. ##TVec::AddBackSorted - TSizeTy AddBackSorted(const TVal& Val, const bool& Asc); - /// Adds element \c Val to a sorted vector only if the element \c Val is not already in the vector. ##TVec::AddMerged - TSizeTy AddMerged(const TVal& Val); - /// Adds elements of \c ValV to a sorted vector only if a particular element is not already in the vector. ##TVec::AddMerged1 - TSizeTy AddVMerged(const TVec& ValV); - /// Adds element \c Val to a vector only if the element \c Val is not already in the vector. ##TVec::AddUnique - TSizeTy AddUnique(const TVal& Val); - /// Returns a reference to the element at position \c ValN in the vector. - const TVal& GetVal(const TSizeTy& ValN) const {return operator[](ValN);} - /// Returns a reference to the element at position \c ValN in the vector. - TVal& GetVal(const TSizeTy& ValN){return operator[](ValN);} - /// Sets the value of element at position \c ValN to \c Val. - void SetVal(const TSizeTy& ValN, const TVal& Val){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR((0<=ValN)&&(ValNBValN...EValN. - void GetSubValV(const TSizeTy& BValN, const TSizeTy& EValN, TVec& ValV) const; - /// Inserts new element \c Val before the element at position \c ValN. - void Ins(const TSizeTy& ValN, const TVal& Val); - /// Removes the element at position \c ValN. - void Del(const TSizeTy& ValN); - /// Removes the elements at positions MnValN...MxValN. - void Del(const TSizeTy& MnValN, const TSizeTy& MxValN); - /// Removes the last element of the vector. - void DelLast(){Del(Len()-1);} - /// Removes the first occurrence of element \c Val. - bool DelIfIn(const TVal& Val); - /// Removes all occurrences of element \c Val. - void DelAll(const TVal& Val); - /// Sets all elements of the vector to value \c Val. - void PutAll(const TVal& Val); - - /// Swaps elements at positions \c ValN1 and \c ValN2. - void Swap(const TSizeTy& ValN1, const TSizeTy& ValN2){EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - const TVal Val=ValT[ValN1]; ValT[ValN1]=ValT[ValN2]; ValT[ValN2]=Val;} - /// Swaps the elements that iterators \c LVal and \c RVal point to. - static void SwapI(TIter LVal, TIter RVal){const TVal Val=*LVal; *LVal=*RVal; *RVal=Val;} - - /// Generates next permutation of the elements in the vector. ##TVec::NextPerm - bool NextPerm(); - /// Generates previous permutation of the elements in the vector. ##TVec::PrevPerm - bool PrevPerm(); - - // Sorting functions - /// Picks three random elements at positions LValN...RValN and returns the middle one. - TSizeTy GetPivotValN(const TSizeTy& LValN, const TSizeTy& RValN) const; - /// Bubble sorts the values between positions MnLValN...MxLValN. ##TVec::BSort - void BSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc); - /// Insertion sorts the values between positions MnLValN...MxLValN. ##TVec::ISort - void ISort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc); - /// Partitions the values between positions MnLValN...MxLValN. ##TVec::Partition - TSizeTy Partition(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc); - /// Quick sorts the values between positions MnLValN...MxLValN. ##TVec::QSort - void QSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc); - /// Sorts the elements of the vector. ##TVec::Sort - void Sort(const bool& Asc=true); - /// Checks whether the vector is sorted in ascending (if \c Asc=true) or descending (if \c Asc=false) order. - bool IsSorted(const bool& Asc=true) const; - /// Randomly shuffles the elements of the vector. - void Shuffle(TRnd& Rnd); - /// Reverses the order of the elements in the vector. - void Reverse(); - /// Reverses the order of elements between LValN...RValN. - void Reverse(TSizeTy LValN, TSizeTy RValN){ Assert(LValN>=0 && RValNBI...EI and returns the middle one under the comparator \c Cmp. - template - static TIter GetPivotValNCmp(const TIter& BI, const TIter& EI, const TCmp& Cmp) { - TSizeTy SubVals=TSizeTy(EI-BI); if (SubVals > TInt::Mx-1) { SubVals = TInt::Mx-1; } - const TSizeTy ValN1=TInt::GetRnd(SubVals), ValN2=TInt::GetRnd(SubVals), ValN3=TInt::GetRnd(SubVals); - const TVal& Val1 = *(BI+ValN1); const TVal& Val2 = *(BI+ValN2); const TVal& Val3 = *(BI+ValN3); - if (Cmp(Val1, Val2)) { - if (Cmp(Val2, Val3)) return BI+ValN2; - else if (Cmp(Val3, Val1)) return BI+ValN1; - else return BI+ValN3; - } else { - if (Cmp(Val1, Val3)) return BI+ValN1; - else if (Cmp(Val3, Val2)) return BI+ValN2; - else return BI+ValN3; } } - /// Partitions the values between positions BI...EI under the comparator \c Cmp. - template - static TIter PartitionCmp(TIter BI, TIter EI, const TVal Pivot, const TCmp& Cmp) { - forever { - while (Cmp(*BI, Pivot)){++BI;} --EI; - while (Cmp(Pivot, *EI)){--EI;} - if (!(BIBI...EI under the comparator \c Cmp. - template - static void BSortCmp(TIter BI, TIter EI, const TCmp& Cmp) { - for (TIter i = BI; i != EI; ++i) { - for (TIter j = EI-1; j != i; --j) { - if (Cmp(*j, *(j-1))) { SwapI(j, j-1); } } } } - /// Insertion sorts the values between positions BI...EI under the comparator \c Cmp. - template - static void ISortCmp(TIter BI, TIter EI, const TCmp& Cmp) { - if (BI + 1 < EI) { - for (TIter i = BI, j; i != EI; ++i) { TVal Tmp=*i; j=i; - while (j > BI && Cmp(Tmp, *(j-1))) { *j = *(j-1); --j; } *j=Tmp; } } } - /// Quick sorts the values between positions BI...EI under the comparator \c Cmp. - template - static void QSortCmp(TIter BI, TIter EI, const TCmp& Cmp) { - if (BI + 1 < EI) { - if (EI - BI < 20) { ISortCmp(BI, EI, Cmp); } - else { const TVal Val = *GetPivotValNCmp(BI, EI, Cmp); - TIter Split = PartitionCmp(BI, EI, Val, Cmp); - QSortCmp(BI, Split, Cmp); QSortCmp(Split, EI, Cmp); } } } - /// Sorts the elements of the vector using the comparator \c Cmp. - template - void SortCmp(const TCmp& Cmp){ QSortCmp(BegI(), EndI(), Cmp);} - /// Checks whether the vector is sorted according to the comparator \c Cmp. - template - bool IsSortedCmp(const TCmp& Cmp) const { - if (EndI() == BegI()) return true; - for (TIter i = BegI(); i != EndI()-1; ++i) { - if (Cmp(*(i+1), *i)){return false;} } return true; } - /// Sets this vector to its intersection with \c ValV. Assumes the vectors are sorted! - void Intrs(const TVec& ValV); - /// Sets this vector to its union with \c ValV. Assumes the vectors are sorted! - void Union(const TVec& ValV); - /// Subtracts \c ValV from this vector. Assumes the vectors are sorted! ##TVec::Diff - void Diff(const TVec& ValV); - /// Sets \c DstValV to the intersection of this vector and \c ValV. Assumes the vectors are sorted! - void Intrs(const TVec& ValV, TVec& DstValV) const; - /// Sets \c DstValV to the union of this vector and \c ValV. Assumes the vectors are sorted! - void Union(const TVec& ValV, TVec& DstValV) const; - /// Sets \c DstValV to the difference between this vector and \c ValV. Assumes the vectors are sorted! ##TVec::Diff1 - void Diff(const TVec& ValV, TVec& DstValV) const; - /// Returns the size of the intersection of vectors \c this and \c ValV. Assumes the vectors are sorted! - TSizeTy IntrsLen(const TVec& ValV) const; - /// Returns the size of the union of vectors \c this and \c ValV. Assumes the vectors are sorted! - TSizeTy UnionLen(const TVec& ValV) const; - - /// Counts the number of occurrences of \c Val in the vector. - TSizeTy Count(const TVal& Val) const; - /// Returns the position of an element with value \c Val. ##TVec::SearchBin - TSizeTy SearchBin(const TVal& Val) const; - /// Returns the position of an element with value \c Val. ##TVec::SearchBin1 - TSizeTy SearchBin(const TVal& Val, TSizeTy& InsValN) const; - /// Returns the position of an element with value \c Val. ##TVec::SearchBinLeft - TSizeTy SearchBinLeft(const TVal& Val, TSizeTy& InsValN) const; - /// Returns the position of an element with value \c Val. ##TVec::SearchForw - TSizeTy SearchForw(const TVal& Val, const TSizeTy& BValN=0) const; - /// Returns the position of an element with value \c Val. ##TVec::SearchBack - TSizeTy SearchBack(const TVal& Val) const; - /// Returns the starting position of vector \c ValV. ##TVec::SearchVForw - TSizeTy SearchVForw(const TVec& ValV, const TSizeTy& BValN=0) const; - - /// Checks whether element \c Val is a member of the vector. - bool IsIn(const TVal& Val) const {return SearchForw(Val)!=-1;} - /// Checks whether element \c Val is a member of the vector. ##TVec::IsIn - bool IsIn(const TVal& Val, TSizeTy& ValN) const { ValN=SearchForw(Val); return ValN!=-1;} - /// Checks whether element \c Val is a member of the vector. ##TVec::IsInBin - bool IsInBin(const TVal& Val) const {return SearchBin(Val)!=-1;} - /// Returns reference to the first occurrence of element \c Val. - const TVal& GetDat(const TVal& Val) const { return GetVal(SearchForw(Val));} - /// Returns reference to the first occurrence of element \c Val. ##TVec::GetAddDat - TVal& GetAddDat(const TVal& Val){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - const TSizeTy ValN=SearchForw(Val); if (ValN==-1){Add(Val); return Last();} else {return GetVal(ValN);}} - /// Returns the position of the largest element in the vector. - TSizeTy GetMxValN() const; - - /// Returns a vector on element \c Val1. - static TVec GetV(const TVal& Val1){ - TVec V(1, 0); V.Add(Val1); return V;} - /// Returns a vector on elements \c Val1, \c Val2. - static TVec GetV(const TVal& Val1, const TVal& Val2){ - TVec V(2, 0); V.Add(Val1); V.Add(Val2); return V;} - /// Returns a vector on elements Val1...Val3. - static TVec GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3){ - TVec V(3, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); return V;} - /// Returns a vector on elements Val1...Val4. - static TVec GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4){ - TVec V(4, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); return V;} - /// Returns a vector on elements Val1...Val5. - static TVec GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5){ - TVec V(5, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); return V;} - /// Returns a vector on elements Val1...Val6. - static TVec GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6){ - TVec V(6, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); return V;} - /// Returns a vector on elements Val1...Val7. - static TVec GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7){ - TVec V(7, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); return V;} - /// Returns a vector on elements Val1...Val8. - static TVec GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7, const TVal& Val8){ - TVec V(8, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); V.Add(Val8); return V;} - /// Returns a vector on elements Val1...Val9. - static TVec GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7, const TVal& Val8, const TVal& Val9){ - TVec V(9, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); V.Add(Val8); V.Add(Val9); return V;} -}; - -template -void TVec::Resize(const TSizeTy& _MxVals){ - IAssertR(MxVals!=-1 || IsShM, TStr::Fmt("Can not increase the capacity of the vector. %s. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", GetTypeNm(*this).CStr()).CStr()); - IAssertR(MxVals!=(TInt::Mx-1024), TStr::Fmt("Buffer size at maximum. %s. [Program refuses to allocate more memory. Solution-1: Send your test case to developers.]", GetTypeNm(*this).CStr()).CStr()); - TSizeTy OldMxVals = MxVals; - if (MxVals == -1) {MxVals = Vals;} - if (_MxVals==-1){ - if (Vals==0){MxVals=16;} else {MxVals*=2;} - } else { - if (_MxVals<=MxVals){return;} else {MxVals=_MxVals;} - } - if (MxVals < 0) { - MxVals = TInt::Mx-1024; - } - if (ValT==NULL){ - try { - ValT=new TVal[MxVals]; - } - catch (std::exception Ex){ - FailR(TStr::Fmt("TVec::Resize: %s, Length:%s, Capacity:%s, New capacity:%s, Type:%s [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", - Ex.what(), TInt::GetStr(Vals).CStr(), TInt::GetStr(MxVals).CStr(), TInt::GetStr(_MxVals).CStr(), GetTypeNm(*this).CStr()).CStr());} - } else { - TVal* NewValT = NULL; - try { - NewValT=new TVal[MxVals]; - } - catch (std::exception Ex){ - FailR(TStr::Fmt("TVec::Resize: %s, Length:%s, Capacity:%s, New capacity:%s, Type:%s [Program failed to allocate more memory. Solution-1: Get a bigger machine and a 64-bit compiler.]", - Ex.what(), TInt::GetStr(Vals).CStr(), TInt::GetStr(MxVals).CStr(), TInt::GetStr(_MxVals).CStr(), GetTypeNm(*this).CStr()).CStr());} - IAssert(NewValT!=NULL); - for (TSizeTy ValN=0; ValN -TStr TVec::GetXOutOfBoundsErrMsg(const TSizeTy& ValN) const { - return TStr()+ - "Index:"+TInt::GetStr(ValN)+ - " Vals:"+TInt::GetStr(Vals)+ - " MxVals:"+TInt::GetStr(MxVals)+ - " Type:"+GetTypeNm(*this); -} - -template -TVec::TVec(const TVec& Vec){ - MxVals=Vec.MxVals; - Vals=Vec.Vals; - if (MxVals==0) {ValT=NULL;} else {ValT=new TVal[MxVals];} - for (TSizeTy ValN=0; ValN -void TVec::LoadShM(TShMIn& ShMIn) { - if ((ValT!=NULL) && (MxVals!=-1)) {delete[] ValT;} - ShMIn.Load(MxVals); - MxVals = -1; - ShMIn.Load(Vals); - if (MxVals == 0) { - ValT = NULL; - } else { - ValT = (TVal*)(ShMIn.AdvanceCursor(Vals*sizeof(TVal))); - IsShM = true; - } -} - -template -void TVec::Load(TSIn& SIn){ - if ( (ValT!=NULL) && (MxVals!=-1)) {delete[] ValT;} - SIn.Load(MxVals); SIn.Load(Vals); MxVals=Vals; - if ( MxVals==0 ){ValT=NULL;} else {ValT=new TVal[MxVals];} - for (TSizeTy ValN=0; ValN -void TVec::Save(TSOut& SOut) const { - if (MxVals!=-1){SOut.Save(MxVals);} else {SOut.Save(Vals);} - SOut.Save(Vals); - for (TSizeTy ValN=0; ValN -TVec& TVec::operator=(const TVec& Vec){ - if (this!=&Vec){ - if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;} - MxVals=Vals=Vec.Vals; - if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];} - for (TSizeTy ValN=0; ValN -bool TVec::operator==(const TVec& Vec) const { - if (this==&Vec){return true;} - if (Len()!=Vec.Len()){return false;} - for (TSizeTy ValN=0; ValN -bool TVec::operator<(const TVec& Vec) const { - if (this==&Vec){return false;} - if (Len()==Vec.Len()){ - for (TSizeTy ValN=0; ValNVec.ValT[ValN]){return false;} - else {} - } - return false; - } else { - return Len() -int TVec::GetPrimHashCd() const { - int hc = 0; - for (TSizeTy i=0; i -int TVec::GetSecHashCd() const { - int hc = 0; - for (TSizeTy i=0; i 0) { - hc = TPairHashImpl::GetHashCd(hc, ValT[0].GetSecHashCd()); } - return hc; -} - -template -void TVec::Clr(const bool& DoDel, const TSizeTy& NoDelLim){ - if ((DoDel)||((!DoDel)&&(NoDelLim!=-1)&&(MxVals>NoDelLim))){ - if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;} - MxVals=Vals=0; ValT=NULL; - } else { - IAssertR(MxVals!=-1 || IsShM, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - Vals=0; - } -} - -template -void TVec::Trunc(const TSizeTy& _Vals){ - EAssertR(!(MxVals==-1 && IsShM), "Cannot truncate a shared memory vector"); - IAssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - IAssert((_Vals==-1)||(_Vals>=0)); - if ((_Vals!=-1)&&(_Vals>=Vals)){ - return; - } else - if (((_Vals==-1)&&(Vals==0))||(_Vals==0)){ - if (ValT!=NULL){delete[] ValT;} - MxVals=Vals=0; ValT=NULL; - } else { - if (_Vals==-1){ - if (MxVals==Vals){return;} else {MxVals=Vals;} - } else { - MxVals=Vals=_Vals; - } - TVal* NewValT=new TVal[MxVals]; - IAssert(NewValT!=NULL); - for (TSizeTy ValN=0; ValN -void TVec::Pack(){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot pack accessed shared memory"); - IAssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - if (Vals==0){ - if (ValT!=NULL){delete[] ValT;} ValT=NULL; - } else - if (Vals -void TVec::MoveFrom(TVec& Vec){ - if (this!=&Vec){ - if (ValT!=NULL && MxVals!=-1){delete[] ValT;} - MxVals=Vec.MxVals; Vals=Vec.Vals; ValT=Vec.ValT; - Vec.MxVals=0; Vec.Vals=0; Vec.ValT=NULL; - } -} - -template -void TVec::CopyUniqueFrom(TVec& Vec, TInt Offset, TInt Sz){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - if (this!=&Vec){ - if (ValT!=NULL && MxVals!=-1 && MxVals < Sz){ - delete[] ValT; - ValT=new TVal[Sz]; - } - if (Sz == 0) { Vals = 0; return; } - ValT[0] = Vec.ValT[Offset]; - Vals = 1; - for (TSizeTy ValN=1; ValN -void TVec::Swap(TVec& Vec){ - if (this!=&Vec){ - ::Swap(MxVals, Vec.MxVals); - ::Swap(Vals, Vec.Vals); - ::Swap(ValT, Vec.ValT); - } -} - -template -TSizeTy TVec::AddV(const TVec& ValV){ - AssertR(MxVals!=-1 || IsShM, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - for (TSizeTy ValN=0; ValN -TSizeTy TVec::AddSorted(const TVal& Val, const bool& Asc, const TSizeTy& _MxVals){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - TSizeTy ValN=Add(Val); - if (Asc){ - while ((ValN>0)&&(ValT[ValN]0)&&(ValT[ValN]>ValT[ValN-1])){ - Swap(ValN, ValN-1); ValN--;} - } - if ((_MxVals!=-1)&&(Len()>_MxVals)){Del(_MxVals, Len()-1);} - return ValN; -} - -template -TSizeTy TVec::AddBackSorted(const TVal& Val, const bool& Asc){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - Add(); - TSizeTy ValN=Vals-2; - while ((ValN>=0)&&((Asc&&(ValValT[ValN])))){ - ValT[ValN+1]=ValT[ValN]; ValN--;} - ValT[ValN+1]=Val; - return ValN+1; -} - -template -TSizeTy TVec::AddMerged(const TVal& Val){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - TSizeTy ValN=SearchBin(Val); - if (ValN==-1){return AddSorted(Val);} - else {GetVal(ValN)=Val; return -1;} -} - -template -TSizeTy TVec::AddVMerged(const TVec& ValV){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - for (TSizeTy ValN=0; ValN -TSizeTy TVec::AddUnique(const TVal& Val){ - AssertR(MxVals!=-1 || IsShM, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - TSizeTy ValN=SearchForw(Val); - if (ValN==-1){return Add(Val);} - else {GetVal(ValN)=Val; return -1;} -} - -template -void TVec::GetSubValV(const TSizeTy& _BValN, const TSizeTy& _EValN, TVec& SubValV) const { - const TSizeTy BValN=TInt::GetInRng(_BValN, 0, Len()-1); - const TSizeTy EValN=TInt::GetInRng(_EValN, 0, Len()-1); - const TSizeTy SubVals=TInt::GetMx(0, EValN-BValN+1); - SubValV.Gen(SubVals, 0); - for (TSizeTy ValN=BValN; ValN<=EValN; ValN++){ - SubValV.Add(GetVal(ValN));} -} - -template -void TVec::Ins(const TSizeTy& ValN, const TVal& Val){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - Add(); Assert((0<=ValN)&&(ValN=ValN; MValN--){ValT[MValN+1]=ValT[MValN];} - ValT[ValN]=Val; -} - -template -void TVec::Del(const TSizeTy& ValN){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - Assert((0<=ValN)&&(ValN -void TVec::Del(const TSizeTy& MnValN, const TSizeTy& MxValN){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - Assert((0<=MnValN)&&(MnValN -bool TVec::DelIfIn(const TVal& Val){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - TSizeTy ValN=SearchForw(Val); - if (ValN!=-1){Del(ValN); return true;} - else {return false;} -} - -template -void TVec::DelAll(const TVal& Val){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - TSizeTy ValN; - while ((ValN=SearchForw(Val))!=-1){Del(ValN);} -} - -template -void TVec::PutAll(const TVal& Val){ - EAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - for (TSizeTy ValN=0; ValN -void TVec::BSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){ - for (TSizeTy ValN1=MnLValN; ValN1<=MxRValN; ValN1++){ - for (TSizeTy ValN2=MxRValN; ValN2>ValN1; ValN2--){ - if (Asc){ - if (ValT[ValN2]ValT[ValN2-1]){Swap(ValN2, ValN2-1);} - } - } - } -} - -template -void TVec::ISort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){ - if (MnLValNMnLValN)&&(ValT[ValN2-1]>Val)){ - ValT[ValN2]=ValT[ValN2-1]; ValN2--;} - } else { - while ((ValN2>MnLValN)&&(ValT[ValN2-1] -TSizeTy TVec::GetPivotValN(const TSizeTy& LValN, const TSizeTy& RValN) const { - TSizeTy SubVals=RValN-LValN+1; - if (SubVals > TInt::Mx-1) { SubVals = TInt::Mx-1; } - const TSizeTy ValN1=LValN+TInt::GetRnd(int(SubVals)); - const TSizeTy ValN2=LValN+TInt::GetRnd(int(SubVals)); - const TSizeTy ValN3=LValN+TInt::GetRnd(int(SubVals)); - const TVal& Val1=ValT[ValN1]; - const TVal& Val2=ValT[ValN2]; - const TVal& Val3=ValT[ValN3]; - if (Val1 -TSizeTy TVec::Partition(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){ - TSizeTy PivotValN=GetPivotValN(MnLValN, MxRValN); - Swap(PivotValN, MnLValN); - TVal PivotVal=ValT[MnLValN]; - TSizeTy LValN=MnLValN-1; TSizeTy RValN=MxRValN+1; - forever { - if (Asc){ - do {RValN--;} while (ValT[RValN]>PivotVal); - do {LValN++;} while (ValT[LValN]PivotVal); - } - if (LValN -void TVec::QSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){ - if (MnLValN -void TVec::Sort(const bool& Asc){ - QSort(0, Len()-1, Asc); -} - -template -bool TVec::IsSorted(const bool& Asc) const { - if (Asc){ - for (TSizeTy ValN=0; ValNValT[ValN+1]){return false;}} - } else { - for (TSizeTy ValN=0; ValN -void TVec::Shuffle(TRnd& Rnd){ - if (Len() < TInt::Mx) { - for (TSizeTy ValN=0; ValN -void TVec::Reverse(){ - for (TSizeTy ValN=0; ValN -void TVec::Merge(){ - IAssertR(!(IsShM && (MxVals == -1)), "Cannot write to shared memory"); - AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); - TVec SortedVec(*this); SortedVec.Sort(); - Clr(); - for (TSizeTy ValN=0; ValN -bool TVec::NextPerm() { - // start with a sorted sequence to obtain all permutations - TSizeTy First = 0, Last = Len(), Next = Len()-1; - if (Last < 2) return false; - for(; ; ) { - // find rightmost element smaller than successor - TSizeTy Next1 = Next; - if (GetVal(--Next) < GetVal(Next1)) { // swap with rightmost element that's smaller, flip suffix - TSizeTy Mid = Last; - for (; GetVal(Next) >= GetVal(--Mid); ) { } - Swap(Next, Mid); - Reverse(Next1, Last-1); - return true; - } - if (Next == First) { // pure descending, flip all - Reverse(); - return false; - } - } -} - -template -bool TVec::PrevPerm() { - TSizeTy First = 0, Last = Len(), Next = Len()-1; - if (Last < 2) return false; - for(; ; ) { - // find rightmost element not smaller than successor - TSizeTy Next1 = Next; - if (GetVal(--Next) >= GetVal(Next1)) { // swap with rightmost element that's not smaller, flip suffix - TSizeTy Mid = Last; - for (; GetVal(Next) < GetVal(--Mid); ) { } - Swap(Next, Mid); - Reverse(Next1, Last); - return true; - } - if (Next == First) { // pure descending, flip all - Reverse(); - return false; - } - } -} - -template -void TVec::Intrs(const TVec& ValV){ - TVec IntrsVec; - Intrs(ValV, IntrsVec); - MoveFrom(IntrsVec); -} - -template -void TVec::Union(const TVec& ValV){ - TVec UnionVec; - Union(ValV, UnionVec); - MoveFrom(UnionVec); -} - -template -void TVec::Diff(const TVec& ValV){ - TVec DiffVec; - Diff(ValV, DiffVec); - MoveFrom(DiffVec); -} - -template -void TVec::Intrs(const TVec& ValV, TVec& DstValV) const { - DstValV.Clr(); - TSizeTy ValN1=0, ValN2=0; - while ((ValN1ValV.GetVal(ValN2))){ - ValN2++;} - if ((ValN2 -void TVec::Union(const TVec& ValV, TVec& DstValV) const { - DstValV.Gen(TInt::GetMx(Len(), ValV.Len()), 0); - TSizeTy ValN1=0, ValN2=0; - while ((ValN1Val2){DstValV.Add(Val2); ValN2++;} - else {DstValV.Add(Val1); ValN1++; ValN2++;} - } - for (TSizeTy RestValN1=ValN1; RestValN1 -void TVec::Diff(const TVec& ValV, TVec& DstValV) const { - DstValV.Clr(); - TSizeTy ValN1=0, ValN2=0; - while (ValN1ValV.GetVal(ValN2)) ValN2++; - if (ValN2 -TSizeTy TVec::IntrsLen(const TVec& ValV) const { - TSizeTy Cnt=0, ValN1=0, ValN2=0; - while ((ValN1ValV.GetVal(ValN2))){ - ValN2++;} - if ((ValN2 -TSizeTy TVec::UnionLen(const TVec& ValV) const { - TSizeTy Cnt = 0, ValN1 = 0, ValN2 = 0; - while ((ValN1 < Len()) && (ValN2 < ValV.Len())) { - const TVal& Val1 = GetVal(ValN1); - const TVal& Val2 = ValV.GetVal(ValN2); - if (Val1 < Val2) { - Cnt++; ValN1++; - } else if (Val1 > Val2) { - Cnt++; ValN2++; - } else { - Cnt++; ValN1++; ValN2++; - } - } - Cnt += (Len() - ValN1) + (ValV.Len() - ValN2); - return Cnt; -} - -template -TSizeTy TVec::Count(const TVal& Val) const { - TSizeTy Count = 0; - for (TSizeTy i = 0; i < Len(); i++){ - if (Val == ValT[i]){Count++;}} - return Count; -} - -template -TSizeTy TVec::SearchBin(const TVal& Val) const { - TSizeTy LValN=0, RValN=Len()-1; - while (RValN>=LValN){ - TSizeTy ValN=(LValN+RValN)/2; - if (Val==ValT[ValN]){return ValN;} - if (Val -TSizeTy TVec::SearchBin(const TVal& Val, TSizeTy& InsValN) const { - TSizeTy LValN=0, RValN=Len()-1; - while (RValN>=LValN){ - TSizeTy ValN=(LValN+RValN)/2; - if (Val==ValT[ValN]){InsValN=ValN; return ValN;} - if (Val -TSizeTy TVec::SearchBinLeft(const TVal& Val, TSizeTy& InsValN) const { - TSizeTy LValN=0, RValN=Len()-1; - while (RValN>=LValN){ - TSizeTy ValN=(LValN+RValN)/2; - if (Val==ValT[ValN]){InsValN=ValN; return ValN;} - if (Val -TSizeTy TVec::SearchForw(const TVal& Val, const TSizeTy& BValN) const { - for (TSizeTy ValN=BValN; ValN -TSizeTy TVec::SearchBack(const TVal& Val) const { - for (TSizeTy ValN=Vals-1; ValN>=0; ValN--){ - if (Val==ValT[ValN]){return ValN;}} - return -1; -} - -template -TSizeTy TVec::SearchVForw(const TVec& ValV, const TSizeTy& BValN) const { - TSizeTy ValVLen=ValV.Len(); - for (TSizeTy ValN=BValN; ValN -TSizeTy TVec::GetMxValN() const { - if (Vals==0){return -1;} - TSizeTy MxValN=0; - for (TSizeTy ValN=1; ValNValT[MxValN]){MxValN=ValN;} - } - return MxValN; -} - -///////////////////////////////////////////////// -// Common-Vector-Types -typedef TVec TBoolV; -typedef TVec TChV; -typedef TVec TUChV; -typedef TVec TUIntV; -typedef TVec TIntV; -typedef TVec TUInt64V; -typedef TVec TFltV; -typedef TVec TSFltV; -typedef TVec TAscFltV; -typedef TVec TStrV; -typedef TVec TChAV; -typedef TVec TIntPrV; -typedef TVec TIntTrV; -typedef TVec TIntQuV; -typedef TVec TFltPrV; -typedef TVec TFltTrV; -typedef TVec TIntKdV; -typedef TVec TUChIntPrV; -typedef TVec TUChUInt64PrV; -typedef TVec TIntUInt64PrV; -typedef TVec TIntUInt64KdV; -typedef TVec TIntFltPrV; -typedef TVec TIntFltPrKdV; -typedef TVec TFltIntPrV; -typedef TVec TFltUInt64PrV; -typedef TVec TFltStrPrV; -typedef TVec TAscFltStrPrV; -typedef TVec TIntStrPrV; -typedef TVec TIntIntStrTrV; -typedef TVec TIntIntFltTrV; -typedef TVec TIntFltIntTrV; -typedef TVec TIntStrIntTrV; -typedef TVec TIntStrStrTrV; -typedef TVec TIntKdV; -typedef TVec TUIntIntKdV; -typedef TVec TIntFltKdV; -typedef TVec TIntPrFltKdV; -typedef TVec TIntStrKdV; -typedef TVec TIntStrPrPrV; -typedef TVec TIntStrVPrV; -typedef TVec TIntIntVIntTrV; -typedef TVec TIntIntIntVTrV; -typedef TVec TUInt64IntPrV; -typedef TVec TUInt64FltPrV; -typedef TVec TUInt64StrPrV; -typedef TVec TUInt64IntKdV; -typedef TVec TUInt64FltKdV; -typedef TVec TUInt64StrKdV; -typedef TVec TFltBoolKdV; -typedef TVec TFltIntKdV; -typedef TVec TFltUInt64KdV; -typedef TVec TFltIntPrKdV; -typedef TVec TFltKdV; -typedef TVec TFltStrKdV; -typedef TVec TFltStrPrPrV; -typedef TVec TFltIntIntTrV; -typedef TVec TFltFltStrTrV; -typedef TVec TAscFltIntPrV; -typedef TVec TAscFltIntKdV; -typedef TVec TStrPrV; -typedef TVec TStrIntPrV; -typedef TVec TStrFltPrV; -typedef TVec TStrIntKdV; -typedef TVec TStrFltKdV; -typedef TVec TStrAscFltKdV; -typedef TVec TStrTrV; -typedef TVec TStrQuV; -typedef TVec TStrFltFltTrV; -typedef TVec TStrStrIntTrV; -typedef TVec TStrKdV; -typedef TVec TStrStrVPrV; -typedef TVec TStrVIntPrV; -typedef TVec TFltIntIntIntQuV; -typedef TVec TIntStrIntIntQuV; -typedef TVec TIntIntPrPrV; - -//#////////////////////////////////////////////// -/// Vector Pool. ##TVecPool -template -class TVecPool { -public: - typedef TPt > PVecPool; - typedef TVec TValV; -private: - TCRef CRef; - TBool FastCopy; - TSize GrowBy, MxVals, Vals; - TVal EmptyVal; // Empty value/vector - TVal *ValBf; // Buffer for storing all the values - TVec IdToOffV; // Id to one past last (Vector starts at [id-1]). Vector length is IdToOff[id]-IdToOff[id-1] -private: - void Resize(const TSize& _MxVals); -public: - /// Vector pool constructor. ##TVecPool::TVecPool - TVecPool(const TSize& ExpectVals=0, const TSize& _GrowBy=1000000, const bool& _FastCopy=false, const TVal& _EmptyVal=TVal()); - TVecPool(const TVecPool& Pool); - TVecPool(TSIn& SIn); - ~TVecPool() { if (ValBf != NULL) { delete [] ValBf; } ValBf=NULL; } - static PVecPool New(const TSize& ExpectVals=0, const TSize& GrowBy=1000000, const bool& FastCopy=false) { - return new TVecPool(ExpectVals, GrowBy, FastCopy); } - static PVecPool Load(TSIn& SIn) { return new TVecPool(SIn); } - static PVecPool Load(const TStr& FNm) { TFIn FIn(FNm); return Load(FIn); } - void Save(TSOut& SOut) const; - TVecPool& operator = (const TVecPool& Pool); - - /// Returns the total number of vectors stored in the vector pool. - int GetVecs() const { return IdToOffV.Len(); } - /// Returns the total number of values stored in the vector pool. - TSize GetVals() const { return Vals; } - /// Tests whether vector of id \c VId is in the pool. - bool IsVId(const int& VId) const { return (0 <= VId) && (VId < IdToOffV.Len()); } - /// Returns the total capacity of the pool. - uint64 Reserved() const { return MxVals; } - /// Reserves enough capacity for the pool to store \c MxVals elements. - void Reserve(const TSize& MxVals) { Resize(MxVals); } - /// Returns the reference to an empty value. - const TVal& GetEmptyVal() const { return EmptyVal; } - /// Sets the empty value. - void SetEmptyVal(const TVal& _EmptyVal) { EmptyVal = _EmptyVal; } - /// Returns the total memory footprint (in bytes) of the pool. - uint64 GetMemUsed() const { - return sizeof(TCRef)+sizeof(TBool)+3*sizeof(TSize)+sizeof(TVal*)+MxVals*sizeof(TVal);} - - /// Adds vector \c ValV to the pool and returns its id. - int AddV(const TValV& ValV); - /// Adds a vector of length \c ValVLen to the pool and returns its id. ##TVecPool::AddEmptyV - int AddEmptyV(const int& ValVLen); - /// Returns the number of elements in the vector with id \c VId. - int GetVLen(const int& VId) const { if (VId==0){return 0;} else {return int(IdToOffV[VId]-IdToOffV[VId-1]);}} - /// Returns pointer to the first element of the vector with id \c VId. - TVal* GetValVPt(const int& VId) const { - if (GetVLen(VId)==0){return (TVal*)&EmptyVal;} - else {return ValBf+IdToOffV[VId-1];}} - /// Returns \c ValV which is a reference (not a copy) to vector with id \c VId. ##TVecPool::GetV - void GetV(const int& VId, TValV& ValV) const { - if (GetVLen(VId)==0){ValV.Clr();} - else { ValV.GenExt(GetValVPt(VId), GetVLen(VId)); } } - /// Sets the values of vector \c VId with those in \c ValV. - void PutV(const int& VId, const TValV& ValV) { - IAssert(IsVId(VId) && GetVLen(VId) == ValV.Len()); - if (FastCopy) { - memcpy(GetValVPt(VId), ValV.BegI(), sizeof(TVal)*ValV.Len()); } - else { TVal* ValPt = GetValVPt(VId); - for (::TSize ValN=0; ValN < ::TSize(ValV.Len()); ValN++, ValPt++) { *ValPt=ValV[ValN]; } - } } - /// Deletes all elements of value \c DelVal from all vectors. ##TVecPool::CompactPool - void CompactPool(const TVal& DelVal); - /// Shuffles the order of all elements in the pool. ##TVecPool::ShuffleAll - void ShuffleAll(TRnd& Rnd=TInt::Rnd); - - /// Clears the contents of the pool. ##TVecPool::Clr - void Clr(bool DoDel = true) { - IdToOffV.Clr(DoDel); MxVals=0; Vals=0; - if (DoDel && ValBf!=NULL) { delete [] ValBf; ValBf=NULL;} - if (! DoDel) { PutAll(EmptyVal); } } - /// Sets the values of all elements in the pool to \c Val. - void PutAll(const TVal& Val) { - for (TSize ValN = 0; ValN < MxVals; ValN++) { ValBf[ValN]=Val; } } - friend class TPt >; -}; - -template -void TVecPool::Resize(const TSize& _MxVals){ - if (_MxVals <= MxVals){ return; } else { MxVals = _MxVals; } - if (ValBf == NULL) { - try { ValBf = new TVal [MxVals]; } - catch (std::exception Ex) { - FailR(TStr::Fmt("TVecPool::Resize 1: %s, MxVals: %s. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), TInt::GetStr(uint64(_MxVals)).CStr()).CStr()); } - IAssert(ValBf != NULL); - if (EmptyVal != TVal()) { PutAll(EmptyVal); } - } else { - // printf("*** Resize vector pool: %llu -> %llu\n", uint64(Vals), uint64(MxVals)); - TVal* NewValBf = NULL; - try { NewValBf = new TVal [MxVals]; } - catch (std::exception Ex) { - FailR(TStr::Fmt("TVecPool::Resize 1: %s, MxVals: %s. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), TInt::GetStr(uint64(_MxVals)).CStr()).CStr()); } - IAssert(NewValBf != NULL); - if (FastCopy) { - memcpy(NewValBf, ValBf, Vals*sizeof(TVal)); } - else { - for (TSize ValN = 0; ValN < Vals; ValN++){ NewValBf[ValN] = ValBf[ValN]; } } - if (EmptyVal != TVal()) { // init empty values - for (TSize ValN = Vals; ValN < MxVals; ValN++) { NewValBf[ValN] = EmptyVal; } - } - delete [] ValBf; - ValBf = NewValBf; - } -} - -template -TVecPool::TVecPool(const TSize& ExpectVals, const TSize& _GrowBy, const bool& _FastCopy, const TVal& _EmptyVal) : GrowBy(_GrowBy), MxVals(0), Vals(0), EmptyVal(_EmptyVal), ValBf(NULL) { - IdToOffV.Add(0); - Resize(ExpectVals); -} - -template -TVecPool::TVecPool(const TVecPool& Pool) : FastCopy(Pool.FastCopy), GrowBy(Pool.GrowBy), MxVals(Pool.MxVals), Vals(Pool.Vals), EmptyVal(Pool.EmptyVal), IdToOffV(Pool.IdToOffV) { - try { - ValBf = new TVal [MxVals]; } - catch (std::exception Ex) { - FailR(TStr::Fmt("TVecPool::TVecPool: %s, MxVals: %s. [Program failed to allocate memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), TInt::GetStr(uint64(MxVals)).CStr()).CStr()); } - IAssert(ValBf != NULL); - if (FastCopy) { - memcpy(ValBf, Pool.ValBf, MxVals*sizeof(TVal)); } - else { - for (TSize ValN = 0; ValN < MxVals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } } -} - -template -TVecPool::TVecPool(TSIn& SIn) : FastCopy(SIn) { - uint64 _GrowBy, _MxVals, _Vals; - SIn.Load(_GrowBy); SIn.Load(_MxVals); SIn.Load(_Vals); - IAssertR(_GrowBy -void TVecPool::Save(TSOut& SOut) const { - SOut.Save(FastCopy); - uint64 _GrowBy=GrowBy, _MxVals=MxVals, _Vals=Vals; - SOut.Save(_GrowBy); SOut.Save(_MxVals); SOut.Save(_Vals); - SOut.Save(EmptyVal); - for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN].Save(SOut); } - { SOut.Save(IdToOffV.Len()); SOut.Save(IdToOffV.Len()); - for (int ValN = 0; ValN < IdToOffV.Len(); ValN++) { - const uint64 Offset=IdToOffV[ValN]; SOut.Save(Offset); - } } -} - -template -TVecPool& TVecPool::operator = (const TVecPool& Pool) { - if (this!=&Pool) { - FastCopy = Pool.FastCopy; - GrowBy = Pool.GrowBy; - MxVals = Pool.MxVals; - Vals = Pool.Vals; - EmptyVal = Pool.EmptyVal; - IdToOffV=Pool.IdToOffV; - try { - ValBf = new TVal [MxVals]; } - catch (std::exception Ex) { - FailR(TStr::Fmt("TVecPool::operator=: %s, MxVals: %s. [Program failed to allocate memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), TInt::GetStr(uint64(MxVals)).CStr()).CStr()); } - IAssert(ValBf != NULL); - if (FastCopy) { - memcpy(ValBf, Pool.ValBf, Vals*sizeof(TVal)); } - else { - for (TSize ValN = 0; ValN < Vals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } } - } - return *this; -} - -template -int TVecPool::AddV(const TValV& ValV) { - const TSize ValVLen = ValV.Len(); - if (ValVLen == 0) { return 0; } - if (MxVals < Vals+ValVLen) { Resize(Vals+MAX(ValVLen, GrowBy)); } - if (FastCopy) { memcpy(ValBf+Vals, ValV.BegI(), sizeof(TVal)*ValV.Len()); } - else { for (TSize ValN=0; ValN < ValVLen; ValN++) { ValBf[Vals+ValN]=ValV[ValN]; } } - Vals+=ValVLen; IdToOffV.Add(Vals); - return IdToOffV.Len()-1; -} - -template -int TVecPool::AddEmptyV(const int& ValVLen) { - if (ValVLen==0){return 0;} - if (MxVals < Vals+ValVLen){Resize(Vals+MAX(TSize(ValVLen), GrowBy)); } - Vals+=ValVLen; IdToOffV.Add(Vals); - return IdToOffV.Len()-1; -} - -// Delete all elements of value DelVal from all vectors. Empty space is left at the end of the pool. -template -void TVecPool::CompactPool(const TVal& DelVal) { - ::TSize TotalDel=0, NDel=0; - // printf("Compacting %d vectors\n", IdToOffV.Len()); - for (int vid = 1; vid < IdToOffV.Len(); vid++) { - // if (vid % 10000000 == 0) { printf(" %dm", vid/1000000); fflush(stdout); } - const uint Len = GetVLen(vid); - TVal* ValV = GetValVPt(vid); - if (TotalDel > 0) { IdToOffV[vid-1] -= TotalDel; } // update end of vector - if (Len == 0) { continue; } - NDel = 0; - for (TVal* v = ValV; v < ValV+Len-NDel; v++) { - if (*v == DelVal) { - TVal* Beg = v; - while (*v == DelVal && v < ValV+Len) { v++; NDel++; } - memcpy(Beg, v, sizeof(TVal)*int(Len - ::TSize(v - ValV))); - v -= NDel; - } - } - memcpy(ValV-TotalDel, ValV, sizeof(TVal)*Len); // move data - TotalDel += NDel; - } - IdToOffV.Last() -= TotalDel; - for (::TSize i = Vals-TotalDel; i < Vals; i++) { ValBf[i] = EmptyVal; } - Vals -= TotalDel; - // printf(" deleted %llu elements from the pool\n", TotalDel); -} - -// shuffles all the order of elements in the pool (does not respect vector boundaries) -template -void TVecPool::ShuffleAll(TRnd& Rnd) { - for (::TSize n = Vals-1; n > 0; n--) { - const ::TSize k = ::TSize(((uint64(Rnd.GetUniDevInt())<<32) | uint64(Rnd.GetUniDevInt())) % (n+1)); - const TVal Tmp = ValBf[n]; - ValBf[n] = ValBf[k]; - ValBf[k] = Tmp; - } -} - - -///////////////////////////////////////////////// -// Below are old 32-bit implementations of TVec and other classes. -// Old TVec takes at most 2G elements. -// The new vector class supports 64-bits for the number of elements, -// but also allows 32-bits for backward compatibility. -// by Jure (Jan 2013) -namespace TGLib_OLD { -///////////////////////////////////////////////// -// Vector Pool -template -class TVecPool { -public: - typedef TPt > PVecPool; - typedef TVec TValV; -private: - TCRef CRef; - TBool FastCopy; - ::TSize GrowBy, MxVals, Vals; - TVal EmptyVal; // empty vector - TVal *ValBf; // buffer storing all the values - TVec< ::TSize> IdToOffV; // id to one past last (vector starts at [id-1]) -private: - void Resize(const ::TSize& _MxVals); -public: - TVecPool(const ::TSize& ExpectVals=0, const ::TSize& _GrowBy=1000000, const bool& _FastCopy=false, const TVal& _EmptyVal=TVal()); - TVecPool(const TVecPool& Pool); - TVecPool(TSIn& SIn); - ~TVecPool() { if (ValBf != NULL) { delete [] ValBf; } ValBf=NULL; } - static PVecPool New(const ::TSize& ExpectVals=0, const ::TSize& GrowBy=1000000, const bool& FastCopy=false) { - return new TVecPool(ExpectVals, GrowBy, FastCopy); } - static PVecPool Load(TSIn& SIn) { return new TVecPool(SIn); } - static PVecPool Load(const TStr& FNm) { TFIn FIn(FNm); return Load(FIn); } - void Save(TSOut& SOut) const; - - TVecPool& operator = (const TVecPool& Pool); - - ::TSize GetVals() const { return Vals; } - ::TSize GetVecs() const { return IdToOffV.Len(); } - bool IsVId(const int& VId) const { return (0 <= VId) && (VId < IdToOffV.Len()); } - ::TSize Reserved() const { return MxVals; } - void Reserve(const ::TSize& MxVals) { Resize(MxVals); } - const TVal& GetEmptyVal() const { return EmptyVal; } - void SetEmptyVal(const TVal& _EmptyVal) { EmptyVal = _EmptyVal; } - ::TSize GetMemUsed() const { - return sizeof(TCRef)+sizeof(TBool)+3*sizeof(TSize)+sizeof(TVal*)+MxVals*sizeof(TVal);} - - int AddV(const TValV& ValV); - int AddEmptyV(const int& ValVLen); - uint GetVLen(const int& VId) const { - if (VId==0){return 0;} - else {return uint(IdToOffV[VId]-IdToOffV[VId-1]);}} - TVal* GetValVPt(const int& VId) const { - if (GetVLen(VId)==0){return (TVal*)&EmptyVal;} - else {return ValBf+IdToOffV[VId-1];}} - void GetV(const int& VId, TValV& ValV) const { - if (GetVLen(VId)==0){ValV.Clr();} - else { ValV.GenExt(GetValVPt(VId), GetVLen(VId)); } } - void PutV(const int& VId, const TValV& ValV) { - IAssert(IsVId(VId) && GetVLen(VId) == ValV.Len()); - if (FastCopy) { - memcpy(GetValVPt(VId), ValV.BegI(), sizeof(TVal)*ValV.Len()); } - else { TVal* ValPt = GetValVPt(VId); - for (uint ValN=0; ValN < uint(ValV.Len()); ValN++, ValPt++) { *ValPt=ValV[ValN]; } - } - } - void CompactPool(const TVal& DelVal); // delete all elements of value DelVal from all vectors - void ShuffleAll(TRnd& Rnd=TInt::Rnd); // shuffles all the order of elements in the Pool (does not respect vector boundaries) - - //bool HasIdMap() const { return ! IdToOffV.Empty(); } - //void ClrIdMap() { IdToOffV.Clr(true); } - void Clr(bool DoDel = true) { - IdToOffV.Clr(DoDel); MxVals=0; Vals=0; - if (DoDel && ValBf!=NULL) { delete [] ValBf; ValBf=NULL;} - if (! DoDel) { PutAll(EmptyVal); } - } - void PutAll(const TVal& Val) { - for (TSize ValN = 0; ValN < MxVals; ValN++) { ValBf[ValN]=Val; } } - - friend class TPt >; -}; - -template -void TVecPool::Resize(const ::TSize& _MxVals){ - if (_MxVals <= MxVals){ return; } else { MxVals = _MxVals; } - if (ValBf == NULL) { - try { ValBf = new TVal [MxVals]; } - catch (std::exception Ex) { - FailR(TStr::Fmt("TVecPool::Resize 1: %s, MxVals: %d. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), _MxVals).CStr()); } - IAssert(ValBf != NULL); - if (EmptyVal != TVal()) { PutAll(EmptyVal); } - } else { - // printf("*** Resize vector pool: %llu -> %llu\n", uint64(Vals), uint64(MxVals)); - TVal* NewValBf = NULL; - try { NewValBf = new TVal [MxVals]; } - catch (std::exception Ex) { FailR(TStr::Fmt("TVecPool::Resize 2: %s, MxVals: %d. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), _MxVals).CStr()); } - IAssert(NewValBf != NULL); - if (FastCopy) { - memcpy(NewValBf, ValBf, Vals*sizeof(TVal)); } - else { - for (TSize ValN = 0; ValN < Vals; ValN++){ NewValBf[ValN] = ValBf[ValN]; } } - if (EmptyVal != TVal()) { // init empty values - for (TSize ValN = Vals; ValN < MxVals; ValN++) { NewValBf[ValN] = EmptyVal; } - } - delete [] ValBf; - ValBf = NewValBf; - } -} - -template -TVecPool::TVecPool(const ::TSize& ExpectVals, const ::TSize& _GrowBy, const bool& _FastCopy, const TVal& _EmptyVal) : - GrowBy(_GrowBy), MxVals(0), Vals(0), EmptyVal(_EmptyVal), ValBf(NULL) { - IdToOffV.Add(0); - Resize(ExpectVals); -} - -template -TVecPool::TVecPool(const TVecPool& Pool): - FastCopy(Pool.FastCopy), GrowBy(Pool.GrowBy), - MxVals(Pool.MxVals), Vals(Pool.Vals), EmptyVal(Pool.EmptyVal), IdToOffV(Pool.IdToOffV) { - try { ValBf = new TVal [MxVals]; } - catch (std::exception Ex) { FailR(TStr::Fmt("TVecPool::TVecPool: %s, MxVals: %d", Ex.what(), MxVals).CStr()); } - IAssert(ValBf != NULL); - if (FastCopy) { - memcpy(ValBf, Pool.ValBf, MxVals*sizeof(TVal)); } - else { - for (TSize ValN = 0; ValN < MxVals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } } -} - -template -TVecPool::TVecPool(TSIn& SIn): - FastCopy(SIn) { - uint64 _GrowBy, _MxVals, _Vals; - SIn.Load(_GrowBy); SIn.Load(_MxVals); SIn.Load(_Vals); - IAssert(_GrowBy -void TVecPool::Save(TSOut& SOut) const { - SOut.Save(FastCopy); - uint64 _GrowBy=GrowBy, _MxVals=MxVals, _Vals=Vals; - SOut.Save(_GrowBy); SOut.Save(_MxVals); SOut.Save(_Vals); - SOut.Save(EmptyVal); - for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN].Save(SOut); } - { SOut.Save(IdToOffV.Len()); SOut.Save(IdToOffV.Len()); - for (int ValN = 0; ValN < IdToOffV.Len(); ValN++) { - const uint64 Offset=IdToOffV[ValN]; SOut.Save(Offset); - } } -} - -template -TVecPool& TVecPool::operator = (const TVecPool& Pool) { - if (this!=&Pool) { - FastCopy = Pool.FastCopy; - GrowBy = Pool.GrowBy; - MxVals = Pool.MxVals; - Vals = Pool.Vals; - EmptyVal = Pool.EmptyVal; - IdToOffV=Pool.IdToOffV; - try { ValBf = new TVal [MxVals]; } - catch (std::exception Ex) { FailR(TStr::Fmt("TVec::operator= : %s, MxVals: %d", Ex.what(), MxVals).CStr()); } - IAssert(ValBf != NULL); - if (FastCopy) { - memcpy(ValBf, Pool.ValBf, Vals*sizeof(TVal)); } - else { - for (uint64 ValN = 0; ValN < Vals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } } - } - return *this; -} - -template -int TVecPool::AddV(const TValV& ValV) { - const ::TSize ValVLen = ValV.Len(); - if (ValVLen == 0) { return 0; } - if (MxVals < Vals+ValVLen) { Resize(Vals+MAX(ValVLen, GrowBy)); } - if (FastCopy) { memcpy(ValBf+Vals, ValV.BegI(), sizeof(TVal)*ValV.Len()); } - else { for (uint ValN=0; ValN < ValVLen; ValN++) { ValBf[Vals+ValN]=ValV[ValN]; } } - Vals+=ValVLen; IdToOffV.Add(Vals); - return IdToOffV.Len()-1; -} - -template -int TVecPool::AddEmptyV(const int& ValVLen) { - if (ValVLen==0){return 0;} - if (MxVals < Vals+ValVLen){Resize(Vals+MAX(TSize(ValVLen), GrowBy)); } - Vals+=ValVLen; IdToOffV.Add(Vals); - return IdToOffV.Len()-1; -} - -// delete all elements of value DelVal from all vectors -// empty space is left at the end of the pool -template -void TVecPool::CompactPool(const TVal& DelVal) { - ::TSize TotalDel=0, NDel=0; - // printf("Compacting %d vectors\n", IdToOffV.Len()); - for (int vid = 1; vid < IdToOffV.Len(); vid++) { - // if (vid % 10000000 == 0) { printf(" %dm", vid/1000000); fflush(stdout); } - const uint Len = GetVLen(vid); - TVal* ValV = GetValVPt(vid); - if (TotalDel > 0) { IdToOffV[vid-1] -= TotalDel; } // update end of vector - if (Len == 0) { continue; } - NDel = 0; - for (TVal* v = ValV; v < ValV+Len-NDel; v++) { - if (*v == DelVal) { - TVal* Beg = v; - while (*v == DelVal && v < ValV+Len) { v++; NDel++; } - memcpy(Beg, v, sizeof(TVal)*int(Len - ::TSize(v - ValV))); - v -= NDel; - } - } - memcpy(ValV-TotalDel, ValV, sizeof(TVal)*Len); // move data - TotalDel += NDel; - } - IdToOffV.Last() -= TotalDel; - for (::TSize i = Vals-TotalDel; i < Vals; i++) { ValBf[i] = EmptyVal; } - Vals -= TotalDel; - // printf(" deleted %llu elements from the pool\n", TotalDel); -} - -// shuffles all the order of elements in the pool (does not respect vector boundaries) -template -void TVecPool::ShuffleAll(TRnd& Rnd) { - for (::TSize n = Vals-1; n > 0; n--) { - const ::TSize k = ::TSize(((uint64(Rnd.GetUniDevInt())<<32) | uint64(Rnd.GetUniDevInt())) % (n+1)); - const TVal Tmp = ValBf[n]; - ValBf[n] = ValBf[k]; - ValBf[k] = Tmp; - } -} - -}; // namespace TGLib_OLD - -typedef TVecPool TIntVecPool; -typedef TPt PIntVecPool; - -///////////////////////////////////////////////// -// Vector-Pointer -template -class PVec{ -private: - TCRef CRef; -public: - TVec V; -public: - PVec(): V(){} - PVec(const PVec& Vec): V(Vec.V){} - static TPt > New(){ - return new PVec();} - PVec(const int& MxVals, const int& Vals): V(MxVals, Vals){} - static TPt > New(const int& MxVals, const int& Vals){ - return new PVec(MxVals, Vals);} - PVec(const TVec& _V): V(_V){} - static TPt > New(const TVec& V){ - return new PVec(V);} - explicit PVec(TSIn& SIn): V(SIn){} - static TPt > Load(TSIn& SIn){return new PVec(SIn);} - void Save(TSOut& SOut) const {V.Save(SOut);} - - PVec& operator=(const PVec& Vec){ - if (this!=&Vec){V=Vec.V;} return *this;} - bool operator==(const PVec& Vec) const {return V==Vec.V;} - bool operator<(const PVec& Vec) const {return V >; -}; - -///////////////////////////////////////////////// -// Common-Vector-Pointer-Types -typedef PVec TFltVP; -typedef TPt PFltV; -typedef PVec TAscFltVP; -typedef TPt PAscFltV; -typedef PVec TStrVP; -typedef TPt PStrV; - -///////////////////////////////////////////////// -// 2D-Vector -template -class TVVec{ -private: - TInt64 XDim, YDim; - TVec ValV; -public: - TVVec(): XDim(), YDim(), ValV(){} - TVVec(const TVVec& Vec): - XDim(Vec.XDim), YDim(Vec.YDim), ValV(Vec.ValV){} - TVVec(const TSizeTy& _XDim, const TSizeTy& _YDim): - XDim(), YDim(), ValV(){Gen(_XDim, _YDim);} - explicit TVVec(const TVec& _ValV, const TSizeTy& _XDim, const TSizeTy& _YDim): - XDim(_XDim), YDim(_YDim), ValV(_ValV){ IAssert(ValV.Len()==XDim*YDim); } - explicit TVVec(TSIn& SIn) {Load(SIn);} - void Load(TSIn& SIn){XDim.Load(SIn); YDim.Load(SIn); ValV.Load(SIn);} - void Save(TSOut& SOut) const { - XDim.Save(SOut); YDim.Save(SOut); ValV.Save(SOut);} - - TVVec& operator=(const TVVec& Vec){ - if (this!=&Vec){XDim=Vec.XDim; YDim=Vec.YDim; ValV=Vec.ValV;} return *this;} - bool operator==(const TVVec& Vec) const { - return (XDim==Vec.XDim)&&(YDim==Vec.YDim)&&(ValV==Vec.ValV);} - - bool Empty() const {return ValV.Len()==0;} - void Clr(){XDim=0; YDim=0; ValV.Clr();} - void Gen(const TSizeTy& _XDim, const TSizeTy& _YDim){ - Assert((_XDim>=0)&&(_YDim>=0)); - XDim=_XDim; YDim=_YDim; ValV.Gen(XDim*YDim);} - TSizeTy GetXDim() const {return XDim;} - TSizeTy GetYDim() const {return YDim;} - TSizeTy GetRows() const {return XDim;} - TSizeTy GetCols() const {return YDim;} - TVec& Get1DVec(){return ValV;} - - const TVal& At(const TSizeTy& X, const TSizeTy& Y) const { - Assert((0<=X)&&(X& Vec) const; - void GetCol(const TSizeTy& ColN, TVec& Vec) const; - - void SwapX(const TSizeTy& X1, const TSizeTy& X2); - void SwapY(const TSizeTy& Y1, const TSizeTy& Y2); - void Swap(TVVec& Vec); - - void ShuffleX(TRnd& Rnd); - void ShuffleY(TRnd& Rnd); - void GetMxValXY(TSizeTy& X, TSizeTy& Y) const; - - void CopyFrom(const TVVec& VVec); - void AddXDim(); - void AddYDim(); - void DelX(const TSizeTy& X); - void DelY(const TSizeTy& Y); -}; - -template -void TVVec::SwapX(const TSizeTy& X1, const TSizeTy& X2){ - for (TSizeTy Y=0; Y -void TVVec::SwapY(const TSizeTy& Y1, const TSizeTy& Y2){ - for (TSizeTy X=0; X -void TVVec::Swap(TVVec& Vec){ //J: - if (this!=&Vec){ - ::Swap(XDim, Vec.XDim); - ::Swap(YDim, Vec.YDim); - ValV.Swap(Vec.ValV); - } -} - -template -void TVVec::ShuffleX(TRnd& Rnd){ - for (TSizeTy X=0; X -void TVVec::ShuffleY(TRnd& Rnd){ - for (TSizeTy Y=0; Y -void TVVec::GetMxValXY(TSizeTy& X, TSizeTy& Y) const { - TSizeTy MxValN=ValV.GetMxValN(); - Y=MxValN%YDim; - X=MxValN/YDim; -} - -template -void TVVec::CopyFrom(const TVVec& VVec){ - TSizeTy CopyXDim = (GetXDim() < VVec.GetXDim()) ? GetXDim() : VVec.GetXDim(); - TSizeTy CopyYDim = (GetYDim() < VVec.GetYDim()) ? GetYDim() : VVec.GetYDim(); - for (TSizeTy X=0; X -void TVVec::AddXDim(){ - TVVec NewVVec(XDim+1, YDim); - NewVVec.CopyFrom(*this); - *this=NewVVec; -} - -template -void TVVec::AddYDim(){ - TVVec NewVVec(XDim, YDim+1); - NewVVec.CopyFrom(*this); - *this=NewVVec; -} - -template -void TVVec::DelX(const TSizeTy& X){ - TVVec NewVVec(XDim-1, YDim); - for (TSizeTy Y=0; Y -void TVVec::DelY(const TSizeTy& Y){ - TVVec NewVVec(XDim, YDim-1); - for (TSizeTy X=0; X -void TVVec::GetRow(const TSizeTy& RowN, TVec& Vec) const { - Vec.Gen(GetCols(), 0); - for (TSizeTy col = 0; col < GetCols(); col++) { - Vec.Add(At(RowN, col)); - } -} - -template -void TVVec::GetCol(const TSizeTy& ColN, TVec& Vec) const { - Vec.Gen(GetRows(), 0); - for (TSizeTy row = 0; row < GetRows(); row++) { - Vec.Add(At(row, ColN)); - } -} - -///////////////////////////////////////////////// -// Common-2D-Vector-Types -typedef TVVec TBoolVV; -typedef TVVec TChVV; -typedef TVVec TIntVV; -typedef TVVec TSFltVV; -typedef TVVec TFltVV; -typedef TVVec TStrVV; -typedef TVVec TIntPrVV; - -///////////////////////////////////////////////// -// 3D-Vector -template -class TVVVec{ -private: - TInt64 XDim, YDim, ZDim; - TVec ValV; -public: - TVVVec(): XDim(), YDim(), ZDim(), ValV(){} - TVVVec(const TVVVec& Vec): - XDim(Vec.XDim), YDim(Vec.YDim), ZDim(Vec.ZDim), ValV(Vec.ValV){} - TVVVec(const TSizeTy& _XDim, const TSizeTy& _YDim, const TSizeTy& _ZDim): - XDim(), YDim(), ZDim(), ValV(){Gen(_XDim, _YDim, _ZDim);} - explicit TVVVec(TSIn& SIn): - XDim(SIn), YDim(SIn), ZDim(SIn), ValV(SIn){} - void Save(TSOut& SOut) const { - XDim.Save(SOut); YDim.Save(SOut); ZDim.Save(SOut); ValV.Save(SOut);} - - TVVVec& operator=(const TVVVec& Vec){ - XDim=Vec.XDim; YDim=Vec.YDim; ZDim=Vec.ZDim; ValV=Vec.ValV; - return *this; - } - bool operator==(const TVVVec& Vec) const { - return (XDim==Vec.XDim)&&(YDim==Vec.YDim)&&(ZDim==Vec.ZDim)&& - (ValV==Vec.ValV);} - - bool Empty() const {return ValV.Len()==0;} - void Clr(){XDim=0; YDim=0; ZDim=0; ValV.Clr();} - void Gen(const TSizeTy& _XDim, const TSizeTy& _YDim, const TSizeTy& _ZDim){ - Assert((_XDim>=0)&&(_YDim>=0)&&(_ZDim>=0)); - XDim=_XDim; YDim=_YDim; ZDim=_ZDim; ValV.Gen(XDim*YDim*ZDim);} - TVal& At(const TSizeTy& X, const TSizeTy& Y, const TSizeTy& Z){ - Assert((0<=X)&&(X TIntVVV; -typedef TVVVec TFltVVV; - -///////////////////////////////////////////////// -// Tree -template -class TTree{ -private: - TVec > NodeV; // (ParentNodeId, ChildNodeIdV, NodeVal) -public: - TTree(): NodeV(){} - TTree(const TTree& Tree): NodeV(Tree.NodeV){} - explicit TTree(TSIn& SIn): NodeV(SIn){} - void Save(TSOut& SOut) const {NodeV.Save(SOut);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TTree& operator=(const TTree& Tree){if (this!=&Tree){NodeV=Tree.NodeV;} return *this;} - bool operator==(const TTree& Tree) const {return NodeV==Tree.NodeV;} - bool operator<(const TTree& Tree) const {return false;} - - int GetPrimHashCd() const {return NodeV.GetPrimHashCd();} - int GetSecHashCd() const {return NodeV.GetSecHashCd();} - - int GetMemUsed() const {return NodeV.GetMemUsed();} - - void Clr(){NodeV.Clr();} - - int AddNode(const int& ParentNodeId, const TVal& NodeVal=TVal()){ - IAssert(((ParentNodeId==-1)&&(NodeV.Len()==0))||(NodeV.Len()>0)); - if (ParentNodeId!=-1){NodeV[ParentNodeId].Val2.Add(NodeV.Len());} - return NodeV.Add(TTriple(ParentNodeId, TIntV(), NodeVal));} - int AddRoot(const TVal& NodeVal=TVal()){ - return AddNode(-1, NodeVal);} - - int GetNodes() const {return NodeV.Len();} - void GetNodeIdV(TIntV& NodeIdV, const int& NodeId=0); - int GetParentNodeId(const int& NodeId) const {return NodeV[NodeId].Val1;} - int GetChildren(const int& NodeId) const {return NodeV[NodeId].Val2.Len();} - int GetChildNodeId(const int& NodeId, const int& ChildN) const {return NodeV[NodeId].Val2[ChildN];} - TVal& GetNodeVal(const int& NodeId){return NodeV[NodeId].Val3;} - - void GenRandomTree(const int& Nodes, TRnd& Rnd); - - void DelNode(const int& NodeId); - void CopyTree(const int& SrcNodeId, TTree& DstTree, const int& DstParentNodeId=-1); - - void WrTree(const int& NodeId=0, const int& Lev=0); -}; - -template -void TTree::GetNodeIdV(TIntV& NodeIdV, const int& NodeId){ - if (NodeId==0){NodeIdV.Clr(); if (GetNodes()==0){return;}} - else if (GetParentNodeId(NodeId)==-1){return;} - NodeIdV.Add(NodeId); - for (int ChildN=0; ChildN -void TTree::GenRandomTree(const int& Nodes, TRnd& Rnd){ - Clr(); - if (Nodes>0){ - AddRoot(TVal()); - for (int NodeN=1; NodeN -void TTree::DelNode(const int& NodeId){ - if (NodeId==0){ - Clr(); - } else { - TIntV& ChildNodeIdV=NodeV[GetParentNodeId(NodeId)].Val2; - int ChildNodeIdN=ChildNodeIdV.SearchForw(NodeId); - ChildNodeIdV[ChildNodeIdN]=-1; - } -} - -template -void TTree::CopyTree(const int& SrcNodeId, TTree& DstTree, const int& DstParentNodeId){ - int DstNodeId=DstTree.AddNode(DstParentNodeId, GetNodeVal(SrcNodeId)); - for (int ChildN=0; ChildN -void TTree::WrTree(const int& NodeId, const int& Lev){ - for (int LevN=0; LevN TIntTree; -typedef TTree TFltTree; -typedef TTree TStrTree; -typedef TTree TStrIntPrTree; -typedef TTree TStrIntStrVTrTree; - -///////////////////////////////////////////////// -// Stack -template -class TSStack{ -private: - TVec ValV; -public: - TSStack(): ValV(){} - TSStack(const int& MxVals): ValV(MxVals, 0){} - TSStack(const TSStack& Stack): ValV(Stack.ValV){} - explicit TSStack(TSIn& SIn): ValV(SIn){} - void Save(TSOut& SOut) const {ValV.Save(SOut);} - - TSStack& operator=(const TSStack& Stack){ - if (this!=&Stack){ValV=Stack.ValV;} return *this;} - bool operator==(const TSStack& Stack) const {return this==&Stack;} - const TVal& operator[](const int& ValN) const {return ValV[ValV.Len()-ValN-1];} - TVal& operator[](const int& ValN) {return ValV[ValV.Len()-ValN-1];} - - bool Empty(){return ValV.Len()==0;} - void Clr(const bool& DoDel=false) {ValV.Clr(DoDel);} - bool IsIn(const TVal& Val) const {return ValV.IsIn(Val);} - int Len(){return ValV.Len();} - TVal& Top(){Assert(0 TIntS; -typedef TSStack TBoolChS; - -///////////////////////////////////////////////// -// Queue -template -class TQQueue{ -private: - TInt MxLast, MxLen; - TInt First, Last; - TVec ValV; -public: - TQQueue(const int& _MxLast=64, const int& _MxLen=-1): - MxLast(_MxLast), MxLen(_MxLen), First(0), Last(0), ValV(){ - Assert(int(MxLast)>0); Assert((MxLen==-1)||(int(MxLen)>0));} - TQQueue(const TQQueue& Queue): - MxLast(Queue.MxLast), MxLen(Queue.MxLen), - First(Queue.First), Last(Queue.Last), ValV(Queue.ValV){} - explicit TQQueue(TSIn& SIn): - MxLast(SIn), MxLen(SIn), First(SIn), Last(SIn), ValV(SIn){} - void Save(TSOut& SOut) const { - MxLast.Save(SOut); MxLen.Save(SOut); - First.Save(SOut); Last.Save(SOut); ValV.Save(SOut);} - - TQQueue& operator=(const TQQueue& Queue){ - if (this!=&Queue){MxLast=Queue.MxLast; MxLen=Queue.MxLen; - First=Queue.First; Last=Queue.Last; ValV=Queue.ValV;} - return *this;} - const TVal& operator[](const int& ValN) const {Assert((0<=ValN)&&(ValN& SubValV) const { - int BValN=TInt::GetMx(0, _BValN); - int EValN=TInt::GetMn(Len()-1, _EValN); - SubValV.Gen(EValN-BValN+1); - for (int ValN=BValN; ValN<=EValN; ValN++){ - SubValV[ValN-BValN]=ValV[Last+ValN];} - } - - bool Empty() const {return First==Last;} - int Len() const {return First-Last;} - const TVal& Top() const { - Assert(First!=Last); return ValV[Last];} - void Pop(){ - IAssert(First!=Last); Last++; - if (First==Last){ValV.Clr(); First=Last=0;}} - void Push(const TVal& Val){ - if (Last>MxLast){ValV.Del(0, Last-1); First-=Last; Last=0;} - if ((MxLen!=-1)&&(MxLen==Len())){Pop();} - First++; ValV.Add(Val);} - - void Shuffle(TRnd& Rnd){ - TVec ValV(Len(), 0); while (!Empty()){ValV.Add(Top()); Pop();} - ValV.Shuffle(Rnd); Clr(); - for (int ValN=0; ValN TIntQ; -typedef TQQueue TFltQ; -typedef TQQueue TStrQ; -typedef TQQueue TIntPrQ; -typedef TQQueue TIntStrPrQ; -typedef TQQueue TFltVQ; -typedef TQQueue TAscFltVQ; -typedef TVec > TIntQV; - -///////////////////////////////////////////////// -// List-Node -template -class TLstNd{ -public: - TLstNd* PrevNd; - TLstNd* NextNd; - TVal Val; -public: - TLstNd(): PrevNd(NULL), NextNd(NULL), Val(){} - TLstNd(const TLstNd&); - TLstNd(TLstNd* _PrevNd, TLstNd* _NextNd, const TVal& _Val): - PrevNd(_PrevNd), NextNd(_NextNd), Val(_Val){} - - TLstNd& operator=(const TLstNd&); - - bool IsPrev() const {return (PrevNd != NULL); } - bool IsNext() const {return (NextNd != NULL); } - TLstNd* Prev() const {Assert(this!=NULL); return PrevNd;} - TLstNd* Next() const {Assert(this!=NULL); return NextNd;} - TVal& GetVal(){Assert(this!=NULL); return Val;} - const TVal& GetVal() const {Assert(this!=NULL); return Val;} -}; - -///////////////////////////////////////////////// -// List -template -class TLst{ -public: - typedef TLstNd* PLstNd; -private: - int Nds; - PLstNd FirstNd; - PLstNd LastNd; -public: - TLst(): Nds(0), FirstNd(NULL), LastNd(NULL){} - TLst(const TLst&); - ~TLst(){Clr();} - explicit TLst(TSIn& SIn); - void Save(TSOut& SOut) const; - - TLst& operator=(const TLst&); - - void Clr(){ - PLstNd Nd=FirstNd; - while (Nd!=NULL){PLstNd NextNd=Nd->NextNd; delete Nd; Nd=NextNd;} - Nds=0; FirstNd=NULL; LastNd=NULL;} - - bool Empty() const {return Nds==0;} - int Len() const {return Nds;} - PLstNd First() const {return FirstNd;} - PLstNd Last() const {return LastNd;} - TVal& FirstVal() const {return FirstNd->GetVal();} - TVal& LastVal() const {return LastNd->GetVal();} - - PLstNd AddFront(const TVal& Val); - PLstNd AddBack(const TVal& Val); - PLstNd AddFrontSorted(const TVal& Val, const bool& Asc=true); - PLstNd AddBackSorted(const TVal& Val, const bool& Asc=true); - void PutFront(const PLstNd& Nd); - void PutBack(const PLstNd& Nd); - PLstNd Ins(const PLstNd& Nd, const TVal& Val); - void Del(const TVal& Val); - void Del(const PLstNd& Nd); - void DelFirst() { PLstNd DelNd = FirstNd; Del(DelNd); } - void DelLast() { PLstNd DelNd = LastNd; Del(DelNd); } - - PLstNd SearchForw(const TVal& Val); - PLstNd SearchBack(const TVal& Val); - - friend class TLstNd; -}; - -template -TLst::TLst(TSIn& SIn): - Nds(0), FirstNd(NULL), LastNd(NULL){ - int CheckNds=0; SIn.Load(CheckNds); - for (int NdN=0; NdN -void TLst::Save(TSOut& SOut) const { - SOut.Save(Nds); - PLstNd Nd=FirstNd; int CheckNds=0; - while (Nd!=NULL){ - Nd->Val.Save(SOut); Nd=Nd->NextNd; CheckNds++;} - IAssert(Nds==CheckNds); -} - -template -TLstNd* TLst::AddFront(const TVal& Val){ - PLstNd Nd=new TLstNd(NULL, FirstNd, Val); - if (FirstNd!=NULL){FirstNd->PrevNd=Nd; FirstNd=Nd;} - else {FirstNd=Nd; LastNd=Nd;} - Nds++; return Nd; -} - -template -TLstNd* TLst::AddBack(const TVal& Val){ - PLstNd Nd=new TLstNd(LastNd, NULL, Val); - if (LastNd!=NULL){LastNd->NextNd=Nd; LastNd=Nd;} - else {FirstNd=Nd; LastNd=Nd;} - Nds++; return Nd; -} - -template -TLstNd* TLst::AddFrontSorted(const TVal& Val, const bool& Asc){ - PLstNd Nd=First(); - if (Nd==NULL){ - return Ins(Nd, Val); - } else { - while ((Nd!=NULL)&&((Asc&&(Val>Nd()))||(!Asc&&(ValNext();} - if (Nd==NULL){return Ins(Nd->Last(), Val);} - else {return Ins(Nd->Prev(), Val);} - } -} - -template -TLstNd* TLst::AddBackSorted(const TVal& Val, const bool& Asc){ - PLstNd Nd=Last(); - while ((Nd!=NULL)&&((Asc&&(ValVal))||(!Asc&&(Val>Nd->Val)))){ - Nd=Nd->Prev();} - return Ins(Nd, Val); -} - -template -void TLst::PutFront(const PLstNd& Nd){ - Assert(Nd!=NULL); - // unchain - if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;} - else {Nd->PrevNd->NextNd=Nd->NextNd;} - if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;} - else {Nd->NextNd->PrevNd=Nd->PrevNd;} - // add to front - Nd->PrevNd=NULL; Nd->NextNd=FirstNd; - if (FirstNd!=NULL){FirstNd->PrevNd=Nd; FirstNd=Nd;} - else {FirstNd=Nd; LastNd=Nd;} -} - -template -void TLst::PutBack(const PLstNd& Nd){ - Assert(Nd!=NULL); - // unchain - if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;} - else {Nd->PrevNd->NextNd=Nd->NextNd;} - if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;} - else {Nd->NextNd->PrevNd=Nd->PrevNd;} - // add to back - Nd->PrevNd=LastNd; Nd->NextNd=NULL; - if (LastNd!=NULL){LastNd->NextNd=Nd; LastNd=Nd;} - else {FirstNd=Nd; LastNd=Nd;} -} - -template -TLstNd* TLst::Ins(const PLstNd& Nd, const TVal& Val){ - if (Nd==NULL){return AddFront(Val);} - else if (Nd->NextNd==NULL){return AddBack(Val);} - else { - PLstNd NewNd=new TLstNd(Nd, Nd->NextNd, Val); - Nd->NextNd=NewNd; NewNd->NextNd->PrevNd=Nd; - Nds++; return Nd; - } -} - -template -void TLst::Del(const TVal& Val){ - PLstNd Nd=SearchForw(Val); - if (Nd!=NULL){Del(Nd);} -} - -template -void TLst::Del(const PLstNd& Nd){ - Assert(Nd!=NULL); - if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;} - else {Nd->PrevNd->NextNd=Nd->NextNd;} - if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;} - else {Nd->NextNd->PrevNd=Nd->PrevNd;} - Nds--; delete Nd; -} - -template -TLstNd* TLst::SearchForw(const TVal& Val){ - PLstNd Nd=First(); - while (Nd!=NULL){ - if (Nd->GetVal()==Val){return Nd;} - Nd=Nd->Next(); - } - return NULL; -} - -template -TLstNd* TLst::SearchBack(const TVal& Val){ - PLstNd Nd=Last(); - while (Nd!=NULL){ - if (Nd->GetVal()==Val){return Nd;} - Nd=Nd->Prev(); - } - return NULL; -} - -///////////////////////////////////////////////// -// Common-List-Types -typedef TLst TIntL; -typedef TLstNd* PIntLN; -typedef TLst TIntKdL; -typedef TLstNd* PIntKdLN; -typedef TLst TFltL; -typedef TLstNd* PFltLN; -typedef TLst TFltIntKdL; -typedef TLstNd* PFltIntKdLN; -typedef TLst TAscFltIntKdL; -typedef TLstNd* PAscFltIntKdLN; -typedef TLst TStrL; -typedef TLstNd* PStrLN; - -///////////////////////////////////////////////// -// Record-File -template -class TFRec{ -private: - PFRnd FRnd; -public: - TFRec(const TStr& FNm, const TFAccess& FAccess, const bool& CreateIfNo): - FRnd(PFRnd(new TFRnd(FNm, FAccess, CreateIfNo, sizeof(THd), sizeof(TRec)))){} - TFRec(const TFRec&); - - TFRec& operator=(const TFRec&); - - void SetRecN(const int& RecN){FRnd->SetRecN(RecN);} - int GetRecN(){return FRnd->GetRecN();} - int GetRecs(){return FRnd->GetRecs();} - - void GetHd(THd& Hd){FRnd->GetHd(&Hd);} - void PutHd(const THd& Hd){FRnd->PutHd(&Hd);} - void GetRec(TRec& Rec, const int& RecN=-1){FRnd->GetRec(&Rec, RecN);} - void PutRec(const TRec& Rec, const int& RecN=-1){FRnd->PutRec(&Rec, RecN);} -}; - -///////////////////////////////////////////////// -// Function -template -class TFunc{ -private: - TFuncPt FuncPt; -public: - TFunc(): FuncPt(NULL){} - TFunc(const TFunc& Func): FuncPt(Func.FuncPt){} - TFunc(const TFuncPt& _FuncPt): FuncPt(_FuncPt){} - TFunc(TSIn&){Fail;} - void Save(TSOut&) const {Fail;} - - TFunc& operator=(const TFunc& Func){ - if (this!=&Func){FuncPt=Func.FuncPt;} return *this;} - bool operator==(const TFunc& Func) const { - return FuncPt==Func.FuncPt;} - bool operator<(const TFunc&) const { - Fail; return false;} - TFuncPt operator()() const {return FuncPt;} -}; diff --git a/inst/include/glib-core/dt.cpp b/inst/include/glib-core/dt.cpp deleted file mode 100644 index a3eaf5e..0000000 --- a/inst/include/glib-core/dt.cpp +++ /dev/null @@ -1,2333 +0,0 @@ -///////////////////////////////////////////////// -// Random -const int TRnd::RndSeed=0; -const int TRnd::a=16807; -const int TRnd::m=2147483647; -const int TRnd::q=127773; // m DIV a -const int TRnd::r=2836; // m MOD a - -void TRnd::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Seed=TXmlObjSer::GetIntArg(XmlTok, "Seed"); -} - -void TRnd::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Seed", TInt::GetStr(Seed)); -} - -void TRnd::PutSeed(const int& _Seed){ - Assert(_Seed>=0); - if (_Seed==0){ - //Seed=int(time(NULL)); - Seed=abs(int(TSysTm::GetPerfTimerTicks())); - } else { - Seed=_Seed; - //Seed=abs(_Seed*100000)+1; - } -} - -void TRnd::Move(const int& Steps){ - for (int StepN=0; StepN=1.0)||(rsq==0.0)); // and if they are not, try again - double fac=sqrt(-2.0*log(rsq)/rsq); // Box-Muller transformation - return v1*fac; -// return v2*fac; // second deviate -} - -double TRnd::GetNrmDev( - const double& Mean, const double& SDev, const double& Mn, const double& Mx){ - double Val=Mean+GetNrmDev()*SDev; - if (ValMx){Val=Mx;} - return Val; -} - -double TRnd::GetExpDev(){ - double UniDev; - do { - UniDev=GetUniDev(); - } while (UniDev==0.0); - return -log(UniDev); -} - -double TRnd::GetExpDev(const double& Lambda) { - return GetExpDev()/Lambda; -} - -double TRnd::GetGammaDev(const int& Order){ - int j; - double am,e,s,v1,v2,x,y; - if (Order<1){Fail;} - if (Order<6) { - x=1.0; - for (j=1;j<=Order;j++) x *=GetUniDev(); - x = -log(x); - } else { - do { - do { - do { - v1=2.0*GetUniDev()-1.0; - v2=2.0*GetUniDev()-1.0; - } while (v1*v1+v2*v2 > 1.0); - y=v2/v1; - am=Order-1; - s=sqrt(2.0*am+1.0); - x=s*y+am; - } while (x <= 0.0); - e=(1.0+y*y)*exp(am*log(x/am)-s*y); - } while (GetUniDev()>e); - } - return x; -} - -double TRnd::GetPoissonDev(const double& Mean){ - static double sq,alxm,g,oldm=(-1.0); - double em,t,y; - if (Mean < 12.0) { - if (Mean != oldm) { - oldm=Mean; - g=exp(-Mean); - } - em = -1; - t=1.0; - do { - ++em; - t *= GetUniDev(); - } while (t>g); - } else { - if (Mean != oldm) { - oldm=Mean; - sq=sqrt(2.0*Mean); - alxm=log(Mean); - g=Mean*alxm-TSpecFunc::LnGamma(Mean+1.0); - } - do { - do { - y=tan(TMath::Pi*GetUniDev()); - em=sq*y+Mean; - } while (em < 0.0); - em=floor(em); - t=0.9*(1.0+y*y)*exp(em*alxm-TSpecFunc::LnGamma(em+1.0)-g); - } while (GetUniDev()>t); - } - return em; -} - -double TRnd::GetBinomialDev(const double& Prb, const int& Trials){ - int j; - static int nold=(-1); - double am,em,g,angle,p,bnl,sq,t,y; - static double pold=(-1.0),pc,plog,pclog,en,oldg; - - p=(Prb <= 0.5 ? Prb : 1.0-Prb); - am=Trials*p; - if (Trials < 25) { - bnl=0.0; - for (j=1;j<=Trials;j++) - if (GetUniDev() < p) ++bnl; - } else if (am < 1.0) { - g=exp(-am); - t=1.0; - for (j=0;j<=Trials;j++) { - t *= GetUniDev(); - if (t < g) break; - } - bnl=(j <= Trials ? j : Trials); - } else { - if (Trials != nold) { - en=Trials; - oldg=TSpecFunc::LnGamma(en+1.0); - nold=Trials; - } if (p != pold) { - pc=1.0-p; - plog=log(p); - pclog=log(pc); - pold=p; - } - sq=sqrt(2.0*am*pc); - do { - do { - angle=TMath::Pi*GetUniDev(); - y=tan(angle); - em=sq*y+am; - } while (em < 0.0 || em >= (en+1.0)); - em=floor(em); - t=1.2*sq*(1.0+y*y)*exp(oldg-(em+1.0) - -TSpecFunc::LnGamma(en-em+1.0)+em*plog+(en-em)*pclog); - } while (GetUniDev() > t); - bnl=em; - } - if (p != Prb) bnl=Trials-bnl; - return bnl; -} - -// sample points from d-dimensional unit sphere -/*void TRnd::GetSphereDev(const int& Dim, TFltV& ValV) { - if (ValV.Len() != Dim) { ValV.Gen(Dim); } - double Length = 0.0; - for (int i = 0; i < Dim; i++) { - ValV[i] = GetNrmDev(); - Length += TMath::Sqr(ValV[i]); } - Length = 1.0 / sqrt(Length); - for (int i = 0; i < Dim; i++) { - ValV[i] *= Length; - } -}*/ - -TRnd TRnd::LoadTxt(TILx& Lx){ - return TRnd(Lx.GetInt()); -} - -void TRnd::SaveTxt(TOLx& Lx) const { - Lx.PutInt(Seed); -} - -///////////////////////////////////////////////// -// Memory -void TMem::Resize(const int& _MxBfL){ - if (_MxBfL<=MxBfL){return;} - else {if (MxBfL*2<_MxBfL){MxBfL=_MxBfL;} else {MxBfL*=2;}} - char* NewBf=new char[MxBfL]; IAssert(NewBf!=NULL); - if (BfL>0){memcpy(NewBf, Bf, BfL);} - if (Bf!=NULL){delete[] Bf;} - Bf=NewBf; -} - -TMem::TMem(const TStr& Str): - MxBfL(Str.Len()), BfL(MxBfL), Bf(NULL){ - if (MxBfL>0){ - Bf=new char[MxBfL]; - if (BfL>0){memcpy(Bf, Str.CStr(), BfL);} - } -} - -void TMem::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveHdArg(Nm, "BfL", TInt::GetStr(BfL)); - SOut.PutStr(TXmlLx::GetXmlStrFromPlainMem(*this)); -} - -bool TMem::DoFitStr(const TStr& Str) const { - return DoFitLen(Str.Len()+1); -} - -TMem& TMem::operator+=(const char& Ch){ - if (BfL==MxBfL){Resize(BfL+1);} - Bf[BfL]=Ch; BfL++; return *this; -} - -TMem& TMem::operator+=(const TMem& Mem){ - int LBfL=Mem.Len(); if (BfL+LBfL>MxBfL){Resize(BfL+LBfL);} - if (LBfL>0){memcpy(&Bf[BfL], Mem(), LBfL);} - BfL+=LBfL; return *this; -} - -TMem& TMem::operator+=(const TStr& Str){ - int LBfL=Str.Len(); if (BfL+LBfL>MxBfL){Resize(BfL+LBfL);} - if (LBfL>0){memcpy(Bf+BfL, Str.CStr(), LBfL);} - BfL+=LBfL; return *this; -} - -TMem& TMem::operator+=(const PSIn& SIn){ - int LBfL=SIn->Len(); if (BfL+LBfL>MxBfL){Resize(BfL+LBfL);} - char* LBf=new char[LBfL]; - SIn->GetBf(LBf, LBfL); - if (LBfL>0){memcpy(Bf+BfL, LBf, LBfL);} - delete[] LBf; - BfL+=LBfL; return *this; -} - -void TMem::Del(const int& BChN, const int& EChN){ - if (BChN>EChN){return;} - if ((BChN==0)&&(EChN==BfL-1)){Clr(); return;} - IAssert((0<=BChN)&&(BChN<=EChN)&&(EChN0){memcpy(&Bf[BfL], Str.CStr(), LBfL);} -// int FChN=BfL; BfL+=LBfL; return FChN; -//} - -void TMem::AddBf(const void* _Bf, const int& _BfL){ - IAssert((_BfL>=0) && (_Bf != NULL)); - Reserve(Len() + _BfL, false); - memcpy(Bf + BfL, _Bf, _BfL); - BfL+=_BfL; - //char* ChBf=(char*)Bf; - //for (int BfC=0; BfCMxBfL){delete[] Bf; Bf=new char[(MxBfL=ChA.BfL)+1];} - BfL=ChA.BfL; strcpy(Bf, ChA.CStr()); - } - return *this; -} - -TChA& TChA::operator=(const TStr& Str){ - if (Str.Len()>MxBfL){delete[] Bf; Bf=new char[(MxBfL=Str.Len())+1];} - BfL=Str.Len(); strcpy(Bf, Str.CStr()); - return *this; -} - -TChA& TChA::operator=(const char* CStr){ - int CStrLen=int(strlen(CStr)); - if (CStrLen>MxBfL){delete[] Bf; Bf=new char[(MxBfL=CStrLen)+1];} - BfL=CStrLen; strcpy(Bf, CStr); - return *this; -} - -TChA& TChA::operator+=(const TMem& Mem) { - Resize(BfL+Mem.Len()); - strcpy(Bf+BfL, Mem.GetBf()); BfL+=Mem.Len(); return *this; -} - -TChA& TChA::operator+=(const TChA& ChA){ - Resize(BfL+ChA.Len()); - strcpy(Bf+BfL, ChA.CStr()); BfL+=ChA.Len(); return *this; -} - -TChA& TChA::operator+=(const TStr& Str){ - Resize(BfL+Str.Len()); - strcpy(Bf+BfL, Str.CStr()); BfL+=Str.Len(); return *this; -} - -TChA& TChA::operator+=(const char* CStr){ - int CStrLen=(int)strlen(CStr); Resize(BfL+CStrLen); - strcpy(Bf+BfL, CStr); BfL+=CStrLen; return *this; -} - -void TChA::Ins(const int& BChN, const char* CStr){ - Assert((0<=BChN)&&(BChN<=BfL)); //** ali je <= v (BChN<=BfL) upravicen? - int CStrLen=int(strlen(CStr)); Resize(BfL+CStrLen); - memmove(Bf+BChN+CStrLen, Bf+BChN, BfL-BChN+1); - memmove(Bf+BChN, CStr, CStrLen); BfL+=CStrLen; -} - -void TChA::Del(const int& ChN){ - Assert((0<=ChN)&&(ChN= Len() || BChN < 0) { BChN = Len()-1; } - for (int i = BChN; i >= 0; i--) { - if (GetCh(i) == Ch) { return i; } - } - return -1; -} - -int TChA::SearchStr(const TChA& Str, const int& BChN) const { - return SearchStr(Str.CStr(), BChN); -} - -int TChA::SearchStr(const TStr& Str, const int& BChN) const { - return SearchStr(Str.CStr(), BChN); -} - -int TChA::SearchStr(const char* CStr, const int& BChN) const { - const char* BegPos=strstr(Bf+BChN, CStr); - if (BegPos==NULL){return -1;} - else {return int(BegPos-Bf);} -} - -bool TChA::IsPrefix(const char* CStr, const int& BChN) const { - if (BChN+(int)strlen(CStr)>Len()){return false;} - const char* B = Bf+BChN; - const char* C = CStr; - while (*C!=0 && *B==*C) { - B++; C++; - } - if (*C==0){return true;} - else {return false;} -} - -bool TChA::IsPrefix(const TStr& Str) const { - return IsPrefix(Str.CStr()); -} - -bool TChA::IsPrefix(const TChA& Str) const { - return IsPrefix(Str.CStr()); -} - -bool TChA::IsSuffix(const char* CStr) const { - if ((int)strlen(CStr) > Len()) { return false; } - const char* E = Bf+Len()-1; - const char* C = CStr+strlen(CStr)-1; - while (C >= CStr && *E==*C) { - E--; C--; - } - if (C+1 == CStr) { return true; } - else { return false; } -} - -bool TChA::IsSuffix(const TStr& Str) const { - return IsSuffix(Str.CStr()); -} - -bool TChA::IsSuffix(const TChA& Str) const { - return IsSuffix(Str.CStr()); -} - -void TChA::ChangeCh(const char& SrcCh, const char& DstCh){ - int StrLen=Len(); - for (int ChN=0; ChN=0)&&TCh::IsWs(GetCh(EChN))){EChN--;} - if ((BChN!=0)||(EChN!=StrLen-1)){ - int DstChN=0; - for (int SrcChN=BChN; SrcChN<=EChN; SrcChN++){ - PutCh(DstChN, GetCh(SrcChN)); DstChN++;} - Trunc(DstChN); - } - return *this; -} - -void TChA::CompressWs(){ - int StrLen=Len(); int SrcChN=0; int DstChN=0; - while ((SrcChN0)&&(TCh::IsWs(GetCh(DstChN-1)))){ - SrcChN++; - } else { - PutCh(DstChN, GetCh(SrcChN)); SrcChN++; DstChN++; - } - } - if ((DstChN>0)&&(TCh::IsWs(GetCh(DstChN-1)))){DstChN--;} - Trunc(DstChN); -} - -void TChA::Swap(const int& ChN1, const int& ChN2){ - char Ch=GetCh(ChN1); - PutCh(ChN1, GetCh(ChN2)); - PutCh(ChN2, Ch); -} - -void TChA::Swap(TChA& ChA) { - ::Swap(MxBfL, ChA.MxBfL); - ::Swap(BfL, ChA.BfL); - ::Swap(Bf, ChA.Bf); -} - -int TChA::GetPrimHashCd() const { - return TStrHashF_DJB::GetPrimHashCd(CStr()); -} - -int TChA::GetSecHashCd() const { - return TStrHashF_DJB::GetSecHashCd(CStr()); -} - - -void TChA::LoadTxt(const PSIn& SIn, TChA& ChA){ - delete[] ChA.Bf; - ChA.Bf=new char[(ChA.MxBfL=ChA.BfL=SIn->Len())+1]; - SIn->GetBf(ChA.CStr(), SIn->Len()); ChA.Bf[ChA.BfL]=0; -} - -void TChA::SaveTxt(const PSOut& SOut) const { - SOut->SaveBf(CStr(), Len()); -} - -/*TChA operator+(const TChA& LStr, const TChA& RStr){ - return LStr+=RStr; } -} - -TChA operator+(const TChA& LStr, const TStr& RStr){ - return LStr+=RStr.CStr(); -} - -TChA operator+(const TStr& LStr, const char* RCStr){ - return LStr+=RCStr; -}*/ - - -///////////////////////////////////////////////// -// Input-Char-Array -TChAIn::TChAIn(const TChA& ChA, const int& _BfC): - TSBase("Input-Char-Array"), TSIn("Input-Char-Array"), Bf(ChA.CStr()), BfC(_BfC), BfL(ChA.Len()){} - -int TChAIn::GetBf(const void* LBf, const TSize& LBfL){ - Assert(TSize(BfC+LBfL)<=TSize(BfL)); - int LBfS=0; - for (TSize LBfC=0; LBfC0){ - Bf[0]=(char)toupper(Bf[0]);} - for (int ChN=1; ChNCStr(); int CStrLen=int(strlen(CStr)); - TRStr* NewRStr; - if (CStrLen==0){NewRStr=TRStr::GetNullRStr();} - // next lines are not multi-threading safe - //else if (CStrLen==1){NewRStr=GetChStr(CStr[0]).RStr;} - //else if (CStrLen==2){NewRStr=GetDChStr(CStr[0], CStr[1]).RStr;} - else {NewRStr=RStr;} - NewRStr->MkRef(); RStr->UnRef(); RStr=NewRStr; -} - -void TStr::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - TStr TokStr=XmlTok->GetTokStr(false); - operator=(TokStr); -} - -void TStr::SaveXml(TSOut& SOut, const TStr& Nm) const { - TStr XmlStr=TXmlLx::GetXmlStrFromPlainStr(*this); - if (XmlStr.Empty()){XSaveBETag(Nm);} - else {XSaveHd(Nm); SOut.PutStr(XmlStr);} -} - -TStr& TStr::ToUc(){ - TRStr* NewRStr=new TRStr(RStr->CStr()); NewRStr->ToUc(); - RStr->UnRef(); RStr=NewRStr; RStr->MkRef(); - Optimize(); return *this; -} - -TStr& TStr::ToLc(){ - TRStr* NewRStr=new TRStr(RStr->CStr()); NewRStr->ToLc(); - RStr->UnRef(); RStr=NewRStr; RStr->MkRef(); - Optimize(); return *this; -} - -TStr& TStr::ToCap(){ - TRStr* NewRStr=new TRStr(RStr->CStr()); NewRStr->ToCap(); - RStr->UnRef(); RStr=NewRStr; RStr->MkRef(); - Optimize(); return *this; -} - -TStr& TStr::ToTrunc(){ - int ThisLen=Len(); char* ThisBf=CStr(); - int BChN=0; int EChN=ThisLen-1; - while ((BChN=0)&&TCh::IsWs(ThisBf[EChN])){EChN--;} - *this=GetSubStr(BChN, EChN); - return *this; -} - -TStr& TStr::ConvUsFromYuAscii(){ - TRStr* NewRStr=new TRStr(RStr->CStr()); NewRStr->ConvUsFromYuAscii(); - RStr->UnRef(); RStr=NewRStr; RStr->MkRef(); - Optimize(); return *this; -} - -TStr& TStr::ToHex(){ - TChA ChA; - int StrLen=Len(); - for (int ChN=0; ChNBf[ChN]); - char MshCh=TCh::GetHexCh((Ch/16)%16); - char LshCh=TCh::GetHexCh(Ch%16); - ChA+=MshCh; ChA+=LshCh; - } - *this=ChA; - return *this; -} - -TStr& TStr::FromHex(){ - int StrLen=Len(); IAssert(StrLen%2==0); - TChA ChA; int ChN=0; - while (ChNBf[ChN]; ChN++; - char LshCh=RStr->Bf[ChN]; ChN++; - uchar Ch=uchar(TCh::GetHex(MshCh)*16+TCh::GetHex(LshCh)); - ChA+=Ch; - } - *this=ChA; - return *this; -} - -TStr TStr::GetSubStr(const int& _BChN, const int& _EChN) const { - int StrLen=Len(); - int BChN=TInt::GetMx(_BChN, 0); - int EChN=TInt::GetMn(_EChN, StrLen-1); - int Chs=EChN-BChN+1; - if (Chs<=0){return TStr();} - else if (Chs==StrLen){return *this;} - else { - char* Bf=new char[Chs+1]; strncpy(Bf, CStr()+BChN, Chs); Bf[Chs]=0; - TStr Str(Bf); delete[] Bf; - return Str; - } -} - -void TStr::InsStr(const int& BChN, const TStr& Str){ - int ThisLen=Len(); - IAssert((0<=BChN)&&(BChN<=ThisLen)); - TStr NewStr; - if (BChN==0){ - NewStr=Str+*this; - } else - if (BChN==ThisLen){ - NewStr=*this+Str; - } else { - NewStr=GetSubStr(0, BChN-1)+Str+GetSubStr(BChN, ThisLen-1); - } - *this=NewStr; -} - -void TStr::DelChAll(const char& Ch){ - TChA ChA(*this); - int ChN=ChA.SearchCh(Ch); - while (ChN!=-1){ - ChA.Del(ChN); - ChN=ChA.SearchCh(Ch); - } - *this=ChA; -} - -void TStr::DelSubStr(const int& _BChN, const int& _EChN){ - int BChN=TInt::GetMx(_BChN, 0); - int EChN=TInt::GetMn(_EChN, Len()-1); - int Chs=Len()-(EChN-BChN+1); - if (Chs==0){Clr();} - else if (Chs=0)&&(ThisBf[ChN]!=SplitCh)){ChN--;} - return (ChN==-1) ? "" : GetSubStr(0, ChN-1); -} - -TStr TStr::RightOf(const char& SplitCh) const { - int ThisLen=Len(); const char* ThisBf=CStr(); - int ChN=0; - while ((ChN=0)&&(ThisBf[ChN]!=SplitCh)){ChN--;} - return (ChN==-1) ? "" : GetSubStr(ChN+1, ThisLen-1); -} - -void TStr::SplitOnCh(TStr& LStr, const char& SplitCh, TStr& RStr) const { - int ThisLen=Len(); const char* ThisBf=CStr(); - int ChN=0; - while ((ChN=0)&&(ThisBf[ChN]!=SplitCh)){ChN--;} - if (ChN==-1){ - LStr=""; RStr=*this; - } else - if (ChN==0){ - LStr=""; RStr=GetSubStr(1, ThisLen-1); - } else { - LStr=GetSubStr(0, ChN-1); RStr=GetSubStr(ChN+1, ThisLen-1); - } -} - -void TStr::SplitOnAllCh( - const char& SplitCh, TStrV& StrV, const bool& SkipEmpty) const { - StrV.Clr(); - char* Bf=new char[Len()+1]; - strcpy(Bf, CStr()); - char* CurStrBf=Bf; - forever{ - char* BfC=CurStrBf; - while ((*BfC!=0)&&(*BfC!=SplitCh)){BfC++;} - bool IsEnd=(*BfC=='\0'); - *BfC=0; - if ((BfC>CurStrBf)||(!SkipEmpty)){StrV.Add(TStr(CurStrBf));} - if (IsEnd){break;} - CurStrBf=BfC+1; - } - delete[] Bf; -} - -void TStr::SplitOnAllAnyCh( - const TStr& SplitChStr, TStrV& StrV, const bool& SkipEmpty) const { - // reset string output-vector - StrV.Clr(); - // prepare working-copy of string - char* Bf=new char[Len()+1]; - strcpy(Bf, CStr()); - char* CurStrBf=Bf; // pointer to current string - // prepare pointer to split-char-string - const char* SplitChBf=SplitChStr.CStr(); - forever{ - char* BfC=CurStrBf; // set the counter for working-string - while (*BfC!=0){ - const char* SplitChBfC=SplitChBf; // set counter for split-char-string - while ((*SplitChBfC!=0)&&(*SplitChBfC!=*BfC)){SplitChBfC++;} - if (*SplitChBfC!=0){break;} // if split-char found - BfC++; - } - bool IsEnd=(*BfC==0); - *BfC=0; - if ((BfC>CurStrBf)||(!SkipEmpty)){StrV.Add(TStr(CurStrBf));} - if (IsEnd){break;} - CurStrBf=BfC+1; - } - // delete working-copy - delete[] Bf; -} - -void TStr::SplitOnWs(TStrV& StrV) const { - StrV.Clr(); - char* Bf=new char[Len()+1]; - strcpy(Bf, CStr()); - char* StrBf=Bf; - forever{ - while ((*StrBf!=0)&&(TCh::IsWs(*StrBf))){StrBf++;} - char* BfC=StrBf; - while ((*BfC!=0)&&(!TCh::IsWs(*BfC))){BfC++;} - bool IsEnd=(*BfC=='\0'); - *BfC=0; - if (BfC>StrBf){StrV.Add(TStr(StrBf));} - if (IsEnd){break;} - StrBf=BfC+1; - } - delete[] Bf; -} - -void TStr::SplitOnNonAlNum(TStrV& StrV) const { - StrV.Clr(); - char* Bf=new char[Len()+1]; - strcpy(Bf, CStr()); - char* StrBf=Bf; - forever{ - while ((*StrBf!=0)&&(!TCh::IsAlNum(*StrBf))){StrBf++;} - char* BfC=StrBf; - while ((*BfC!=0)&&(TCh::IsAlNum(*BfC))){BfC++;} - bool IsEnd=(*BfC=='\0'); - *BfC=0; - if (BfC>StrBf){StrV.Add(TStr(StrBf));} - if (IsEnd){break;} - StrBf=BfC+1; - } - delete[] Bf; -} - -void TStr::SplitOnStr(const TStr& SplitStr, TStrV& StrV) const { - StrV.Clr(); - int SplitStrLen=SplitStr.Len(); - int PrevChN=0; int ChN=0; - while ((ChN=SearchStr(SplitStr, ChN))!=-1){ - // extract & add string - TStr SubStr=GetSubStr(PrevChN, ChN-1); - StrV.Add(SubStr); - PrevChN=ChN=ChN+SplitStrLen; - } - // add last string - TStr LastSubStr=GetSubStr(PrevChN, Len()-1); - StrV.Add(LastSubStr); -} - -void TStr::SplitOnStr(TStr& LeftStr, const TStr& MidStr, TStr& RightStr) const { - const int ChN=SearchStr(MidStr); - if (ChN==-1){ - LeftStr=*this; RightStr=GetNullStr(); - } else { - LeftStr=GetSubStr(0, ChN-1); - RightStr=GetSubStr(ChN+MidStr.Len(), Len()-1); - } -} - -int TStr::CountCh(const char& Ch, const int& BChN) const { - const int ThisLen=Len(); - const char* ThisBf=CStr(); - int Chs=0; - for (int ChN=TInt::GetMx(BChN, 0); ChN=StrLen){BChN=StrLen-1;} - const char* ThisBf=CStr(); - const char* Pt=ThisBf + BChN; - while (Pt>=ThisBf) { - if (*Pt==Ch){return (int)(Pt-ThisBf);} - Pt--; - } - return -1; -} - -int TStr::SearchStr(const TStr& Str, const int& BChN) const { - int NrBChN=TInt::GetMx(BChN, 0); - const char* StrPt=strstr((const char*)CStr()+NrBChN, Str.CStr()); - if (StrPt==NULL){return -1;} - else {return int(StrPt-CStr());} -/* // slow implementation - int ThisLen=Len(); int StrLen=Str.Len(); - int ChN=TInt::GetMx(BChN, 0); - while (ChN thisLen) { - return false; - } else { - size_t minLen = MIN(len, thisLen); - int cmp = strncmp(Str, RStr->Bf, minLen); - return cmp == 0; - } -} - -bool TStr::IsSuffix(const char *Str) const { - size_t len = strlen(Str); - size_t thisLen = Len(); - if (len > thisLen) { - // too long to be a suffix anyway - return false; - } else { - // move to the point in the buffer where we would expect the suffix to be - const char *ending = RStr->Bf + thisLen - len; - int cmp = strncmp(Str, ending, len); - return cmp == 0; - } -} - -int TStr::ChangeCh(const char& SrcCh, const char& DstCh, const int& BChN){ - int ChN=SearchCh(SrcCh, BChN); - if (ChN!=-1){PutCh(ChN, DstCh);} - return ChN; -} - -int TStr::ChangeChAll(const char& SrcCh, const char& DstCh){ - int FirstChN=SearchCh(SrcCh); - if (FirstChN==-1){ - return 0; - } else { - TRStr* NewRStr=new TRStr(RStr->CStr()); - RStr->UnRef(); RStr=NewRStr; RStr->MkRef(); - char* ThisBf=CStr(); int StrLen=Len(); int Changes=0; - for (int ChN=FirstChN; ChNPutCh(ChN, DstCh); Changes++;} - if (ThisBf[ChN]==SrcCh){ThisBf[ChN]=DstCh; Changes++;} - } - Optimize(); - return Changes; - } -} - -int TStr::ChangeStr(const TStr& SrcStr, const TStr& DstStr, const int& BChN){ - int ChN=SearchStr(SrcStr, BChN); - if (ChN==-1){ - return -1; - } else { - DelSubStr(ChN, ChN+SrcStr.Len()-1); - InsStr(ChN, DstStr); - return ChN; - } -} - -int TStr::ChangeStrAll(const TStr& SrcStr, const TStr& DstStr, const bool& FromStartP){ - const int DstStrLen=DstStr.Len(); - int Changes=0-1; int BChN=0-DstStrLen; - do { - Changes++; - if (FromStartP){BChN=0-DstStrLen;} - BChN+=DstStrLen; - BChN=ChangeStr(SrcStr, DstStr, BChN); - } while (BChN!=-1); - return Changes; -} - -bool TStr::IsBool(bool& Val) const { - if (operator==("T")){Val=true; return true;} - else if (operator==("F")){Val=false; return true;} - else {return false;} -} - -bool TStr::IsInt( - const bool& Check, const int& MnVal, const int& MxVal, int& Val) const { - // parsing format {ws} [+/-] +{ddd} - int _Val=0; - bool Minus=false; - TChRet Ch(TStrIn::New(*this)); - while (TCh::IsWs(Ch.GetCh())){} - if (Ch()=='+'){Minus=false; Ch.GetCh();} - if (Ch()=='-'){Minus=true; Ch.GetCh();} - if (!TCh::IsNum(Ch())){return false;} - _Val=TCh::GetNum(Ch()); - while (TCh::IsNum(Ch.GetCh())){_Val=10*_Val+TCh::GetNum(Ch());} - if (Minus){_Val=-_Val;} - if (Check&&((_ValMxVal))){return false;} - if (Ch.Eof()){Val=_Val; return true;} else {return false;} -} - -bool TStr::IsUInt( - const bool& Check, const uint& MnVal, const uint& MxVal, uint& Val) const { - // parsing format {ws} [+]{ddd} - uint _Val=0; - TChRet Ch(TStrIn::New(*this)); - while (TCh::IsWs(Ch.GetCh())){} - if (Ch()=='+'){Ch.GetCh();} - if (!TCh::IsNum(Ch())){return false;} - _Val=TCh::GetNum(Ch()); - while (TCh::IsNum(Ch.GetCh())){_Val=10*_Val+TCh::GetNum(Ch());} - if (Check&&((_ValMxVal))){return false;} - if (Ch.Eof()){Val=_Val; return true;} else {return false;} -} - -bool TStr::IsHexInt( const bool& Check, const int& MnVal, const int& MxVal, int& Val) const { - // parsing format {ws} [+/-][0x] +{XXX} - int _Val=0; - bool Minus=false; - TChRet Ch(TStrIn::New(*this)); - while (TCh::IsWs(Ch.GetCh())){} - if (Ch()=='+'){Minus=false; Ch.GetCh();} - if (Ch()=='-'){Minus=true; Ch.GetCh();} - if (Ch()=='0'){ - Ch.GetCh(); - if (tolower(Ch())=='x' ){ - Ch.GetCh(); if (Ch.Eof()){return false;} - } - } - if (!Ch.Eof() && !TCh::IsHex(Ch())){return false;} - if (!Ch.Eof()) _Val = TCh::GetHex(Ch()); - while (TCh::IsHex(Ch.GetCh())){_Val=16*_Val+TCh::GetHex(Ch());} - if (Minus){_Val=-_Val;} - if (Check&&((_ValMxVal))){return false;} - if (Ch.Eof()){Val=_Val; return true;} else {return false;} -} - -bool TStr::IsInt64( - const bool& Check, const int64& MnVal, const int64& MxVal, int64& Val) const { - // parsing format {ws} [+/-] +{ddd} - int64 _Val=0; - bool Minus=false; - TChRet Ch(TStrIn::New(*this)); - while (TCh::IsWs(Ch.GetCh())){} - if (Ch()=='+'){Minus=false; Ch.GetCh();} - if (Ch()=='-'){Minus=true; Ch.GetCh();} - if (!TCh::IsNum(Ch())){return false;} - _Val=TCh::GetNum(Ch()); - while (TCh::IsNum(Ch.GetCh())){_Val=10*_Val+TCh::GetNum(Ch());} - if (Minus){_Val=-_Val;} - if (Check&&((_ValMxVal))){return false;} - if (Ch.Eof()){Val=_Val; return true;} else {return false;} -} - -bool TStr::IsUInt64( - const bool& Check, const uint64& MnVal, const uint64& MxVal, uint64& Val) const { - // parsing format {ws} [+]{ddd} - uint64 _Val=0; - TChRet Ch(TStrIn::New(*this)); - while (TCh::IsWs(Ch.GetCh())){} - if (Ch()=='+'){Ch.GetCh();} - if (!TCh::IsNum(Ch())){return false;} - _Val=TCh::GetNum(Ch()); - while (TCh::IsNum(Ch.GetCh())){_Val=10*_Val+TCh::GetNum(Ch());} - if (Check&&((_ValMxVal))){return false;} - if (Ch.Eof()){Val=_Val; return true;} else {return false;} -} - -bool TStr::IsHexInt64( - const bool& Check, const int64& MnVal, const int64& MxVal, int64& Val) const { - // parsing format {ws} [+/-][0x] +{XXX} - int64 _Val=0; - bool Minus=false; - TChRet Ch(TStrIn::New(*this)); - while (TCh::IsWs(Ch.GetCh())){} - if (Ch()=='+'){Minus=false; Ch.GetCh();} - if (Ch()=='-'){Minus=true; Ch.GetCh();} - if (Ch()=='0'){ - Ch.GetCh(); - if (tolower(Ch())=='x' ){ - Ch.GetCh(); if (Ch.Eof()){return false;} - } - } - if (!Ch.Eof()) _Val=TCh::GetHex(Ch()); - while (TCh::IsHex(Ch.GetCh())){_Val=16*_Val+TCh::GetHex(Ch());} - if (Minus){_Val=-_Val;} - if (Check&&((_ValMxVal))){return false;} - if (Ch.Eof()){Val=_Val; return true;} else {return false;} -} - -bool TStr::IsFlt(const bool& Check, const double& MnVal, const double& MxVal, - double& Val, const char& DecDelimCh) const { - // parsing format {ws} [+/-] +{d} ([.]{d}) ([E|e] [+/-] +{d}) - TChRet Ch(TStrIn::New(*this)); - while (TCh::IsWs(Ch.GetCh())){} - if ((Ch()=='+')||(Ch()=='-')){Ch.GetCh();} - if (!TCh::IsNum(Ch())&&Ch()!=DecDelimCh){return false;} - while (TCh::IsNum(Ch.GetCh())){} - if (Ch()==DecDelimCh){ - Ch.GetCh(); - while (TCh::IsNum(Ch.GetCh())){} - } - if ((Ch()=='e')||(Ch()=='E')){ - Ch.GetCh(); - if ((Ch()=='+')||(Ch()=='-')){Ch.GetCh();} - if (!TCh::IsNum(Ch())){return false;} - while (TCh::IsNum(Ch.GetCh())){} - } - if (!Ch.Eof()){return false;} - double _Val=atof(CStr()); - if (Check&&((_ValMxVal))){ - return false; - } else { - Val=_Val; return true; - } -} - -bool TStr::IsWord(const bool& WsPrefixP, const bool& FirstUcAllowedP) const { - // parsing format {ws} (A-Z,a-z) *{A-Z,a-z,0-9} - TChRet Ch(TStrIn::New(*this)); - if (WsPrefixP){while (TCh::IsWs(Ch.GetCh())){}} - else {Ch.GetCh();} - if (!TCh::IsAlpha(Ch())){return false;} - else if (!FirstUcAllowedP&&(TCh::IsUc(Ch()))){return false;} - while (TCh::IsAlNum(Ch.GetCh())){} - if (!Ch.Eof()){return false;} - return true; -} - -bool TStr::IsWs() const { - // if string is just a bunch of whitespace chars - TChRet Ch(TStrIn::New(*this)); - while (TCh::IsWs(Ch.GetCh())){} - return Ch.Eof(); -} - -bool TStr::IsWcMatch( - const int& StrBChN, const TStr& WcStr, const int& WcStrBChN, TStrV& StarStrV, - const char& StarCh, const char& QuestCh) const { - int StrLen=Len(); int WcStrLen=WcStr.Len(); - int StrChN=StrBChN; int WcStrChN=WcStrBChN; - while ((StrChNStrChN){ - StarChA+=GetCh(AfterStrChN-1);} - if (IsWcMatch(AfterStrChN, WcStr, WcStrChN+1, StarStrV, StarCh, QuestCh)){ - StarStrV.Add(StarChA); return true; - } - } - return false; - } else { - return false; - } - } - if (StrChN==StrLen){ - for (int AfterWcStrChN=WcStrChN; AfterWcStrChNStarStrN){ - StarStr=StarStrV[StarStrV.Len()-StarStrN-1]; - } else { - StarStr=""; - } - return true; - } else { - return false; - } -} - -bool TStr::IsWcMatch(const TStr& WcStr) const { - TStrV StarStrV; - return IsWcMatch(0, WcStr, 0, StarStrV); -} - -TStr TStr::GetWcMatch(const TStr& WcStr, const int& StarStrN) const { - TStrV StarStrV; - if (IsWcMatch(WcStr, StarStrV)&&(StarStrV.Len()>=StarStrN)){ - IAssert(StarStrN>=0); - return StarStrV[StarStrV.Len()-StarStrN-1]; - } else { - return ""; - } -} - -TStr TStr::GetFPath() const { - int ThisLen=Len(); const char* ThisBf=CStr(); - int ChN=ThisLen-1; - while ((ChN>=0)&&(ThisBf[ChN]!='/')&&(ThisBf[ChN]!='\\')){ChN--;} - return GetSubStr(0, ChN); -} - -TStr TStr::GetFBase() const { - int ThisLen=Len(); const char* ThisBf=CStr(); - int ChN=ThisLen-1; - while ((ChN>=0)&&(ThisBf[ChN]!='/')&&(ThisBf[ChN]!='\\')){ChN--;} - return GetSubStr(ChN+1, ThisLen); -} - -TStr TStr::GetFMid() const { - int ThisLen=Len(); const char* ThisBf=CStr(); - int ChN=ThisLen-1; - while ((ChN>=0)&&(ThisBf[ChN]!='/')&&(ThisBf[ChN]!='\\')&&(ThisBf[ChN]!='.')){ - ChN--;} - if (ChN<0){ - return *this; - } else { - if (ThisBf[ChN]=='.'){ - int EChN= --ChN; - while ((ChN>=0)&&(ThisBf[ChN]!='/')&&(ThisBf[ChN]!='\\')){ChN--;} - return GetSubStr(ChN+1, EChN); - } else { - return GetSubStr(ChN+1, ThisLen); - } - } -} - -TStr TStr::GetFExt() const { - int ThisLen=Len(); const char* ThisBf=CStr(); - int ChN=ThisLen-1; - while ((ChN>=0)&&(ThisBf[ChN]!='/')&&(ThisBf[ChN]!='\\')&& - (ThisBf[ChN]!='.')){ChN--;} - if ((ChN>=0)&&(ThisBf[ChN]=='.')){return GetSubStr(ChN, Len());} - else {return TStr();} -} - -TStr TStr::GetNrFPath(const TStr& FPath){ - TChA NrFPath(FPath.Len()+4); NrFPath+=FPath; - NrFPath.ChangeCh('\\', '/'); - if (NrFPath.Empty()){NrFPath="./";} - if ((NrFPath.Len()>=2)&&isalpha(NrFPath[0])&&(NrFPath[1]==':')){ - if (NrFPath.Len()==2){NrFPath+="./";} - if ((NrFPath[2]!='.')&&(NrFPath[2]!='/')){NrFPath.Ins(2, "./");} - if (NrFPath[NrFPath.Len()-1]!='/'){NrFPath+="/";} - } else { - if ((NrFPath[0]!='.')&&(NrFPath[0]!='/')){NrFPath.Ins(0, "./");} - if (NrFPath[NrFPath.Len()-1]!='/'){NrFPath+="/";} - } - return NrFPath; -} - -TStr TStr::GetNrFMid(const TStr& FMid){ - TChA NrFMid; - int FMidLen=FMid.Len(); - for (int ChN=0; ChN=3)&&isalpha(FPath[0])&&(FPath[1]==':')&& - ((FPath[2]=='/')||(FPath[2]=='\\'))){ - return true; - } - return false; -} - -TStr TStr::PutFExt(const TStr& FNm, const TStr& FExt){ - return FNm.GetFPath()+FNm.GetFMid()+FExt; -} - -TStr TStr::PutFExtIfEmpty(const TStr& FNm, const TStr& FExt){ - if (FNm.GetFExt().Empty()){ - return FNm.GetFPath()+FNm.GetFMid()+FExt; - } else { - return FNm; - } -} - -TStr TStr::PutFBase(const TStr& FNm, const TStr& FBase){ - return FNm.GetFPath()+FBase; -} - -TStr TStr::PutFBaseIfEmpty(const TStr& FNm, const TStr& FBase){ - if (FNm.GetFBase().Empty()){ - return FNm.GetFPath()+FBase; - } else { - return FNm; - } -} - -TStr TStr::AddToFMid(const TStr& FNm, const TStr& ExtFMid){ - return FNm.GetFPath()+FNm.GetFMid()+ExtFMid+FNm.GetFExt(); -} - -TStr TStr::GetNumFNm(const TStr& FNm, const int& Num){ - return FNm.GetFPath()+FNm.GetFMid()+TInt::GetStr(Num, "%03d")+FNm.GetFExt(); -} - -TStr TStr::GetFNmStr(const TStr& Str, const bool& AlNumOnlyP){ - TChA FNm=Str; - for (int ChN=0; ChN= GrowBy && GrowBy > 0) newSize += GrowBy; - else if (newSize > 0) newSize *= 2; - else newSize = TInt::GetMn(GrowBy, 1024); - // check for overflow at 4GB - IAssertR(newSize >= MxBfL, TStr::Fmt("TStrPool::Resize: %u, %u [Size larger than 4Gb, which is not supported by TStrPool]", newSize, MxBfL).CStr()); - } - if (newSize > MxBfL) { - Bf = (char *) realloc(Bf, newSize); - IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr()); - MxBfL = newSize; - } - IAssertR(MxBfL >= _MxBfL, TStr::Fmt("new size: %u, requested size: %u", MxBfL, _MxBfL).CStr()); -} - -TStrPool::TStrPool(const uint& MxBfLen, const uint& _GrowBy) : MxBfL(MxBfLen), BfL(0), GrowBy(_GrowBy), Bf(0) { - //IAssert(MxBfL >= 0); IAssert(GrowBy >= 0); - if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssertR(Bf, TStr::Fmt("Can not resize buffer to %u bytes. [Program failed to allocate more memory. Solution: Get a bigger machine.]", MxBfL).CStr()); } - AddStr(""); // add an empty string at the beginning for fast future access -} - -TStrPool::TStrPool(TSIn& SIn, bool LoadCompact) : MxBfL(0), BfL(0), GrowBy(0), Bf(0) { - SIn.Load(MxBfL); SIn.Load(BfL); SIn.Load(GrowBy); - //IAssert(MxBfL >= BfL); IAssert(BfL >= 0); IAssert(GrowBy >= 0); - if (LoadCompact) MxBfL = BfL; - if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssertR(Bf, TStr::Fmt("Can not resize buffer to %u bytes. [Program failed to allocate more memory. Solution: Get a bigger machine.]", MxBfL).CStr()); } - if (BfL > 0) SIn.LoadBf(Bf, BfL); - SIn.LoadCs(); -} - -void TStrPool::Save(TSOut& SOut) const { - SOut.Save(MxBfL); SOut.Save(BfL); SOut.Save(GrowBy); - SOut.SaveBf(Bf, BfL); - SOut.SaveCs(); -} - -TStrPool& TStrPool::operator = (const TStrPool& Pool) { - if (this != &Pool) { - GrowBy = Pool.GrowBy; MxBfL = Pool.MxBfL; BfL = Pool.BfL; - if (Bf) free(Bf); else IAssertR(MxBfL == 0, TStr::Fmt("size: %u, expected size: 0", MxBfL).CStr()); - Bf = (char *) malloc(MxBfL); IAssertR(Bf, TStr::Fmt("Can not resize buffer to %u bytes. [Program failed to allocate more memory. Solution: Get a bigger machine.]", MxBfL).CStr()); memcpy(Bf, Pool.Bf, BfL); - } - return *this; -} - -// Adds Len characters to pool. To append a null -// terminated string Len must be equal to strlen(s) + 1 -uint TStrPool::AddStr(const char *Str, const uint& Len) { - IAssertR(Len > 0, "String too short (length includes the null character)"); //J: if (! Len) return -1; - if (Len == 1 && BfL > 0) { return 0; } // empty string - Assert(Str); Assert(Len > 0); - if (BfL + Len > MxBfL) Resize(BfL + Len); - memcpy(Bf + BfL, Str, Len); - uint Pos = BfL; BfL += Len; return Pos; -} - -int TStrPool::GetPrimHashCd(const char *CStr) { - return TStrHashF_DJB::GetPrimHashCd(CStr); -} - -int TStrPool::GetSecHashCd(const char *CStr) { - return TStrHashF_DJB::GetSecHashCd(CStr); -} - -///////////////////////////////////////////////// -// String-Pool-64bit -void TStrPool64::Resize(const ::TSize& _MxBfL) { - ::TSize newSize = MxBfL; - while (newSize < _MxBfL) { - if (newSize >= GrowBy && GrowBy > 0) newSize += GrowBy; - else if (newSize > 0) newSize *= 2; - else newSize = (GrowBy > ::TSize(1024)) ? ::TSize(1024) : GrowBy; - IAssert(newSize >= MxBfL); // assert we are growing - } - if (newSize > MxBfL) { - Bf = (char *) realloc(Bf, newSize); - IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr()); - MxBfL = newSize; - } - IAssert(MxBfL >= _MxBfL); -} - -TStrPool64::TStrPool64(::TSize _MxBfL, ::TSize _GrowBy): - MxBfL(_MxBfL), BfL(0), GrowBy(_GrowBy), Bf(NULL) { - - if (MxBfL > 0) { Bf = (char*)malloc(MxBfL); IAssert(Bf != NULL); } - AddStr(""); -} - -TStrPool64::TStrPool64(const TStrPool64& StrPool): - MxBfL(StrPool.MxBfL), BfL(StrPool.BfL), GrowBy(StrPool.GrowBy) { - if (Bf != NULL) { free(Bf); } else { IAssert(MxBfL == 0); } - Bf = (char*)malloc(StrPool.MxBfL); IAssert(Bf != NULL); - memcpy(Bf, StrPool.Bf, BfL); -} - -TStrPool64::TStrPool64(TSIn& SIn, bool LoadCompact): - MxBfL(0), BfL(0), GrowBy(0), Bf(0) { - uint64 _GrowBy, _MxBfL, _BfL; - SIn.Load(_GrowBy); SIn.Load(_MxBfL); SIn.Load(_BfL); - GrowBy = (::TSize)_GrowBy; MxBfL = (::TSize)_MxBfL; BfL = (::TSize)_BfL; - if (LoadCompact) { MxBfL = BfL; } - if (MxBfL > 0) { Bf = (char*)malloc(MxBfL); IAssert(Bf != NULL); } - for (::TSize BfN = 0; BfN < _BfL; BfN++) { Bf[BfN] = SIn.GetCh(); } - SIn.LoadCs(); -} - -void TStrPool64::Save(TSOut& SOut) const { - uint64 _GrowBy = GrowBy, _MxBfL = MxBfL, _BfL = BfL; - SOut.Save(_GrowBy); SOut.Save(_MxBfL); SOut.Save(_BfL); - for (::TSize BfN = 0; BfN < _BfL; BfN++) { SOut.PutCh(Bf[BfN]); } - SOut.SaveCs(); -} - -TStrPool64& TStrPool64::operator=(const TStrPool64& StrPool) { - if (this != &StrPool) { - GrowBy = StrPool.GrowBy; MxBfL = StrPool.MxBfL; BfL = StrPool.BfL; - if (Bf != NULL) { free(Bf); } else { IAssert(MxBfL == 0); } - Bf = (char*)malloc(MxBfL); IAssert(Bf != NULL); - memcpy(Bf, StrPool.Bf, BfL); - } - return *this; -} - -void TStrPool64::Clr(bool DoDel) { - BfL = 0; - if (DoDel && (Bf!=NULL)) { - free(Bf); - Bf = NULL; MxBfL = 0; - } -} - -uint64 TStrPool64::AddStr(const TStr& Str) { - const int Len = Str.Len() + 1; - if (BfL + Len > MxBfL) { Resize(BfL + Len); } - memcpy(Bf + BfL, Str.CStr(), Len); - ::TSize Offset = BfL; BfL += Len; - return uint64(Offset); -} - -TStr TStrPool64::GetStr(const uint64& StrId) const { - ::TSize Offset = (::TSize)StrId; - return TStr(Bf + Offset); -} - -///////////////////////////////////////////////// -// Void -void TVoid::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); -} - -void TVoid::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETag(Nm); -} - -///////////////////////////////////////////////// -// Boolean -const bool TBool::Mn=0; -const bool TBool::Mx=1; -const int TBool::Vals=TBool::Mx-TBool::Mn+1; -TRnd TBool::Rnd; - -const TStr TBool::FalseStr="F"; -const TStr TBool::TrueStr="T"; -const TStr TBool::NStr="N"; -const TStr TBool::YStr="Y"; -const TStr TBool::NoStr="No"; -const TStr TBool::YesStr="Yes"; - -void TBool::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=TXmlObjSer::GetBoolArg(XmlTok, "Val"); -} - -void TBool::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TBool::GetStr(Val)); -} - -bool TBool::IsValStr(const TStr& Str){ - TStr UcStr=Str.GetUc(); - return - (UcStr==FalseStr)||(UcStr==TrueStr)|| - (UcStr==YStr)||(UcStr==NStr)|| - (UcStr==YesStr)||(UcStr==NoStr); -} - -bool TBool::GetValFromStr(const TStr& Str){ - return (Str==TrueStr)||(Str==YStr)||(Str==YesStr); -} - -bool TBool::GetValFromStr(const TStr& Str, const bool& DfVal){ - TStr UcStr=Str.GetUc(); - if (IsValStr(UcStr)){ - return (UcStr==TrueStr)||(UcStr==YStr)||(UcStr==YesStr); - } else { - return DfVal; - } -} - -///////////////////////////////////////////////// -// Char -const char TCh::Mn=CHAR_MIN; -const char TCh::Mx=CHAR_MAX; -const int TCh::Vals=int(TCh::Mx)-int(TCh::Mn)+1; - -const char TCh::NullCh=char(0); -const char TCh::TabCh=char(9); -const char TCh::LfCh=char(10); -const char TCh::CrCh=char(13); -const char TCh::EofCh=char(26); -const char TCh::HashCh='#'; - -void TCh::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=char(TXmlObjSer::GetIntArg(XmlTok, "Val")); -} - -void TCh::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TInt::GetStr(Val)); -} - -char TCh::GetUsFromYuAscii(const char& Ch){ - switch (Ch){ - case '~': return 'c'; - case '^': return 'C'; - case '{': return 's'; - case '[': return 'S'; - case '`': return 'z'; - case '@': return 'Z'; - case '|': return 'd'; - case '\\': return 'D'; - default: return Ch; - } -} - -///////////////////////////////////////////////// -// Unsigned-Char -const uchar TUCh::Mn=0; -const uchar TUCh::Mx=UCHAR_MAX; -const int TUCh::Vals=int(TUCh::Mx)-int(TUCh::Mn)+1; - -void TUCh::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=uchar(TXmlObjSer::GetIntArg(XmlTok, "Val")); -} - -void TUCh::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TInt::GetStr(Val)); -} - -///////////////////////////////////////////////// -// Integer -const int TInt::Mn=INT_MIN; -const int TInt::Mx=INT_MAX; -const int TInt::Kilo=1024; -const int TInt::Mega=1024*1024; -const int TInt::Giga=1024*1024*1024; -TRnd TInt::Rnd; - -void TInt::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=TXmlObjSer::GetIntArg(XmlTok, "Val"); -} - -void TInt::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TInt::GetStr(Val)); -} - -TStr TInt::GetStr(const int& Val, const char* FmtStr){ - if (FmtStr==NULL){ - return GetStr(Val); - } else { - char Bf[255]; - sprintf(Bf, FmtStr, Val); - return TStr(Bf); - } -} - -//----------------------------------------------------------------------------- -// Frugal integer serialization -//----------------------------------------------------------------------------- -// These routines serialize integers to sequences of 1..4 bytes, with smaller -// integers receiving shorter codes. They do not work directly with streams -// but rather with a user-provided buffer. It is expected that one will want -// to store several such integers at once, and it would be inefficient to -// call the virtual TSOut::PutCh method once for every byte; this is why -// SaveFrugalInt and LoadFrugalInt work with a user-provided char* buffer -// rather than with TSIn/TSOut. To store a vector of such integers, use the -// SaveFrugalIntV/LoadFrugalIntV pair. - -char* TInt::SaveFrugalInt(char *pDest, int i){ - // <0xxx xxxx> has 128 combinations and is used to store -1..126. - // <1xxx xxxx> <00xx xxxx> has 2^13 = 8192 combinations and is used to store 127..8318. - // <1xxx xxxx> <01xx xxxx> has 2^13 = 8192 combinations and is used to store -2..-8193. - // <1xxx xxxx> <1xxx xxxx> <0xxx xxxx> has 2^29 = 536870912 combinations and is used to store 8319..536879230. - // <1xxx xxxx> <1xxx xxxx> <1xxx xxxx> has 2^29 = 536870912 combinations and is used to store -8194..-536879105. - i++; - if (i >= 0 && i <= 127) { *pDest++ = char(i); return pDest; } - if (i >= 128 && i < 128 + 8192) { i -= 128; *pDest++ = char(0x80 | (i & 0x7f)); - *pDest++ = char((i >> 7) & 0x3f); return pDest; } - if (i <= -1 && i > -1 - 8192) { i = -1 - i; *pDest++ = char(0x80 | (i & 0x7f)); - *pDest++ = char(0x40 | ((i >> 7) & 0x3f)); return pDest; } - if (i >= 128 + 8192 && i < 128 + 8192 + 536870912) { i -= 128 + 8192; - *pDest++ = char(0x80 | (i & 0x7f)); *pDest++ = char(0x80 | ((i >> 7) & 0x7f)); - *pDest++ = char((i >> 14) & 0xff); *pDest++ = char((i >> 22) & 0x7f); return pDest; } - if (i <= -1 - 8192 && i > -1 - 8192 - 536870912) { i = (-1 - 8192) - i; - *pDest++ = char(0x80 | (i & 0x7f)); *pDest++ = char(0x80 | ((i >> 7) & 0x7f)); - *pDest++ = char((i >> 14) & 0xff); *pDest++ = char(0x80 | ((i >> 22) & 0x7f)); return pDest; } - IAssertR(false, TInt::GetStr(i)); return 0; -} - -char* TInt::LoadFrugalInt(char *pSrc, int& i){ - i = 0; - int ch = (int) ((unsigned char) (*pSrc++)); - if ((ch & 0x80) == 0) { i = ch; i--; return pSrc; } - i = (ch & 0x7f); - ch = (int) ((unsigned char) (*pSrc++)); - if ((ch & 0x80) == 0) - { - i |= (ch & 0x3f) << 7; - if ((ch & 0x40) == 0) i += 128; else i = -1 - i; - i--; return pSrc; - } - i |= (ch & 0x7f) << 7; - ch = (int) ((unsigned char) (*pSrc++)); - i |= ch << 14; - ch = (int) ((unsigned char) (*pSrc++)); - i |= (ch & 0x7f) << 22; - if ((ch & 0x80) == 0) i += 128 + 8192; else i = (-1 - 8192) - i; - i--; return pSrc; -} - -// Tests the SaveFrugalInt/LoadFrugalInt combination on all the -// integers they can work with (about 10^9 integers). -void TInt::TestFrugalInt(){ - char buf[10], *p = &buf[0], *r, *s; - int i, j; -#define __TEST(from, to, len) \ - for (i = (from); i <= (to); i++) \ - { if ((i & 0xffff) == 0) printf("%d\r", i); \ - r = SaveFrugalInt(p, i); s = LoadFrugalInt(p, j); \ - IAssert(r == s); IAssert(i == j); IAssert(r - p == len); } - - __TEST(-1, 126, 1); - __TEST(127, 127 + 8191, 2); - __TEST(-2 - 8191, -2, 2); - __TEST(127 + 8192, 127 + 8191 + (1 << 29), 4); - __TEST(-2 - 8191 - (1 << 29), -2 - 8192, 4); -#undef __TEST -} - -// Suppose that the contents of 'v', encoded using SaveFrugalInt, -// occupy 'n' bytes. SaveFrugalIntV first stores 'n' (using -// SaveFrugalInt), then the contents. -void TInt::SaveFrugalIntV(TSOut& SOut, const TIntV& v){ - // Prepare a large enough buffer. - int count = v.Len(); - char *buf = new char[4 * (count + 1)], *pStart, *pEnd; - // Encode the contents of 'v'. - pStart = buf + 4; pEnd = pStart; - for (int i = 0; i < count; i++) - pEnd = SaveFrugalInt(pEnd, v[i].Val); - // Encode the size of the encoded contents of 'v'. - // This is stored at the beginning of 'buf' and is then - // moved so that there is no gap between it and the - // beginning of the stored contents (at pStart). - int size = int(pEnd - pStart); - char *pSizeStart = buf; - char *pSizeEnd = SaveFrugalInt(pSizeStart, size); - while (pSizeEnd > pSizeStart) *(--pStart) = *(--pSizeEnd); - // Write the buffer and free the memory. - SOut.PutBf(pStart, TSize(pEnd - pStart)); - delete[] buf; -} - -// Loads an integer 'n' (using LoadFrugalInt), then loads -// 'n' bytes, decoding them using LoadFrugalInt and adding -// them to the vector 'v'. If clearVec is true, 'v' is -// cleared before anything is added to it. -void TInt::LoadFrugalIntV(TSIn& SIn, TIntV& v, bool clearVec){ - if (clearVec) v.Clr(); - char sizeBuf[4], *p, *pEnd; - // Load the first frugally-stored integer into the sizeBuf - // buffer. 'count' bytes will be read. - sizeBuf[0] = SIn.GetCh(); int count = 1; - if (sizeBuf[0] & 0x80) - { - sizeBuf[1] = SIn.GetCh(); count++; - if (sizeBuf[1] & 0x80) { sizeBuf[2] = SIn.GetCh(); - sizeBuf[3] = SIn.GetCh(); count += 2;} - } - // Decode the stored size. - int size; - pEnd = LoadFrugalInt(&sizeBuf[0], size); - IAssert(pEnd - &sizeBuf[0] == count); - if (size <= 0) return; - // Allocate a buffer and read the compressed data. - char *buf = new char[size]; - SIn.GetBf(buf, size); - // Decode the compressed integers and add them into 'v'. - p = buf; pEnd = buf + size; - while (p < pEnd) - { int i; p = LoadFrugalInt(p, i); v.Add(i); } - IAssert(p == pEnd); - delete[] buf; -} - -///////////////////////////////////////////////// -// Unsigned-Integer -const uint TUInt::Mn=0; -const uint TUInt::Mx=UINT_MAX; -TRnd TUInt::Rnd; - -void TUInt::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=TXmlObjSer::GetIntArg(XmlTok, "Val"); -} - -void TUInt::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TInt::GetStr(Val)); -} - -TStr TUInt::GetStr(const uint& Val, const char* FmtStr){ - if (FmtStr==NULL){ - return GetStr(Val); - } else { - char Bf[255]; - sprintf(Bf, FmtStr, Val); - return TStr(Bf); - } -} - -bool TUInt::IsIpStr(const TStr& IpStr, uint& Ip, const char& SplitCh) { - TStrV IpStrV; IpStr.SplitOnAllCh(SplitCh, IpStrV); - Ip = 0; int Byte = 0; - if (IpStrV.Len() != 4) { return false; } - if (!IpStrV[0].IsInt(true, 0, 255, Byte)) { return false; }; Ip = (uint)Byte; - if (!IpStrV[1].IsInt(true, 0, 255, Byte)) { return false; }; Ip = (Ip << 8) | (uint)Byte; - if (!IpStrV[2].IsInt(true, 0, 255, Byte)) { return false; }; Ip = (Ip << 8) | (uint)Byte; - if (!IpStrV[3].IsInt(true, 0, 255, Byte)) { return false; }; Ip = (Ip << 8) | (uint)Byte; - return true; -} - -uint TUInt::GetUIntFromIpStr(const TStr& IpStr, const char& SplitCh) { - TStrV IpStrV; IpStr.SplitOnAllCh(SplitCh, IpStrV); - uint Ip = 0; int Byte = 0; - EAssertR(IpStrV[0].IsInt(true, 0, 255, Byte), TStr::Fmt("Bad IP: '%s;", IpStr.CStr())); Ip = (uint)Byte; - EAssertR(IpStrV[1].IsInt(true, 0, 255, Byte), TStr::Fmt("Bad IP: '%s;", IpStr.CStr())); Ip = (Ip << 8) | (uint)Byte; - EAssertR(IpStrV[2].IsInt(true, 0, 255, Byte), TStr::Fmt("Bad IP: '%s;", IpStr.CStr())); Ip = (Ip << 8) | (uint)Byte; - EAssertR(IpStrV[3].IsInt(true, 0, 255, Byte), TStr::Fmt("Bad IP: '%s;", IpStr.CStr())); Ip = (Ip << 8) | (uint)Byte; - return Ip; -} - -TStr TUInt::GetStrFromIpUInt(const uint& Ip) { - return TStr::Fmt("%d.%d.%d.%d", ((Ip>>24) & 0xFF), - ((Ip>>16) & 0xFF), ((Ip>>8) & 0xFF), (Ip & 0xFF)); -} - -bool TUInt::IsIpv6Str(const TStr& IpStr, const char& SplitCh) { - TStrV IpStrV; IpStr.SplitOnAllCh(SplitCh, IpStrV, false); - // check we have 8 groups - if (IpStrV.Len() > 8) { return false; } - // each group must be in hexa and in range from 0x0000 to 0xFFFF - int Group = 0; - for (int IpStrN = 0; IpStrN < IpStrV.Len(); IpStrN++) { - if (IpStrV[IpStrN].Empty()) { continue; } - if (IpStrV[IpStrN].IsHexInt(true, 0x0000, 0xFFFF, Group)) { continue; } - return false; - } - // all fine - return true; -} - -///////////////////////////////////////////////// -// Unsigned-Integer-64Bit - -#if defined (GLib_WIN32) -const TUInt64 TUInt64::Mn(uint64(0x0000000000000000i64)); -const TUInt64 TUInt64::Mx(uint64(0xFFFFFFFFFFFFFFFFi64)); -#elif defined (GLib_BCB) -const TUInt64 TUInt64::Mn(0x0000000000000000i64); -const TUInt64 TUInt64::Mx(0xFFFFFFFFFFFFFFFFi64); -#else -const TUInt64 TUInt64::Mn((uint64)0x0000000000000000LL); -const TUInt64 TUInt64::Mx(0xFFFFFFFFFFFFFFFFLL); -#endif - -void TUInt64::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=TXmlObjSer::GetInt64Arg(XmlTok, "Val"); -} - -void TUInt64::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TUInt64::GetStr(Val)); -} - -/*///////////////////////////////////////////////// -// Unsigned-Integer-64Bit -const TUInt64 TUInt64::Mn(0, 0); -const TUInt64 TUInt64::Mx(UINT_MAX, UINT_MAX); - -void TUInt64::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); XLoad(TInt(MsVal)); XLoad(TInt(LsVal)); -} - -void TUInt64::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveHd(Nm); XSave(TInt(MsVal)); XSave(TInt(LsVal)); -} - -TUInt64& TUInt64::operator+=(const TUInt64& Int){ - uint CarryVal=(LsVal>TUInt::Mx-Int.LsVal) ? 1 : 0; - LsVal+=Int.LsVal; - MsVal+=Int.MsVal+CarryVal; - return *this; -} - -TUInt64& TUInt64::operator-=(const TUInt64& Int){ - IAssert(*this>=Int); - uint CarryVal; - if (LsVal>=Int.LsVal){ - LsVal-=Int.LsVal; CarryVal=0; - } else { - LsVal+=(TUInt::Mx-Int.LsVal)+1; CarryVal=1; - } - MsVal-=(Int.MsVal+CarryVal); - return *this; -} - -TUInt64 TUInt64::operator++(int){ - if (LsVal==TUInt::Mx){ - Assert(MsVal0); MsVal--; LsVal=TUInt::Mx;} - else {LsVal--;} - return *this; -} - -TStr TUInt64::GetStr(const TUInt64& Int){ - char Bf[255]; sprintf(Bf, "%.0Lf", ldouble(Int)); - return TStr(Bf); -} - -TStr TUInt64::GetHexStr(const TUInt64& Int){ - char Bf[255]; sprintf(Bf, "%08X%08X", Int.MsVal, Int.LsVal); - return TStr(Bf); -}*/ - -///////////////////////////////////////////////// -// Float -const double TFlt::Mn=-DBL_MAX; -const double TFlt::Mx=+DBL_MAX; -const double TFlt::NInf=-DBL_MAX; -const double TFlt::PInf=+DBL_MAX; -const double TFlt::Eps=1e-16; -const double TFlt::EpsHalf =1e-7; - -TRnd TFlt::Rnd; - -void TFlt::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=TXmlObjSer::GetFltArg(XmlTok, "Val"); -} - -void TFlt::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TFlt::GetStr(Val)); -} - -TStr TFlt::GetStr(const double& Val, const int& Width, const int& Prec){ - char Bf[255]; - if ((Width==-1)&&(Prec==-1)){sprintf(Bf, "%g", Val);} - else {sprintf(Bf, "%*.*f", Width, Prec, Val);} - return TStr(Bf); -} - -TStr TFlt::GetStr(const double& Val, const char* FmtStr){ - if (FmtStr==NULL){ - return GetStr(Val); - } else { - char Bf[255]; - sprintf(Bf, FmtStr, Val); - return TStr(Bf); - } -} - -///////////////////////////////////////////////// -// Short-Float -const sdouble TSFlt::Mn=-FLT_MIN; -const sdouble TSFlt::Mx=+FLT_MAX; - -void TSFlt::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=sdouble(TXmlObjSer::GetFltArg(XmlTok, "Val")); -} - -void TSFlt::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TFlt::GetStr(Val)); -} - -///////////////////////////////////////////////// -// Long-Float -const ldouble TLFlt::Mn=-LDBL_MAX; -const ldouble TLFlt::Mx=+LDBL_MAX; -/*const ldouble TUInt64::Mn_LFlt=TUInt64::Mn; -const ldouble TUInt64::Mx_LFlt=TUInt64::Mx; -const ldouble TUInt64::MxP1_LFlt=ldouble(TUInt::Mx)+ldouble(1);*/ - -void TLFlt::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - Val=TXmlObjSer::GetFltArg(XmlTok, "Val"); -} - -void TLFlt::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg(Nm, "Val", TFlt::GetStr(double(Val))); -} - -TStr TLFlt::GetStr(const ldouble& Val, const int& Width, const int& Prec){ - char Bf[255]; - if ((Width==-1)&&(Prec==-1)){sprintf(Bf, "%Lg", Val);} - else {sprintf(Bf, "%*.*Lf", Width, Prec, Val);} - return TStr(Bf); -} - -TStr TLFlt::GetStr(const ldouble& Val, const char* FmtStr){ - if (FmtStr==NULL){ - return GetStr(Val); - } else { - char Bf[255]; - sprintf(Bf, FmtStr, Val); - return TStr(Bf); - } -} - -///////////////////////////////////////////////// -// Float-Rectangle -void TFltRect::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){ - XLoadHd(Nm); - MnX=TXmlObjSer::GetFltArg(XmlTok, "MnX"); - MnY=TXmlObjSer::GetFltArg(XmlTok, "MnY"); - MxX=TXmlObjSer::GetFltArg(XmlTok, "MxX"); - MxY=TXmlObjSer::GetFltArg(XmlTok, "MxY"); -} - -void TFltRect::SaveXml(TSOut& SOut, const TStr& Nm) const { - XSaveBETagArg4(Nm, - "MnX", TFlt::GetStr(double(MnX)), "MnY", TFlt::GetStr(double(MnY)), - "MxX", TFlt::GetStr(double(MxX)), "MxY", TFlt::GetStr(double(MxY))); -} - -bool TFltRect::Intersection(const TFltRect& Rect1, const TFltRect& Rect2){ - const double MnXX = TFlt::GetMx(Rect1.GetMnX(), Rect2.GetMnX()); - const double MnYY = TFlt::GetMx(Rect1.GetMnY(), Rect2.GetMnY()); - const double MxXX = TFlt::GetMn(Rect1.GetMxX(), Rect2.GetMxX()); - const double MxYY = TFlt::GetMn(Rect1.GetMxY(), Rect2.GetMxY()); - return (MnXX < MxXX) && (MnYY < MxYY); -} - -TStr TFltRect::GetStr() const { - TChA ChA; - ChA+='('; - ChA+=TFlt::GetStr(MnX, "%0.2f"); ChA+=','; - ChA+=TFlt::GetStr(MnY, "%0.2f"); ChA+=','; - ChA+=TFlt::GetStr(MxX, "%0.2f"); ChA+=','; - ChA+=TFlt::GetStr(MxY, "%0.2f"); ChA+=')'; - return ChA; -} diff --git a/inst/include/glib-core/dt.h b/inst/include/glib-core/dt.h deleted file mode 100644 index e9a53fb..0000000 --- a/inst/include/glib-core/dt.h +++ /dev/null @@ -1,1617 +0,0 @@ -#include "bd.h" - -///////////////////////////////////////////////// -// Forward -class TILx; -class TOLx; -ClassHdTP(TXmlTok, PXmlTok); - -///////////////////////////////////////////////// -// Random -class TRnd{ -public: - static const int RndSeed; -private: - static const int a, m, q, r; - int Seed; - int GetNextSeed(){ - if ((Seed=a*(Seed%q)-r*(Seed/q))>0){return Seed;} else {return Seed+=m;}} -public: - TRnd(const int& _Seed=1, const int& Steps=0){ - PutSeed(_Seed); Move(Steps);} - explicit TRnd(TSIn& SIn){SIn.Load(Seed);} - void Save(TSOut& SOut) const {SOut.Save(Seed);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TRnd& operator=(const TRnd& Rnd){Seed=Rnd.Seed; return *this;} - bool operator==(const TRnd&) const {Fail; return false;} - - double GetUniDev(){return GetNextSeed()/double(m);} - int GetUniDevInt(const int& Range=0); - int GetUniDevInt(const int& MnVal, const int& MxVal){ - IAssert(MnVal<=MxVal); return MnVal+GetUniDevInt(MxVal-MnVal+1);} - uint GetUniDevUInt(const uint& Range=0); - int64 GetUniDevInt64(const int64& Range=0); - uint64 GetUniDevUInt64(const uint64& Range=0); - double GetNrmDev(); - double GetNrmDev( - const double& Mean, const double& SDev, const double& Mn, const double& Mx); - double GetExpDev(); - double GetExpDev(const double& Lambda); // mean=1/lambda - double GetGammaDev(const int& Order); - double GetPoissonDev(const double& Mean); - double GetBinomialDev(const double& Prb, const int& Trials); - int GetGeoDev(const double& Prb){ - return 1+(int)floor(log(1.0-GetUniDev())/log(1.0-Prb));} - double GetPowerDev(const double& AlphaSlope){ // power-law degree distribution (AlphaSlope>0) - IAssert(AlphaSlope>1.0); - return pow(1.0-GetUniDev(), -1.0/(AlphaSlope-1.0));} - double GetRayleigh(const double& Sigma) { // 1/sqrt(alpha) = sigma - IAssert(Sigma>0.0); - return Sigma*sqrt(-2*log(1-GetUniDev()));} - double GetWeibull(const double& K, const double& Lambda) { // 1/alpha = lambda - IAssert(Lambda>0.0 && K>0.0); - return Lambda*pow(-log(1-GetUniDev()), 1.0/K);} - //void GetSphereDev(const int& Dim, TFltV& ValV); - - void PutSeed(const int& _Seed); - int GetSeed() const {return Seed;} - void Randomize(){PutSeed(RndSeed);} - void Move(const int& Steps); - bool Check(); - - static double GetUniDevStep(const int& Seed, const int& Steps){ - TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetUniDev();} - static double GetNrmDevStep(const int& Seed, const int& Steps){ - TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetNrmDev();} - static double GetExpDevStep(const int& Seed, const int& Steps){ - TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetExpDev();} - - static TRnd LoadTxt(TILx& Lx); - void SaveTxt(TOLx& Lx) const; -}; - -///////////////////////////////////////////////// -// Memory -ClassTP(TMem, PMem)//{ -private: - int MxBfL, BfL; - char* Bf; - void Resize(const int& _MxBfL); - bool DoFitLen(const int& LBfL) const {return BfL+LBfL<=MxBfL;} -public: - TMem(const int& _MxBfL=0): - MxBfL(_MxBfL), BfL(0), Bf(NULL){ IAssert(BfL>=0); - if (MxBfL>0){Bf=new char[MxBfL]; IAssert(Bf!=NULL);}} - static PMem New(const int& MxBfL=0){return new TMem(MxBfL);} - TMem(const void* _Bf, const int& _BfL): - MxBfL(_BfL), BfL(_BfL), Bf(NULL){ IAssert(BfL>=0); - if (BfL>0){Bf=new char[BfL]; IAssert(Bf!=NULL); memcpy(Bf, _Bf, BfL);}} - static PMem New(const void* Bf, const int& BfL){return new TMem(Bf, BfL);} - TMem(const TMem& Mem): - MxBfL(Mem.MxBfL), BfL(Mem.BfL), Bf(NULL){ - if (MxBfL>0){Bf=new char[MxBfL]; memcpy(Bf, Mem.Bf, BfL);}} - static PMem New(const TMem& Mem){return new TMem(Mem);} - static PMem New(const PMem& Mem){return new TMem(*Mem);} - TMem(const TStr& Str); - static PMem New(const TStr& Str){return new TMem(Str);} - ~TMem(){if (Bf!=NULL){delete[] Bf;}} - explicit TMem(TSIn& SIn){ - SIn.Load(MxBfL); SIn.Load(BfL); - Bf=new char[MxBfL=BfL]; SIn.LoadBf(Bf, BfL);} - void Save(TSOut& SOut) const { - SOut.Save(MxBfL); SOut.Save(BfL); SOut.SaveBf(Bf, BfL);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TMem& operator=(const TMem& Mem){ - if (this!=&Mem){ - if (Bf!=NULL){delete[] Bf;} - MxBfL=Mem.MxBfL; BfL=Mem.BfL; Bf=NULL; - if (MxBfL>0){Bf=new char[MxBfL]; memcpy(Bf, Mem.Bf, BfL);}} - return *this;} - char* operator()() const {return Bf;} - TMem& operator+=(const char& Ch); - TMem& operator+=(const TMem& Mem); - TMem& operator+=(const TStr& Str); - TMem& operator+=(const PSIn& SIn); - char& operator[](const int& ChN) const { - Assert((0<=ChN)&&(ChN 0) memset(Bf, 0, BfL);} - void Reserve(const int& _MxBfL, const bool& DoClr = true){ - if (DoClr){ Clr(); } Resize(_MxBfL);} - void Del(const int& BChN, const int& EChN); - void Clr(const bool& DoDel=true){ - if (DoDel){if (Bf!=NULL){delete[] Bf;} MxBfL=0; BfL=0; Bf=NULL;} - else {BfL=0;}} - int Len() const {return BfL;} - bool Empty() const {return BfL==0;} - void Trunc(const int& _BfL){ - if ((0<=_BfL)&&(_BfL<=BfL)){BfL=_BfL;}} - void Push(const char& Ch){operator+=(Ch);} - char Pop(){IAssert(BfL>0); BfL--; return Bf[BfL];} - - bool DoFitStr(const TStr& Str) const; - //int AddStr(const TStr& Str); - void AddBf(const void* Bf, const int& BfL); - char* GetBf() const {return Bf;} - TStr GetAsStr(const char& NewNullCh='\0') const; - PSIn GetSIn() const { - TMOut MOut(BfL); MOut.SaveBf(Bf, BfL); return MOut.GetSIn();} - - static void LoadMem(const PSIn& SIn, TMem& Mem){ - Mem.Clr(); Mem.Gen(SIn->Len()); SIn->GetBf(Mem.Bf, SIn->Len());} - static void LoadMem(const PSIn& SIn, const PMem& Mem){ - Mem->Clr(); Mem->Gen(SIn->Len()); SIn->GetBf(Mem->Bf, SIn->Len());} - void SaveMem(const PSOut& SOut) const {SOut->SaveBf(Bf, Len());} -}; - -///////////////////////////////////////////////// -// Input-Memory -class TMemIn: public TSIn{ -private: - PMem Mem; - const char* Bf; - int BfC, BfL; -public: - TMemIn(const TMem& _Mem, const int& _BfC=0); - static PSIn New(const TMem& Mem){ - return PSIn(new TMemIn(Mem));} - static PSIn New(const PMem& Mem){ - TMemIn* MemIn=new TMemIn(*Mem); MemIn->Mem=Mem; return PSIn(MemIn);} - ~TMemIn(){} - - bool Eof(){return BfC==BfL;} - int Len() const {return BfL-BfC;} - char GetCh(){Assert(BfCoperator+=(Ch); return Ch;} - int PutBf(const void* LBf, const TSize& LBfL); - void Flush(){} -}; - -///////////////////////////////////////////////// -// Char-Array -class TChA{ -private: - int MxBfL, BfL; - char* Bf; - void Resize(const int& _MxBfL); -public: - explicit TChA(const int& _MxBfL=256){ - Bf=new char[(MxBfL=_MxBfL)+1]; Bf[BfL=0]=0;} - TChA(const char* CStr){ - Bf=new char[(MxBfL=BfL=int(strlen(CStr)))+1]; strcpy(Bf, CStr);} - TChA(const char* CStr, const int& StrLen) : MxBfL(StrLen), BfL(StrLen) { - Bf=new char[StrLen+1]; strncpy(Bf, CStr, StrLen); Bf[StrLen]=0;} - TChA(const TChA& ChA){ - Bf=new char[(MxBfL=ChA.MxBfL)+1]; BfL=ChA.BfL; strcpy(Bf, ChA.CStr());} - TChA(const TStr& Str); - TChA(const TMem& Mem){ - Bf=new char[(MxBfL=BfL=Mem.Len())+1]; Bf[MxBfL]=0; - memcpy(CStr(), Mem(), Mem.Len());} - ~TChA(){delete[] Bf;} - explicit TChA(TSIn& SIn){ - SIn.Load(MxBfL); SIn.Load(BfL); SIn.Load(Bf, MxBfL, BfL);} - void Load(TSIn& SIn){ delete[] Bf; - SIn.Load(MxBfL); SIn.Load(BfL); SIn.Load(Bf, MxBfL, BfL);} - void Save(TSOut& SOut, const bool& SaveCompact=true) const { //J: - SOut.Save(SaveCompact?BfL:MxBfL); SOut.Save(BfL); SOut.Save(Bf, BfL);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TChA& operator=(const TChA& ChA); - TChA& operator=(const TStr& Str); - TChA& operator=(const char* CStr); - bool operator==(const TChA& ChA) const {return strcmp(CStr(), ChA.CStr())==0;} - bool operator==(const char* _CStr) const {return strcmp(CStr(), _CStr)==0;} - bool operator==(const char& Ch) const {return (BfL==1)&&(Bf[0]==Ch);} - bool operator!=(const TChA& ChA) const {return strcmp(CStr(), ChA.CStr())!=0;} - bool operator!=(const char* _CStr) const {return strcmp(CStr(), _CStr)!=0;} - bool operator!=(const char& Ch) const {return !((BfL==1)&&(Bf[0]==Ch));} - bool operator<(const TChA& ChA) const {return strcmp(CStr(), ChA.CStr())<0;} - - TChA& operator+=(const TMem& Mem); - TChA& operator+=(const TChA& ChA); - TChA& operator+=(const TStr& Str); - TChA& operator+=(const char* CStr); - TChA& operator+=(const char& Ch){ - if (BfL==MxBfL){Resize(BfL+1);} - Bf[BfL]=Ch; BfL++; Bf[BfL]=0; return *this;} - char operator[](const int& ChN) const { - Assert((0<=ChN)&&(ChN0); BfL--; char Ch=Bf[BfL]; Bf[BfL]=0; return Ch;} - void Trunc(); - void Trunc(const int& _BfL){ - if ((0<=_BfL)&&(_BfL<=BfL)){Bf[BfL=_BfL]=0;}} - void Reverse(); - - void AddCh(const char& Ch, const int& MxLen=-1){ - if ((MxLen==-1)||(BfLMxBfL){Resize(BfL+BfS+1);} - strncpy(Bf+BfL,NewBf,BfS); BfL+=BfS; Bf[BfL]=0;} - void PutCh(const int& ChN, const char& Ch){ - Assert((0<=ChN)&&(ChN=0); Refs=0; Bf=new char[Len+1]; Bf[Len]=0;} - TRStr(const char* CStr){ - Refs=0; Bf=new char[strlen(CStr)+1]; strcpy(Bf, CStr);} - TRStr(const char* CStr, const int& MxLen){ - Refs=0; Bf=new char[MxLen+1]; strncpy(Bf, CStr, MxLen); Bf[MxLen]=0;} - TRStr(const char* CStr1, const char* CStr2){ - Refs=0; int CStr1Len=int(strlen(CStr1)); Bf=new char[CStr1Len+int(strlen(CStr2))+1]; - strcpy(Bf, CStr1); strcpy(Bf+CStr1Len, CStr2);} - TRStr(const char& Ch){ - Refs=0; Bf=new char[1+1]; Bf[0]=Ch; Bf[1]=0;} - TRStr(const char& Ch1, const char& Ch2){ - Refs=0; Bf=new char[2+1]; Bf[0]=Ch1; Bf[1]=Ch2; Bf[2]=0;} - ~TRStr(){ - Assert(((this!=GetNullRStr())&&(Refs==0))||((this==GetNullRStr())&&(Refs==1))); - delete[] Bf;} - explicit TRStr(TSIn& SIn, const bool& IsSmall){ - if (IsSmall){Refs=0; SIn.Load(Bf);} - else {Refs=0; int BfL; SIn.Load(BfL); SIn.Load(Bf, BfL, BfL);}} - void Save(TSOut& SOut, const bool& IsSmall) const { - if (IsSmall){SOut.Save(Bf);} - else {int BfL=int(strlen(Bf)); SOut.Save(BfL); SOut.Save(Bf, BfL);}} - - TRStr& operator=(const TRStr&){Fail; return *this;} - int GetMemUsed() const {return int(sizeof(int))+int(strlen(Bf));} - - void MkRef(){Refs++;} - void UnRef(){Assert(Refs>0); if (--Refs==0){delete this;}} - - const char* CStr() const {return Bf;} - char* CStr() {return Bf;} - bool Empty() const {return Bf[0]==0;} - int Len() const {return int(strlen(Bf));} - - void PutCh(const int& ChN, const char& Ch){ - Assert((0<=ChN)&&(ChN class TVec; -typedef TVec TStrV; - -class TStr{ -private: - TRStr* RStr; -private: - TStr(const char& Ch, bool){ - RStr=new TRStr(Ch); RStr->MkRef();} - TStr(const char& Ch1, const char& Ch2, bool){ - RStr=new TRStr(Ch1, Ch2); RStr->MkRef();} - static TRStr* GetRStr(const char* CStr); - void Optimize(); -public: - TStr(){RStr=TRStr::GetNullRStr(); RStr->MkRef();} - TStr(const TStr& Str){RStr=Str.RStr; RStr->MkRef();} - TStr(const TChA& ChA){RStr=GetRStr(ChA.CStr()); RStr->MkRef();} - TStr(const TSStr& SStr){RStr=GetRStr(SStr.CStr()); RStr->MkRef();} - TStr(const char* CStr){RStr=GetRStr(CStr); RStr->MkRef();} - explicit TStr(const char& Ch){RStr=GetChStr(Ch).RStr; RStr->MkRef();} - TStr(const TMem& Mem){ - RStr=new TRStr(Mem.Len()); RStr->MkRef(); - memcpy(CStr(), Mem(), Mem.Len()); Optimize();} - explicit TStr(const PSIn& SIn){ - int SInLen=SIn->Len(); RStr=new TRStr(SInLen); RStr->MkRef(); - SIn->GetBf(CStr(), SInLen); Optimize();} - ~TStr(){RStr->UnRef();} - explicit TStr(TSIn& SIn, const bool& IsSmall=false): - RStr(new TRStr(SIn, IsSmall)){RStr->MkRef(); Optimize();} - void Load(TSIn& SIn, const bool& IsSmall=false){ - *this=TStr(SIn, IsSmall);} - void Save(TSOut& SOut, const bool& IsSmall=false) const { - RStr->Save(SOut, IsSmall);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TStr& operator=(const TStr& Str){ - if (this!=&Str){RStr->UnRef(); RStr=Str.RStr; RStr->MkRef();} return *this;} - TStr& operator=(const TChA& ChA){ - RStr->UnRef(); RStr=GetRStr(ChA.CStr()); RStr->MkRef(); return *this;} - TStr& operator=(const char* CStr){ - RStr->UnRef(); RStr=GetRStr(CStr); RStr->MkRef(); return *this;} - TStr& operator=(const char& Ch){ - RStr->UnRef(); RStr=GetChStr(Ch).RStr; RStr->MkRef(); return *this;} - TStr& operator+=(const TStr& Str){ - TRStr* NewRStr=new TRStr(RStr->CStr(), Str.RStr->CStr()); - RStr->UnRef(); RStr=NewRStr; RStr->MkRef(); - Optimize(); return *this;} - TStr& operator+=(const char* CStr){ - TRStr* NewRStr=new TRStr(RStr->CStr(), CStr); - RStr->UnRef(); RStr=NewRStr; RStr->MkRef(); - Optimize(); return *this;} - bool operator==(const TStr& Str) const { - return (RStr==Str.RStr)||(strcmp(RStr->CStr(), Str.RStr->CStr())==0);} - bool operator==(const char* CStr) const { - return strcmp(RStr->CStr(), CStr)==0;} -// bool operator!=(const TStr& Str) const { -// return strcmp(RStr->CStr(), Str.RStr->CStr())!=0;} - bool operator!=(const char* CStr) const { - return strcmp(RStr->CStr(), CStr)!=0;} - bool operator<(const TStr& Str) const { - return strcmp(RStr->CStr(), Str.RStr->CStr())<0;} - char operator[](const int& ChN) const {return RStr->GetCh(ChN);} - int GetMemUsed() const {return int(sizeof(TRStr*)+RStr->GetMemUsed());} - - char* operator()(){return RStr->CStr();} - const char* operator()() const {return RStr->CStr();} - char* CStr() {return RStr->CStr();} - const char* CStr() const {return RStr->CStr();} - - void PutCh(const int& ChN, const char& Ch){ - TRStr* NewRStr=new TRStr(RStr->CStr()); - RStr->UnRef(); RStr=NewRStr; RStr->MkRef(); - RStr->PutCh(ChN, Ch); Optimize();} - char GetCh(const int& ChN) const {return RStr->GetCh(ChN);} - char LastCh() const {return GetCh(Len()-1);} - - void Clr(){RStr->UnRef(); RStr=TRStr::GetNullRStr(); RStr->MkRef();} - int Len() const {return RStr->Len();} - bool Empty() const {return RStr->Empty();} - - // upper-case - bool IsUc() const {return RStr->IsUc();} - TStr& ToUc(); - TStr GetUc() const {return TStr(*this).ToUc();} - int CmpI(const TStr& Str) const {return TRStr::CmpI(CStr(), Str.CStr());} - bool EqI(const TStr& Str) const {return TRStr::CmpI(CStr(), Str.CStr())==0;} - // lower-case - bool IsLc() const {return RStr->IsLc();} - TStr& ToLc(); - TStr GetLc() const {return TStr(*this).ToLc();} - // capitalize - TStr& ToCap(); - TStr GetCap() const {return TStr(*this).ToCap();} - - // truncate - TStr& ToTrunc(); - TStr GetTrunc() const {return TStr(*this).ToTrunc();} - // Yu-Ascii to Us-Ascii - TStr& ConvUsFromYuAscii(); - TStr GetUsFromYuAscii() const {return TStr(*this).ConvUsFromYuAscii();} - // hex - TStr& ToHex(); - TStr GetHex() const {return TStr(*this).ToHex();} - TStr& FromHex(); - TStr GetFromHex() const {return TStr(*this).FromHex();} - - TStr GetSubStr(const int& BChN, const int& EChN) const; - TStr GetSubStr(const int& BChN) const { return GetSubStr(BChN, Len()-1); } - void InsStr(const int& BChN, const TStr& Str); - void DelChAll(const char& Ch); - void DelSubStr(const int& BChN, const int& EChN); - bool DelStr(const TStr& Str); - TStr LeftOf(const char& SplitCh) const; - TStr LeftOfLast(const char& SplitCh) const; - TStr RightOf(const char& SplitCh) const; - TStr RightOfLast(const char& SplitCh) const; - void SplitOnCh(TStr& LStr, const char& SplitCh, TStr& RStr) const; - void SplitOnLastCh(TStr& LStr, const char& SplitCh, TStr& RStr) const; - void SplitOnAllCh( - const char& SplitCh, TStrV& StrV, const bool& SkipEmpty=true) const; - void SplitOnAllAnyCh( - const TStr& SplitChStr, TStrV& StrV, const bool& SkipEmpty=true) const; - void SplitOnWs(TStrV& StrV) const; - void SplitOnNonAlNum(TStrV& StrV) const; - void SplitOnStr(const TStr& SplitStr, TStrV& StrV) const; - void SplitOnStr(TStr& LeftStr, const TStr& MidStr, TStr& RightStr) const; - - //TStr Left(const int& Chs) const { return Chs>0 ? GetSubStr(0, Chs-1) : GetSubStr(0, Len()-Chs-1);} - //TStr Right(const int& Chs) const {return GetSubStr(Len()-Chs, Len()-1);} - TStr Mid(const int& BChN, const int& Chs) const { return GetSubStr(BChN, BChN+Chs-1); } - TStr Mid(const int& BChN) const {return GetSubStr(BChN, Len()-1); } - //TStr Slice(const int& BChN, const int& EChNP1) const {return GetSubStr(BChN, EChNP1-1);} - //TStr operator()(const int& BChN, const int& EChNP1) const {return Slice(BChN, EChNP1);} - //J: as in python or matlab: position 1 is 1st character, -1 is last character - TStr Left(const int& EChN) const { return EChN>0 ? GetSubStr(0, EChN-1) : GetSubStr(0, Len()+EChN-1);} - TStr Right(const int& BChN) const {return BChN>=0 ? GetSubStr(BChN, Len()-1) : GetSubStr(Len()+BChN, Len()-1);} - TStr Slice(int BChN, int EChNP1) const { if(BChN<0){BChN=Len()+BChN;} if(EChNP1<=0){EChNP1=Len()+EChNP1;} return GetSubStr(BChN, EChNP1-1); } - TStr operator()(const int& BChN, const int& EChNP1) const {return Slice(BChN, EChNP1);} - - int CountCh(const char& Ch, const int& BChN=0) const; - int SearchCh(const char& Ch, const int& BChN=0) const; - int SearchChBack(const char& Ch, int BChN=-1) const; - int SearchStr(const TStr& Str, const int& BChN=0) const; - bool IsChIn(const char& Ch) const {return SearchCh(Ch)!=-1;} - bool IsStrIn(const TStr& Str) const {return SearchStr(Str)!=-1;} - bool IsPrefix(const char *Str) const; - bool IsPrefix(const TStr& Str) const { - return IsPrefix(Str.CStr());} - bool IsSuffix(const char *Str) const; - bool IsSuffix(const TStr& Str) const { - return IsSuffix(Str.CStr());} - - int ChangeCh(const char& SrcCh, const char& DstCh, const int& BChN=0); - int ChangeChAll(const char& SrcCh, const char& DstCh); - int ChangeStr(const TStr& SrcStr, const TStr& DstStr, const int& BChN=0); - int ChangeStrAll(const TStr& SrcStr, const TStr& DstStr, const bool& FromStartP=false); - TStr Reverse() const { - TChA ChA(*this); ChA.Reverse(); return ChA;} - - int GetPrimHashCd() const {return RStr->GetPrimHashCd();} - int GetSecHashCd() const {return RStr->GetSecHashCd();} - - bool IsBool(bool& Val) const; - - bool IsInt( - const bool& Check, const int& MnVal, const int& MxVal, int& Val) const; - bool IsInt(int& Val) const {return IsInt(false, 0, 0, Val);} - bool IsInt() const {int Val; return IsInt(false, 0, 0, Val);} - int GetInt() const {int Val; IAssertR(IsInt(false, 0, 0, Val), *this); return Val;} - int GetInt(const int& DfVal) const { - int Val; if (IsInt(false, 0, 0, Val)){return Val;} else {return DfVal;}} - - bool IsUInt( - const bool& Check, const uint& MnVal, const uint& MxVal, uint& Val) const; - bool IsUInt(uint& Val) const {return IsUInt(false, 0, 0, Val);} - bool IsUInt() const {uint Val; return IsUInt(false, 0, 0, Val);} - uint GetUInt() const {uint Val; IAssert(IsUInt(false, 0, 0, Val)); return Val;} - uint GetUInt(const uint& DfVal) const { - uint Val; if (IsUInt(false, 0, 0, Val)){return Val;} else {return DfVal;}} - - bool IsInt64( - const bool& Check, const int64& MnVal, const int64& MxVal, int64& Val) const; - bool IsInt64(int64& Val) const {return IsInt64(false, 0, 0, Val);} - bool IsInt64() const {int64 Val; return IsInt64(false, 0, 0, Val);} - int64 GetInt64() const { - int64 Val; IAssert(IsInt64(false, 0, 0, Val)); return Val;} - int64 GetInt64(const int64& DfVal) const { - int64 Val; if (IsInt64(false, 0, 0, Val)){return Val;} else {return DfVal;}} - - bool IsUInt64( - const bool& Check, const uint64& MnVal, const uint64& MxVal, uint64& Val) const; - bool IsUInt64(uint64& Val) const {return IsUInt64(false, 0, 0, Val);} - bool IsUInt64() const {uint64 Val; return IsUInt64(false, 0, 0, Val);} - uint64 GetUInt64() const { - uint64 Val; IAssert(IsUInt64(false, 0, 0, Val)); return Val;} - uint64 GetUInt64(const uint64& DfVal) const { - uint64 Val; if (IsUInt64(false, 0, 0, Val)){return Val;} else {return DfVal;}} - - bool IsHexInt(const bool& Check, const int& MnVal, const int& MxVal, int& Val) const; - bool IsHexInt(int& Val) const {return IsHexInt(false, 0, 0, Val);} - bool IsHexInt() const {int Val; return IsHexInt(false, 0, 0, Val);} - int GetHexInt() const { - int Val; IAssert(IsHexInt(false, 0, 0, Val)); return Val;} - int GetHexInt(const int& DfVal) const { - int Val; if (IsHexInt(false, 0, 0, Val)){return Val;} else {return DfVal;}} - - bool IsHexInt64(const bool& Check, const int64& MnVal, const int64& MxVal, int64& Val) const; - bool IsHexInt64(int64& Val) const {return IsHexInt64(false, 0, 0, Val);} - bool IsHexInt64() const {int64 Val; return IsHexInt64(false, 0, 0, Val);} - int64 GetHexInt64() const { - int64 Val; IAssert(IsHexInt64(false, 0, 0, Val)); return Val;} - int64 GetHexInt64(const int64& DfVal) const { - int64 Val; if (IsHexInt64(false, 0, 0, Val)){return Val;} else {return DfVal;}} - - bool IsFlt(const bool& Check, const double& MnVal, const double& MxVal, - double& Val, const char& DecDelimCh='.') const; - bool IsFlt(double& Val) const {return IsFlt(false, 0, 0, Val);} - bool IsFlt() const {double Val; return IsFlt(false, 0, 0, Val);} - double GetFlt() const { - double Val; IAssert(IsFlt(false, 0, 0, Val)); return Val;} - double GetFlt(const double& DfVal) const { - double Val; if (IsFlt(false, 0, 0, Val)){return Val;} else {return DfVal;}} - - bool IsWord(const bool& WsPrefixP=true, const bool& FirstUcAllowedP=true) const; - bool IsWs() const; - - bool IsWcMatch( - const int& StrBChN, const TStr& WcStr, const int& WcStrBChN, TStrV& StarStrV, - const char& StarCh='*', const char& QuestCh='?') const; - bool IsWcMatch( - const TStr& WcStr, TStrV& StarStrV, - const char& StarCh='*', const char& QuestCh='?') const; - bool IsWcMatch(const TStr& WcStr, const char& StarCh, const char& QuestCh) const; - bool IsWcMatch(const TStr& WcStr, const int& StarStrN, TStr& StarStr) const; - bool IsWcMatch(const TStr& WcStr) const; - TStr GetWcMatch(const TStr& WcStr, const int& StarStrN=0) const; - - TStr GetFPath() const; - TStr GetFBase() const; - TStr GetFMid() const; - TStr GetFExt() const; - static TStr GetNrFPath(const TStr& FPath); - static TStr GetNrFMid(const TStr& FMid); - static TStr GetNrFExt(const TStr& FExt); - static TStr GetNrNumFExt(const int& FExtN); - static TStr GetNrFNm(const TStr& FNm); - static TStr GetNrAbsFPath(const TStr& FPath, const TStr& BaseFPath=TStr()); - static bool IsAbsFPath(const TStr& FPath); - static TStr PutFExt(const TStr& FNm, const TStr& FExt); - static TStr PutFExtIfEmpty(const TStr& FNm, const TStr& FExt); - static TStr PutFBase(const TStr& FNm, const TStr& FBase); - static TStr PutFBaseIfEmpty(const TStr& FNm, const TStr& FBase); - static TStr AddToFMid(const TStr& FNm, const TStr& ExtFMid); - static TStr GetNumFNm(const TStr& FNm, const int& Num); - static TStr GetFNmStr(const TStr& Str, const bool& AlNumOnlyP=true); - - static TStr LoadTxt(const PSIn& SIn){ - return TStr(SIn);} - static TStr LoadTxt(const TStr& FNm){ - PSIn SIn=TFIn::New(FNm); return LoadTxt(SIn);} - void SaveTxt(const PSOut& SOut) const { - SOut->SaveBf(CStr(), Len());} - void SaveTxt(const TStr& FNm) const { - PSOut SOut=TFOut::New(FNm); SaveTxt(SOut);} - - static TStr& GetChStr(const char& Ch); - static TStr& GetDChStr(const char& Ch1, const char& Ch2); - - TStr GetStr() const {return *this;} - static TStr GetStr(const TStr& Str, const char* FmtStr); - static TStr GetStr(const TStr& Str, const TStr& FmtStr){ - return GetStr(Str, FmtStr.CStr());} - static TStr GetStr(const TStrV& StrV, const TStr& DelimiterStr); - static TStr Fmt(const char *FmtStr, ...); - static TStr GetSpaceStr(const int& Spaces); - char* GetCStr() const { - char* Bf=new char[Len()+1]; strcpy(Bf, CStr()); return Bf;} - - static TStr MkClone(const TStr& Str){return TStr(Str.CStr());} - static TStr GetNullStr(); - - friend TStr operator+(const TStr& LStr, const TStr& RStr); - friend TStr operator+(const TStr& LStr, const char* RCStr); -}; - -///////////////////////////////////////////////// -// Input-String -class TStrIn: public TSIn{ -private: - TStr Str; - char* Bf; - int BfC, BfL; -private: - TStrIn(); - TStrIn(const TStrIn&); - TStrIn& operator = (const TStrIn&); -public: - TStrIn(const TStr& _Str); - static PSIn New(const TStr& Str){return PSIn(new TStrIn(Str));} - ~TStrIn(){} - - bool Eof(){return BfC==BfL;} - int Len() const {return BfL-BfC;} - char GetCh(){Assert(BfC class TNum{ -public: - Base Val; - TNum() : Val(0){} - TNum(const Base& _Val) : Val(_Val){} - operator Base() const { return Val; } - explicit TNum(TSIn& SIn){ SIn.Load(Val); } - void Load(TSIn& SIn){ SIn.Load(Val); } - void Save(TSOut& SOut) const { SOut.Save(Val); } - - TNum& operator=(const TNum& Other){ Val = Other.Val; return *this; } - TNum& operator=(const Base& _Val){ Val = _Val; return *this; } - TNum& operator++(){ ++Val; return *this; } // prefix - TNum& operator--(){ --Val; return *this; } // prefix - TNum operator++(Base){ TNum oldVal = Val; Val++; return oldVal; } // postfix - TNum operator--(Base){ TNum oldVal = Val; Val--; return oldVal; } // postfix - Base& operator()() { return Val; } - - int GetMemUsed() const { return sizeof(TNum); } -}; - -///////////////////////////////////////////////// -// Signed-Integer-64Bit -typedef TNum TInt64; -template<> -class TNum{ -public: - int64 Val; -public: - static const int64 Mn; - static const int64 Mx; - - TNum() : Val(0){} - TNum(const TNum& Int) : Val(Int.Val){} - TNum(const int64& Int) : Val(Int){} - operator int64() const { return Val; } - explicit TNum(TSIn& SIn){ SIn.Load(Val); } - void Load(TSIn& SIn){ SIn.Load(Val); } - void Save(TSOut& SOut) const { SOut.Save(Val); } - TNum& operator=(const TNum& Int){ Val = Int.Val; return *this; } - TNum& operator+=(const TNum& Int){ Val += Int.Val; return *this; } - TNum& operator-=(const TNum& Int){ Val -= Int.Val; return *this; } - TNum& operator++(){ ++Val; return *this; } // prefix - TNum& operator--(){ --Val; return *this; } // prefix - TNum operator++(int){ TNum oldVal = Val; Val++; return oldVal; } // postfix - TNum operator--(int){ TNum oldVal = Val; Val--; return oldVal; } // postfix - int GetMemUsed() const { return sizeof(TNum); } - -#ifdef GLib_WIN - TStr GetStr() const { return TStr::Fmt("%I64", Val); } - static TStr GetStr(const TNum& Int){ return TStr::Fmt("%I64", Int.Val); } - static TStr GetHexStr(const TNum& Int){ return TStr::Fmt("%I64X", Int.Val); } -#else - TStr GetStr() const { return TStr::Fmt("%ll", Val); } - static TStr GetStr(const TNum& Int){ return TStr::Fmt("%ll", Int.Val); } - static TStr GetHexStr(const TNum& Int){ return TStr::Fmt("%ll", Int.Val); } -#endif - - static TStr GetKiloStr(const int64& Val){ - if (Val>100 * 1000){ return GetStr(Val / 1000) + "K"; } - else if (Val>1000){ return GetStr(Val / 1000) + "." + GetStr((Val % 1000) / 100) + "K"; } - else { return GetStr(Val); } - } - static TStr GetMegaStr(const int64& Val){ - if (Val>100 * 1000000){ return GetStr(Val / 1000000) + "M"; } - else if (Val>1000000){ - return GetStr(Val / 1000000) + "." + GetStr((Val % 1000000) / 100000) + "M"; - } - else { return GetKiloStr(Val); } - } - /*static TStr GetGigaStr(const int64& Val){ - * if (Val>100*1000000000){return GetStr(Val/1000000000)+"G";} - * else if (Val>1000000000){ - * return GetStr(Val/1000000000)+"."+GetStr((Val%1000000000)/100000000)+"G";} - * else {return GetMegaStr(Val);}}*/ - - static int64 GetFromBufSafe(const char * Bf) { -#ifdef ARM - int64 Val; - memcpy(&Val, Bf, sizeof(int64)); //we cannot use a cast on ARM (needs 8byte memory aligned doubles) - return Val; -#else - return *((int64*)Bf); -#endif - } -}; - -///////////////////////////////////////////////// -// Void -class TVoid{ -public: - TVoid(){} - TVoid(TSIn&){} - void Save(TSOut&) const {} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TVoid& operator=(const TVoid&){return *this;} - bool operator==(const TVoid&) const {return true;} - bool operator<(const TVoid&) const {Fail; return false;} - int GetMemUsed() const {return sizeof(TVoid);} -}; - -///////////////////////////////////////////////// -// Boolean -class TBool{ -public: - bool Val; -public: - static const bool Mn; - static const bool Mx; - static const int Vals; - static TRnd Rnd; - - static const TStr FalseStr; - static const TStr TrueStr; - static const TStr NStr; - static const TStr YStr; - static const TStr NoStr; - static const TStr YesStr; - - TBool(): Val(false){} - TBool(const bool& _Val): Val(_Val){} - operator bool() const {return Val;} - explicit TBool(TSIn& SIn){SIn.Load(Val);} - void Load(TSIn& SIn){SIn.Load(Val);} - void Save(TSOut& SOut) const {SOut.Save(Val);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TBool& operator=(const TBool& Bool){Val=Bool.Val; return *this;} - bool operator==(const TBool& Bool) const {return Val==Bool.Val;} - bool operator<(const TBool& Bool) const {//return Val0?1:0);} - static void Swap(int& Int1, int& Int2){ - int SwapInt1=Int1; Int1=Int2; Int2=SwapInt1;} - static int GetRnd(const int& Range=0){return Rnd.GetUniDevInt(Range);} - - static bool IsOdd(const int& Int){return ((Int%2)==1);} - static bool IsEven(const int& Int){return ((Int%2)==0);} - - static int GetMn(const int& Int1, const int& Int2){ - return Int1Int2?Int1:Int2;} - static int GetMn(const int& Int1, const int& Int2, const int& Int3){ - return GetMn(Int1, GetMn(Int2, Int3));} - static int GetMn(const int& Int1, const int& Int2, - const int& Int3, const int& Int4){ - return GetMn(GetMn(Int1, Int2), GetMn(Int3, Int4));} - static int GetMx(const int& Int1, const int& Int2, const int& Int3){ - return GetMx(Int1, GetMx(Int2, Int3));} - static int GetMx(const int& Int1, const int& Int2, - const int& Int3, const int& Int4){ - return GetMx(GetMx(Int1, Int2), GetMx(Int3, Int4));} - static int GetInRng(const int& Val, const int& Mn, const int& Mx){ - IAssert(Mn<=Mx); return ValMx?Mx:Val);} - - TStr GetStr() const {return TInt::GetStr(Val);} - - static TStr GetStr(const int& Val){ return TStr::Fmt("%d", Val); } - static TStr GetStr(const TInt& Int){ return GetStr(Int.Val);} - static TStr GetStr(const int& Val, const char* FmtStr); - static TStr GetStr(const int& Val, const TStr& FmtStr){ return GetStr(Val, FmtStr.CStr());} - - //J: So that TInt can convert any kind of integer to a string - static TStr GetStr(const uint& Val){ return TStr::Fmt("%u", Val); } - #ifdef GLib_WIN - static TStr GetStr(const int64& Val) {return TStr::Fmt("%I64d", Val);} - static TStr GetStr(const uint64& Val) {return TStr::Fmt("%I64u", Val);} - #else - static TStr GetStr(const int64& Val) {return TStr::Fmt("%lld", Val);} - static TStr GetStr(const uint64& Val) {return TStr::Fmt("%llu", Val);} - #endif - - static TStr GetHexStr(const int& Val){ - char Bf[255]; sprintf(Bf, "%X", Val); return TStr(Bf);} - static TStr GetHexStr(const TInt& Int){ - return GetHexStr(Int.Val);} - - static TStr GetKiloStr(const int& Val){ - if (Val>=100*1000){return GetStr(Val/1000)+"K";} - else if (Val>=1000){return GetStr(Val/1000)+"."+GetStr((Val%1000)/100)+"K";} - else {return GetStr(Val);}} - static TStr GetMegaStr(const int& Val){ - if (Val>=100*1000000){return GetStr(Val/1000000)+"M";} - else if (Val>=1000000){ - return GetStr(Val/1000000)+"."+GetStr((Val%1000000)/100000)+"M";} - else {return GetKiloStr(Val);}} - - // frugal - static char* SaveFrugalInt(char *pDest, int i); - static char* LoadFrugalInt(char *pSrc, int& i); - static void TestFrugalInt(); - static void SaveFrugalIntV(TSOut& SOut, const TVec& IntV); - static void LoadFrugalIntV(TSIn& SIn, TVec& IntV, bool ClrP=true); -}; - -///////////////////////////////////////////////// -// Unsigned-Integer -class TUInt{ -public: - uint Val; -public: - static const uint Mn; - static const uint Mx; - static TRnd Rnd; - - TUInt(): Val(0){} - TUInt(const uint& _Val): Val(_Val){} - operator uint() const {return Val;} - explicit TUInt(TSIn& SIn){SIn.Load(Val);} - void Load(TSIn& SIn){SIn.Load(Val);} - void Save(TSOut& SOut) const {SOut.Save(Val);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TUInt& operator=(const TUInt& UInt){Val=UInt.Val; return *this;} - TUInt& operator=(const uint& _Val){Val=_Val; return *this;} - TUInt operator++(int){Val++; return *this;} - TUInt operator--(int){Val--; return *this;} - //bool operator==(const TUInt& UInt) const {return Val==UInt.Val;} - //bool operator==(const uint& UInt) const {return Val==UInt;} - //bool operator!=(const uint& UInt) const {return Val!=UInt;} - //bool operator<(const TUInt& UInt) const {return Val>=(const int& ShiftBits){Val>>=ShiftBits; return *this;} - TUInt& operator<<=(const int& ShiftBits){Val<<=ShiftBits; return *this;} - int GetMemUsed() const {return sizeof(TUInt);} - - int GetPrimHashCd() const {return int(Val);} - int GetSecHashCd() const {return Val/0x10;} - - static uint GetRnd(const uint& Range=0){return Rnd.GetUniDevUInt(Range);} - - TStr GetStr() const {return TUInt::GetStr(Val);} - static TStr GetStr(const uint& Val){ - char Bf[255]; sprintf(Bf, "%u", Val); return TStr(Bf);} - static TStr GetStr(const TUInt& UInt){ - return GetStr(UInt.Val);} - static TStr GetStr(const uint& Val, const char* FmtStr); - static TStr GetStr(const uint& Val, const TStr& FmtStr){ - return GetStr(Val, FmtStr.CStr());} - - static TStr GetKiloStr(const uint& Val){ - if (Val>100*1000){return GetStr(Val/1000)+"K";} - else if (Val>1000){return GetStr(Val/1000)+"."+GetStr((Val%1000)/100)+"K";} - else {return GetStr(Val);}} - static TStr GetMegaStr(const uint& Val){ - if (Val>100*1000000){return GetStr(Val/1000000)+"M";} - else if (Val>1000000){ - return GetStr(Val/1000000)+"."+GetStr((Val%1000000)/100000)+"M";} - else {return GetKiloStr(Val);}} - - static uint JavaUIntToCppUInt(const uint& JavaUInt){ - uint B1=(JavaUInt & 0xFF000000) >> 24; - uint B2=(JavaUInt & 0x00FF0000) >> 16; - uint B3=(JavaUInt & 0x0000FF00) >> 8; - uint B4=(JavaUInt & 0x000000FF) >> 0; - uint CppUInt=(B4<<24)+(B3<<16)+(B2<<8)+(B1<<0); - return CppUInt;} - - static bool IsIpStr(const TStr& IpStr, uint& Ip, const char& SplitCh = '.'); - static bool IsIpStr(const TStr& IpStr, const char& SplitCh = '.') { uint Ip; return IsIpStr(IpStr, Ip, SplitCh); } - static uint GetUIntFromIpStr(const TStr& IpStr, const char& SplitCh = '.'); - static TStr GetStrFromIpUInt(const uint& Ip); - static bool IsIpv6Str(const TStr& IpStr, const char& SplitCh = ':'); -}; - -///////////////////////////////////////////////// -// Unsigned-Integer-64Bit -class TUInt64{ -public: - uint64 Val; -public: - static const TUInt64 Mn; - static const TUInt64 Mx; - - TUInt64(): Val(0){} - TUInt64(const TUInt64& Int): Val(Int.Val){} - TUInt64(const uint64& Int): Val(Int){} - TUInt64(const uint& MsVal, const uint& LsVal): Val(0){ - Val=(((uint64)MsVal) << 32) | ((uint64)LsVal);} - explicit TUInt64(void* Pt): Val(0){ - TConv_Pt64Ints32 Conv(Pt); Val=Conv.GetUInt64();} - operator uint64() const {return Val;} - explicit TUInt64(TSIn& SIn){SIn.Load(Val);} - void Load(TSIn& SIn){SIn.Load(Val);} - void Save(TSOut& SOut) const {SOut.Save(Val);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TUInt64& operator=(const TUInt64& Int){Val=Int.Val; return *this;} - TUInt64& operator+=(const TUInt64& Int){Val+=Int.Val; return *this;} - TUInt64& operator-=(const TUInt64& Int){Val-=Int.Val; return *this;} - TUInt64& operator*=(const TUInt64& Int){Val*=Int.Val; return *this;} - TUInt64 operator++(int){Val++; return *this;} - TUInt64 operator--(int){Val--; return *this;} - int GetMemUsed() const {return sizeof(TUInt64);} - - int GetPrimHashCd() const { return (int)GetMsVal() + (int)GetLsVal(); } //TODO: to check - int GetSecHashCd() const { return ((int)GetMsVal() + (int)GetLsVal()) / 0x10; } //TODO: to check - - uint GetMsVal() const { - return (uint)(Val >> 32);} - uint GetLsVal() const { - return (uint)(Val & 0xffffffff);} - - //TStr GetStr() const {return TStr::Fmt("%Lu", Val);} - //static TStr GetStr(const TUInt64& Int){return TStr::Fmt("%Lu", Int.Val);} - //static TStr GetHexStr(const TUInt64& Int){return TStr::Fmt("%LX", Int.Val);} - #ifdef GLib_WIN - TStr GetStr() const {return TStr::Fmt("%I64u", Val);} - static TStr GetStr(const TUInt64& Int){return TStr::Fmt("%I64u", Int.Val);} - static TStr GetHexStr(const TUInt64& Int){return TStr::Fmt("%I64X", Int.Val);} - #else - TStr GetStr() const {return TStr::Fmt("%llu", Val);} - static TStr GetStr(const TUInt64& Int){return TStr::Fmt("%llu", Int.Val);} - static TStr GetHexStr(const TUInt64& Int){return TStr::Fmt("%llX", Int.Val);} - #endif - - static TStr GetKiloStr(const uint64& Val){ - if (Val>100*1000){return GetStr(Val/1000)+"K";} - else if (Val>1000){return GetStr(Val/1000)+"."+GetStr((Val%1000)/100)+"K";} - else {return GetStr(Val);}} - static TStr GetMegaStr(const uint64& Val){ - if (Val>100*1000000){return GetStr(Val/1000000)+"M";} - else if (Val>1000000){ - return GetStr(Val/1000000)+"."+GetStr((Val%1000000)/100000)+"M";} - else {return GetKiloStr(Val);}} - /*static TStr GetGigaStr(const uint64& Val){ - if (Val>100*1000000000){return GetStr(Val/1000000000)+"G";} - else if (Val>1000000000){ - return GetStr(Val/1000000000)+"."+GetStr((Val%1000000000)/100000000)+"G";} - else {return GetMegaStr(Val);}}*/ -}; - -///////////////////////////////////////////////// -// Float -class TFlt{ -public: - double Val; -public: - static const double Mn; - static const double Mx; - static const double NInf; - static const double PInf; - static const double Eps; - static const double EpsHalf; - static TRnd Rnd; - - TFlt(): Val(0){} - TFlt(const double& _Val): Val(_Val){} - operator double() const {return Val;} - explicit TFlt(TSIn& SIn){SIn.Load(Val);} - void Save(TSOut& SOut) const {SOut.Save(Val);} - explicit TFlt(TSIn& SIn, const bool& IsTxt){ - if (IsTxt){TStr Str(SIn, true); Val=Str.GetFlt(0);} else {SIn.Load(Val);}} - void Load(TSIn& SIn){SIn.Load(Val);} - void Save(TSOut& SOut, const bool& IsTxt) const { - if (IsTxt){GetStr(Val).Save(SOut, true);} else {SOut.Save(Val);}} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TFlt& operator=(const TFlt& Flt){Val=Flt.Val; return *this;} - TFlt& operator=(const double& Flt){Val=Flt; return *this;} - bool operator==(const TFlt& Flt) const _CMPWARN {return Val==Flt.Val;} - bool operator==(const double& Flt) const _CMPWARN {return Val==Flt;} - bool operator!=(const double& Flt) const _CMPWARN {return Val!=Flt;} - double operator()() const {return Val;} - TFlt& operator+=(const double& Flt){Val+=Flt; return *this;} - TFlt& operator-=(const double& Flt){Val-=Flt; return *this;} - TFlt& operator*=(const double& Flt){Val*=Flt; return *this;} - TFlt& operator/=(const double& Flt){Val/=Flt; return *this;} - TFlt operator++(int){Val++; return *this;} - TFlt operator--(int){Val--; return *this;} - int GetMemUsed() const {return sizeof(TFlt);} - - int GetPrimHashCd() const { - int Expn; return int((frexp(Val, &Expn)-0.5)*double(TInt::Mx));} - int GetSecHashCd() const { - int Expn; frexp(Val, &Expn); return Expn;} - - static double Abs(const double& Flt){return Flt<0?-Flt:Flt;} - static int Sign(const double& Flt){return Flt<0?-1:(Flt>0?1:0);} - static int Round(const double& Flt){return int(floor(Flt+0.5));} - static double GetRnd(){return Rnd.GetUniDev();} - static bool Eq6(const double& LFlt, const double& RFlt){ - return fabs(LFlt-RFlt)<0.000001;} - - static double GetMn(const double& Flt1, const double& Flt2){ - return Flt1Flt2?Flt1:Flt2;} - static double GetMx(const double& Flt1, const double& Flt2, const double Flt3){ - return GetMx(GetMx(Flt1, Flt2), Flt3); } - static double GetMx(const double& Flt1, const double& Flt2, const double Flt3, const double& Flt4){ - return GetMx(GetMx(Flt1, Flt2), GetMx(Flt3, Flt4)); } - - static double GetInRng(const double& Val, const double& Mn, const double& Mx){ - IAssert(Mn<=Mx); return ValMx?Mx:Val);} - - static bool IsNum(const double& Val){ - return (Mn<=Val)&&(Val<=Mx);} - static bool IsNan(const double& Val){ - return (Val!=Val);} - - bool IsNum() const { return IsNum(Val); } - bool IsNan() const { return IsNan(Val); } - - TStr GetStr() const {return TFlt::GetStr(Val);} - static TStr GetStr(const double& Val, const int& Width=-1, const int& Prec=-1); - static TStr GetStr(const TFlt& Flt, const int& Width=-1, const int& Prec=-1){ - return GetStr(Flt.Val, Width, Prec);} - static TStr GetStr(const double& Val, const char* FmtStr); - static TStr GetStr(const double& Val, const TStr& FmtStr){ - return GetStr(Val, FmtStr.CStr());} - static TStr GetPrcStr(const double& RelVal, const double& FullVal){ - return GetStr(100*RelVal/FullVal, "%3.0f%%");} - - static TStr GetKiloStr(const double& Val){ - if (fabs(Val)>100*1000){return TStr::Fmt("%.0fK", Val/1000);} - else if (fabs(Val)>1000){return TStr::Fmt("%.1fK", Val/1000);} - else {return TStr::Fmt("%.0f", Val);}} - static TStr GetMegaStr(const double& Val){ - if (fabs(Val)>100*1000000){return TStr::Fmt("%.0fM", Val/1000000);} - else if (fabs(Val)>1000000){return TStr::Fmt("%.1fM", Val/1000000);} - else {return GetKiloStr(Val);}} - static TStr GetGigaStr(const double& Val){ - if (fabs(Val)>100*1000000000.0){return TStr::Fmt("%.0fG", Val/1000000000.0);} - else if (fabs(Val)>1000000000.0){return TStr::Fmt("%.1fG", Val/1000000000.0);} - else {return GetMegaStr(Val);}} -}; - -///////////////////////////////////////////////// -// Ascii-Float -class TAscFlt: public TFlt{ -public: - TAscFlt(): TFlt(){} - TAscFlt(const double& Val): TFlt(Val){} - explicit TAscFlt(TSIn& SIn): TFlt(SIn, true){} - void Save(TSOut& SOut) const {TFlt::Save(SOut, true);} -}; - -///////////////////////////////////////////////// -// Short-Float -class TSFlt{ -public: - sdouble Val; -public: - static const sdouble Mn; - static const sdouble Mx; - - TSFlt(): Val(0){} - TSFlt(const sdouble& _Val): Val(sdouble(_Val)){} - //TSFlt(const double& _Val): Val(sdouble(_Val)){} - operator sdouble() const {return Val;} - //operator double() const {return Val;} - explicit TSFlt(TSIn& SIn){SIn.Load(Val);} - void Save(TSOut& SOut) const {SOut.Save(Val);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - TSFlt& operator=(const TSFlt& SFlt){Val=SFlt.Val; return *this;} - bool operator==(const TSFlt& SFlt) const _CMPWARN {return Val==SFlt.Val;} - bool operator==(const double& Flt) const _CMPWARN {return Val==Flt;} - bool operator!=(const double& Flt) const _CMPWARN {return Val!=Flt;} - bool operator<(const TSFlt& SFlt) const {return ValFromArgN){CmLnChA+=' ';} - CmLnChA+=GetArg(ArgN); - } - return CmLnChA; -} - -int TEnv::GetPrefixArgN(const TStr& PrefixStr) const { - int ArgN=0; - while (ArgN1){ - if ((Val[0]=='\"')&&(Val.LastCh()=='\"')){ - Val=Val.GetSubStr(1, Val.Len()-2); - } - } - } else { - Val=DfVal; - } - TStr MsgStr=DNm+" ("+PrefixStr+")="+Val; - if (!SilentP){TNotify::OnStatus(Notify, MsgStr);} - return Val; - } -} - -TStrV TEnv::GetIfArgPrefixStrV( - const TStr& PrefixStr, TStrV& DfValV, const TStr& DNm) const { - TStrV ArgValV; - if (Env.GetArgs()<=MnArgs){ - // 'usage' argument message - if (!SilentP){ - printf(" %s%s (default:", PrefixStr.CStr(), DNm.CStr()); - for (int DfValN=0; DfValN0){printf(", ");} - printf("'%s'", DfValV[DfValN].CStr()); - } - printf(")\n"); - } - return ArgValV; - } else { - // argument & value message - TStr ArgValVChA; - for (int ArgN=0; ArgN1){ArgValVChA+=", ";} - ArgValVChA+=ArgValV.Last(); - } - } - if (ArgValV.Empty()){ArgValV=DfValV;} - // output argument values - TChA MsgChA; - MsgChA+=DNm; MsgChA+=" ("; MsgChA+=PrefixStr; MsgChA+=")="; - for (int ArgValN=0; ArgValN0){MsgChA+=", ";} - MsgChA+="'"; MsgChA+=ArgValV[ArgValN]; MsgChA+="'"; - } - if (!SilentP){TNotify::OnStatus(Notify, MsgChA);} - return ArgValV; - } -} - -bool TEnv::GetIfArgPrefixBool( - const TStr& PrefixStr, const bool& DfVal, const TStr& DNm) const { - if (Env.GetArgs()<=MnArgs){ - // 'usage' argument message - if (!SilentP){ - printf(" %s%s (default:'%s')\n", - PrefixStr.CStr(), DNm.CStr(), TBool::GetStr(DfVal).CStr());} - return DfVal; - } else { - // argument & value message - bool Val; - if ((PrefixStr.Len()>0)&&(PrefixStr.LastCh()==':')){ - if (Env.IsArgPrefix(PrefixStr)){ - // try to find one of boolean string value representations - TStr ValStr=Env.GetArgPostfix(PrefixStr); - Val=TBool::GetValFromStr(ValStr, DfVal); - } else { - // remove ':' and try to find option - TStr RedPrefixStr=PrefixStr; - RedPrefixStr.DelSubStr(PrefixStr.Len()-1, PrefixStr.Len()-1); - if (Env.IsArgPrefix(RedPrefixStr)){Val=true;} else {Val=DfVal;} - } - } else { - if (Env.IsArgPrefix(PrefixStr)){Val=true;} else {Val=DfVal;} - } - TStr MsgStr=DNm+" ("+PrefixStr+")="+TBool::GetYesNoStr(Val); - if (!SilentP){TNotify::OnStatus(Notify, MsgStr);} - return Val; - } -} - -int TEnv::GetIfArgPrefixInt( - const TStr& PrefixStr, const int& DfVal, const TStr& DNm) const { - if (Env.GetArgs()<=MnArgs){ - // 'usage' argument message - if (!SilentP){ - printf(" %s%s (default:%d)\n", PrefixStr.CStr(), DNm.CStr(), DfVal);} - return DfVal; - } else { - // argument & value message - int Val; - if (Env.IsArgPrefix(PrefixStr)){ - TStr ValStr=Env.GetArgPostfix(PrefixStr); - Val=ValStr.GetInt(DfVal); - } else { - Val=DfVal; - } - TStr MsgStr=DNm+" ("+PrefixStr+")="+TInt::GetStr(Val); - if (!SilentP){TNotify::OnStatus(Notify, MsgStr);} - return Val; - } -} - -TIntV TEnv::GetIfArgPrefixIntV( - const TStr& PrefixStr, TIntV& DfValV, const TStr& DNm) const { - // convert default-integer-values to default-string-values - TStrV DfValStrV; - for (int ValN=0; ValN -} - -#endif - -///////////////////////////////////////////////// -// Check-Sum -const int TCs::MxMask=0x0FFFFFFF; - -TCs TCs::GetCsFromBf(char* Bf, const int& BfL){ - TCs Cs; - for (int BfC=0; BfC=0, "Error reading stream '"+GetSNm()+"'."); - CStr=new char[CStrLen+1]; - if (CStrLen>0){Cs+=GetBf(CStr, CStrLen);} - CStr[CStrLen]=TCh::NullCh; -} - -bool TSIn::GetNextLn(TStr& LnStr){ - TChA LnChA; - const bool IsNext=GetNextLn(LnChA); - LnStr=LnChA; - return IsNext; -} - -bool TSIn::GetNextLn(TChA& LnChA){ - LnChA.Clr(); - while (!Eof()){ - const char Ch=GetCh(); - if (Ch=='\n'){return true;} - if (Ch=='\r' && PeekCh()=='\n'){GetCh(); return true;} - LnChA.AddCh(Ch); - } - return !LnChA.Empty(); -} - -const PSIn TSIn::StdIn=PSIn(new TStdIn()); - -TStdIn::TStdIn(): TSBase("Standard input"), TSIn("Standard input") {} - -///////////////////////////////////////////////// -// Output-Stream -TSOut::TSOut(const TStr& Str): - TSBase(Str.CStr()), MxLnLen(-1), LnLen(0){} - -int TSOut::UpdateLnLen(const int& StrLen, const bool& ForceInLn){ - int Cs=0; - if (MxLnLen!=-1){ - if ((!ForceInLn)&&(LnLen+StrLen>MxLnLen)){Cs+=PutLn();} - LnLen+=StrLen; - } - return Cs; -} - -int TSOut::PutMem(const TMem& Mem){ - return PutBf(Mem(), Mem.Len()); -} - -int TSOut::PutCh(const char& Ch, const int& Chs){ - int Cs=0; - for (int ChN=0; ChN0){ - if (LnLen+1+NextStrLen>MxLnLen){Cs+=PutLn();} else {Cs+=PutCh(' ');} - } - } - return Cs; -} - -int TSOut::PutSepLn(const int& Lns){ - int Cs=0; - if (LnLen>0){Cs+=PutLn();} - Cs+=PutLn(Lns); - return Cs; -} - -void TSOut::Save(const char* CStr){ - int CStrLen=int(strlen(CStr)); - EAssertR(CStrLen<=127, "Error writting stream '"+GetSNm()+"'."); - Save(char(CStrLen)); - if (CStrLen>0){Cs+=PutBf(CStr, CStrLen);} -} - -void TSOut::Save(TSIn& SIn, const TSize& BfL){ - Fail; - if (BfL==0){ //J: used to be ==-1 - while (!SIn.Eof()){Save(SIn.GetCh());} - } else { - for (TSize BfC=0; BfCGetMsgStr().CStr()); - EFailR(Except->GetMsgStr()); - } - - return PSIn(new TFIn(FNm)); -} - -PSIn TFIn::New(const TStr& FNm, bool& OpenedP){ - return PSIn(new TFIn(FNm, OpenedP)); -} - -TFIn::~TFIn(){ - if (FileId!=NULL){ - EAssertR(fclose(FileId)==0, "Can not close file '"+GetSNm()+"'.");} - if (Bf!=NULL){delete[] Bf;} -} - -int TFIn::GetBf(const void* LBf, const TSize& LBfL){ - int LBfS=0; - if (TSize(BfC+LBfL)>TSize(BfL)){ - for (TSize LBfC=0; LBfC= BfL) { - // reset the current pointer, FindEol() will read a new buffer - BfP = 0; - } else { - BfP = BfC; - } - Status = FindEol(BfN,CrEnd); - if (Status >= 0) { - if (BfN-BfP > 0) { - LnChA.AddBf(&Bf[BfP],BfN-BfP); - } - if (Status == 1) { - // got a complete line - return true; - } - } - // get more data, if the line is incomplete - } while (Status == 0); - - // eof or the last line has no newline - return !LnChA.Empty(); -} - -// Sets BfN to the end of line or end of buffer. Reads more data, if needed. -// Returns 1, when an end of line was found, BfN is end of line. -// Returns 0, when an end of line was not found and more data is required, -// BfN is end of buffer. -// Returns -1, when an end of file was found, BfN is not defined. - -int TFIn::FindEol(int& BfN, bool& CrEnd) { - char Ch; - - if (BfC >= BfL) { - // read more data, check for eof - if (Eof()) { - return -1; - } - if (CrEnd && Bf[BfC]=='\n') { - BfC++; - BfN = BfC-1; - return 1; - } - } - - CrEnd = false; - while (BfC < BfL) { - Ch = Bf[BfC++]; - if (Ch=='\n') { - BfN = BfC-1; - return 1; - } - if (Ch=='\r') { - if (BfC == BfL) { - CrEnd = true; - BfN = BfC-1; - return 0; - } else if (Bf[BfC]=='\n') { - BfC++; - BfN = BfC-2; - return 1; - } - } - } - BfN = BfC; - - return 0; -} - -///////////////////////////////////////////////// -// Output-File -const TSize TFOut::MxBfL=16*1024;; - -void TFOut::FlushBf(){ - EAssertR( - fwrite(Bf, 1, BfL, FileId)==BfL, - "Error writting to the file '"+GetSNm()+"'."); - BfL=0; -} - -TFOut::TFOut(const TStr& FNm, const bool& Append): - TSBase(FNm.CStr()), TSOut(FNm), FileId(NULL), Bf(NULL), BfL(0){ - if (FNm.GetUc()=="CON"){ - FileId=stdout; - } else { - if (Append){FileId=fopen(FNm.CStr(), "a+b");} - else {FileId=fopen(FNm.CStr(), "w+b");} - EAssertR(FileId!=NULL, "Can not open file '"+FNm+"'."); - Bf=new char[MxBfL]; BfL=0; - } -} - -TFOut::TFOut(const TStr& FNm, const bool& Append, bool& OpenedP): - TSBase(FNm.CStr()), TSOut(FNm), FileId(NULL), Bf(NULL), BfL(0){ - if (FNm.GetUc()=="CON"){ - FileId=stdout; - } else { - if (Append){FileId=fopen(FNm.CStr(), "a+b");} - else {FileId=fopen(FNm.CStr(), "w+b");} - OpenedP=(FileId!=NULL); - if (OpenedP){ - Bf=new char[MxBfL]; BfL=0;} - } -} - -PSOut TFOut::New(const TStr& FNm, const bool& Append){ - return PSOut(new TFOut(FNm, Append)); -} - -PSOut TFOut::New(const TStr& FNm, const bool& Append, bool& OpenedP){ - PSOut SOut=PSOut(new TFOut(FNm, Append, OpenedP)); - if (OpenedP){return SOut;} else {return NULL;} -} - -TFOut::~TFOut(){ - if (FileId!=NULL){FlushBf();} - if (Bf!=NULL){delete[] Bf;} - if (FileId!=NULL){ - EAssertR(fclose(FileId)==0, "Can not close file '"+GetSNm()+"'.");} -} - -int TFOut::PutCh(const char& Ch){ - if (BfL==TSize(MxBfL)){FlushBf();} - return Bf[BfL++]=Ch; -} - -int TFOut::PutBf(const void* LBf, const TSize& LBfL){ - int LBfS=0; - if (BfL+LBfL>MxBfL){ - for (TSize LBfC=0; LBfC(BfL)]; memmove(Bf, _Bf, static_cast(BfL)); - } -} - -TMIn::TMIn(TSIn& SIn): - TSBase("Input-Memory"), TSIn("Input-Memory"), Bf(NULL), BfC(0), BfL(0), IsMemoryMapped(false){ - BfL=SIn.Len(); Bf=new char[static_cast(BfL)]; - for (uint64 BfC=0; BfC(BfL+1)]; strcpy(Bf, CStr); -} - -/* GLib_LINUX should be defined if FromFile is true */ -TMIn::TMIn(const TStr& Str, bool FromFile): - TSBase("Input-Memory"), TSIn("Input-Memory"), Bf(NULL), BfC(0), BfL(0){ - if (FromFile == false) { - BfL=Str.Len(); Bf=new char[static_cast(BfL)]; strncpy(Bf, Str.CStr(), static_cast(BfL)); - IsMemoryMapped = false; - } - else { -#ifdef GLib_LINUX - TStr FNm = Str; - TFileId FileId; - int fd; - uint64 FLen; - EAssertR(!FNm.Empty(), "Empty file-name."); - FileId=fopen(FNm.CStr(), "rb"); - fd = fileno(FileId); - - EAssertR(FileId!=NULL, "Can not open file '"+FNm+"'."); - - EAssertR( - fseek(FileId, 0, SEEK_END)==0, - "Error seeking into file '"+TStr(FNm)+"'."); - FLen=(uint64)ftell(FileId); - EAssertR( - fseek(FileId, 0, SEEK_SET)==0, - "Error seeking into file '"+TStr(FNm)+"'."); - - // memory map contents of file - char *mapped; - mapped = (char *) mmap (0, FLen, PROT_READ, MAP_PRIVATE, fd, 0); - IsMemoryMapped = true; - - if (mapped == MAP_FAILED) { - printf("mmap failed: %d %s\n", fd, strerror (errno)); - Bf = NULL; - BfC = BfL = 0; - } - else { - Bf = mapped; - BfC = 0; - BfL = FLen; - } - IsMemoryMapped = true; -#else - TExcept::Throw("TMIn::TMIn(TStr, Bool): GLib_LINUX undefined.\n"); -#endif - } -} - -TMIn::TMIn(const TChA& ChA): - TSBase("Input-Memory"), TSIn("Input-Memory"), Bf(NULL), BfC(0), BfL(0), IsMemoryMapped(false){ - BfL=ChA.Len(); Bf=new char[static_cast(BfL)]; strncpy(Bf, ChA.CStr(), static_cast(BfL)); -} - -PSIn TMIn::New(const void* _Bf, const uint64& _BfL, const bool& TakeBf){ - return PSIn(new TMIn(_Bf, _BfL, TakeBf)); -} - -PSIn TMIn::New(const char* CStr){ - return PSIn(new TMIn(CStr)); -} - -PSIn TMIn::New(const TStr& Str){ - return PSIn(new TMIn(Str)); -} - -PMIn TMIn::New(const TStr& Str, bool FromFile){ - return new TMIn(Str, FromFile); -} - -PSIn TMIn::New(const TChA& ChA){ - return PSIn(new TMIn(ChA)); -} - -TMIn::~TMIn(){ - if (Bf!=NULL){ - if (IsMemoryMapped) { -#ifdef GLib_LINUX - munmap(Bf, BfL); -#endif - } - else { - delete[] Bf; - } - } -} - -char TMIn::GetCh(){ - EAssertR(BfC= BfL) { - // read more data, check for eof - if (Eof()) { - return -1; - } - if (CrEnd && Bf[BfC]=='\n') { - BfC++; - BfN = BfC-1; - return 1; - } - } - - CrEnd = false; - while (BfC < BfL) { - Ch = Bf[BfC++]; - if (Ch=='\n') { - BfN = BfC-1; - return 1; - } - if (Ch=='\r') { - if (BfC == BfL) { - CrEnd = true; - BfN = BfC-1; - return 0; - } else if (Bf[BfC]=='\n') { - BfC++; - BfN = BfC-2; - return 1; - } - } - } - BfN = BfC; - - return 0; -} - -bool TMIn::GetNextLnBf(TChA& LnChA){ - // not implemented - FailR(TStr::Fmt("TMIn::GetNextLnBf: not implemented").CStr()); - return false; -} - -uint64 TMIn::GetBfC() { - return BfC; -} - -uint64 TMIn::GetBfL() { - return BfL; -} - -void TMIn::SetBfC(uint64 Pos) { - BfC = Pos; -} - -// Assumes that lines end in '\n' -uint64 TMIn::CountNewLinesInRange(uint64 Lb, uint64 Ub) { - uint64 Cnt = 0; - if (Lb >= BfL) { - return 0; - } - for (uint64 i = Lb; i < Ub; i++) { - if (Bf[i] == '\n') { - Cnt += 1; - } - } - return Cnt; -} - -uint64 TMIn::GetLineStartPos(uint64 Ind) { - while (Ind > 0 && Bf[Ind-1] != '\n') { - Ind--; - } - return Ind; -} - -uint64 TMIn::GetLineEndPos(uint64 Ind) { - while (Ind < BfL && Bf[Ind] != '\n') { - Ind++; - } - if (Ind == BfL) Ind--; - return Ind; -} - -char* TMIn::GetLine(uint64 Index) { - return &Bf[Index]; -} - -void TMIn::SkipCommentLines() { - while (BfC < BfL && TCh::IsHashCh(Bf[BfC])) { - while (BfC < BfL && Bf[BfC] != '\n') { - BfC++; - } - BfC++; - } -} - -///////////////////////////////////////////////// -// Output-Memory -void TMOut::Resize(const int& ReqLen){ - IAssert(OwnBf&&(BfL==MxBfL || ReqLen >= 0)); - if (Bf==NULL){ - IAssert(MxBfL==0); - if (ReqLen < 0) Bf=new char[MxBfL=1024]; - else Bf=new char[MxBfL=ReqLen]; - } else { - if (ReqLen < 0){ MxBfL*=2; } - else if (ReqLen < MxBfL){ return; } // nothing to do - else { MxBfL=(2*MxBfL < ReqLen ? ReqLen : 2*MxBfL); } - char* NewBf=new char[MxBfL]; - memmove(NewBf, Bf, BfL); delete[] Bf; Bf=NewBf; - } -} - -TMOut::TMOut(const int& _MxBfL): - TSBase("Output-Memory"), TSOut("Output-Memory"), - Bf(NULL), BfL(0), MxBfL(0), OwnBf(true){ - MxBfL=_MxBfL>0?_MxBfL:1024; - Bf=new char[MxBfL]; -} - -TMOut::TMOut(char* _Bf, const int& _MxBfL): - TSBase("Output-Memory"), TSOut("Output-Memory"), - Bf(_Bf), BfL(0), MxBfL(_MxBfL), OwnBf(false){} - -void TMOut::AppendBf(const void* LBf, const TSize& LBfL) { - Resize(Len() + (int)LBfL); - memcpy(Bf + BfL, LBf, LBfL); - BfL += (int)LBfL; -} - -int TMOut::PutBf(const void* LBf, const TSize& LBfL){ - int LBfS=0; - if (TSize(BfL+LBfL)>TSize(MxBfL)){ - for (TSize LBfC=0; LBfCEof()) { return false; } - TChA LnChA; char Ch = TCh::EofCh; - while (!SIn->Eof() && ((Ch=SIn->GetCh())!='\n')) { - if (Ch != '\r') { LnChA += Ch; } - } - LnStr = LnChA; return true; -} - -///////////////////////////////////////////////// -// fseek-Constants-Definitions -// because of strange Borland CBuilder behaviour in sysdefs.h -#ifndef SEEK_SET -#define SEEK_CUR 1 -#define SEEK_END 2 -#define SEEK_SET 0 -#endif - -///////////////////////////////////////////////// -// Random-File -void TFRnd::RefreshFPos(){ - EAssertR( - fseek(FileId, 0, SEEK_CUR)==0, - "Error seeking into file '"+TStr(FNm)+"'."); -} - -TFRnd::TFRnd(const TStr& _FNm, const TFAccess& FAccess, - const bool& CreateIfNo, const int& _HdLen, const int& _RecLen): - FileId(NULL), FNm(_FNm.CStr()), - RecAct(false), HdLen(_HdLen), RecLen(_RecLen){ - RecAct=(HdLen>=0)&&(RecLen>0); - switch (FAccess){ - case faCreate: FileId=fopen(FNm.CStr(), "w+b"); break; - case faUpdate: FileId=fopen(FNm.CStr(), "r+b"); break; - case faAppend: FileId=fopen(FNm.CStr(), "r+b"); - if (FileId!=NULL){fseek(FileId, SEEK_END, 0);} break; - case faRdOnly: FileId=fopen(FNm.CStr(), "rb"); break; - default: Fail; - } - if ((FileId==NULL)&&(CreateIfNo)){ - FileId=fopen(FNm.CStr(), "w+b");} - EAssertR(FileId!=NULL, "Can not open file '"+_FNm+"'."); -} - -TFRnd::~TFRnd(){ - EAssertR(fclose(FileId)==0, "Can not close file '"+TStr(FNm)+"'."); -} - -TStr TFRnd::GetFNm() const { - return FNm.CStr(); -} - -void TFRnd::SetFPos(const int& FPos){ - EAssertR( - fseek(FileId, FPos, SEEK_SET)==0, - "Error seeking into file '"+TStr(FNm)+"'."); -} - -void TFRnd::MoveFPos(const int& DFPos){ - EAssertR( - fseek(FileId, DFPos, SEEK_CUR)==0, - "Error seeking into file '"+TStr(FNm)+"'."); -} - -int TFRnd::GetFPos(){ - int FPos= (int) ftell(FileId); - EAssertR(FPos!=-1, "Error seeking into file '"+TStr(FNm)+"'."); - return FPos; -} - -int TFRnd::GetFLen(){ - int FPos=GetFPos(); - EAssertR( - fseek(FileId, 0, SEEK_END)==0, - "Error seeking into file '"+TStr(FNm)+"'."); - int FLen=GetFPos(); SetFPos(FPos); return FLen; -} - -void TFRnd::SetRecN(const int& RecN){ - IAssert(RecAct); - SetFPos(HdLen+RecN*RecLen); -} - -int TFRnd::GetRecN(){ - IAssert(RecAct); - int FPos=GetFPos()-HdLen; - EAssertR(FPos%RecLen==0, "Invalid position in file'"+TStr(FNm)+"'."); - return FPos/RecLen; -} - -int TFRnd::GetRecs(){ - IAssert(RecAct); - int FLen=GetFLen()-HdLen; - EAssertR(FLen%RecLen==0, "Invalid length of file'"+TStr(FNm)+"'."); - return FLen/RecLen; -} - -void TFRnd::GetBf(void* Bf, const TSize& BfL){ - RefreshFPos(); - EAssertR( - fread(Bf, 1, BfL, FileId)==BfL, - "Error reading file '"+TStr(FNm)+"'."); -} - -void TFRnd::PutBf(const void* Bf, const TSize& BfL){ - RefreshFPos(); - EAssertR( - fwrite(Bf, 1, BfL, FileId)==BfL, - "Error writting to the file '"+TStr(FNm)+"'."); -} - -void TFRnd::Flush(){ - EAssertR(fflush(FileId)==0, "Can not flush file '"+TStr(FNm)+"'."); -} - -void TFRnd::PutCh(const char& Ch, const int& Chs){ - if (Chs>0){ - char* CStr=new char[Chs]; - for (int ChN=0; ChNLen(); - char* Bf=new char[BfL]; - SIn->GetBf(Bf, BfL); - Cs=TCs::GetCsFromBf(Bf, BfL); - PutBf(Bf, BfL); - delete[] Bf; -} - -PSIn TFRnd::GetSIn(const int& BfL, TCs& Cs){ - char* Bf=new char[BfL]; - GetBf(Bf, BfL); - Cs=TCs::GetCsFromBf(Bf, BfL); - PSIn SIn=PSIn(new TMIn(Bf, BfL, true)); - return SIn; -} - -TStr TFRnd::GetStrFromFAccess(const TFAccess& FAccess){ - switch (FAccess){ - case faCreate: return "Create"; - case faUpdate: return "Update"; - case faAppend: return "Append"; - case faRdOnly: return "ReadOnly"; - case faRestore: return "Restore"; - default: Fail; return TStr(); - } -} - -TFAccess TFRnd::GetFAccessFromStr(const TStr& Str){ - TStr UcStr=Str.GetUc(); - if (UcStr=="CREATE"){return faCreate;} - if (UcStr=="UPDATE"){return faUpdate;} - if (UcStr=="APPEND"){return faAppend;} - if (UcStr=="READONLY"){return faRdOnly;} - if (UcStr=="RESTORE"){return faRestore;} - - if (UcStr=="NEW"){return faCreate;} - if (UcStr=="CONT"){return faUpdate;} - if (UcStr=="CONTINUE"){return faUpdate;} - if (UcStr=="REST"){return faRestore;} - if (UcStr=="RESTORE"){return faRestore;} - return faUndef; -} - -///////////////////////////////////////////////// -// Files -const TStr TFile::TxtFExt=".Txt"; -const TStr TFile::HtmlFExt=".Html"; -const TStr TFile::HtmFExt=".Htm"; -const TStr TFile::GifFExt=".Gif"; -const TStr TFile::JarFExt=".Jar"; - -bool TFile::Exists(const TStr& FNm){ - if (FNm.Empty()) { return false; } - bool DoExists; - TFIn FIn(FNm, DoExists); - return DoExists; -} - -#if defined(GLib_WIN32) - -void TFile::Copy(const TStr& SrcFNm, const TStr& DstFNm, - const bool& ThrowExceptP, const bool& FailIfExistsP){ - if (ThrowExceptP){ - if (CopyFile(SrcFNm.CStr(), DstFNm.CStr(), FailIfExistsP) == 0) { - int ErrorCode = (int)GetLastError(); - TExcept::Throw(TStr::Fmt( - "Error %d copying file '%s' to '%s'.", - ErrorCode, SrcFNm.CStr(), DstFNm.CStr())); - } - } else { - CopyFile(SrcFNm.CStr(), DstFNm.CStr(), FailIfExistsP); - } -} - -#elif defined(GLib_LINUX) - -void TFile::Copy(const TStr& SrcFNm, const TStr& DstFNm, - const bool& ThrowExceptP, const bool& FailIfExistsP){ - int input, output; - size_t filesize; - void *source, *target; - - if( (input = open(SrcFNm.CStr(), O_RDONLY)) == -1) { - if (ThrowExceptP) { - TExcept::Throw(TStr::Fmt( - "Error copying file '%s' to '%s': cannot open source file for reading.", - SrcFNm.CStr(), DstFNm.CStr())); - } else { - return; - } - } - - - if( (output = open(DstFNm.CStr(), O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) { - close(input); - - if (ThrowExceptP) { - TExcept::Throw(TStr::Fmt( - "Error copying file '%s' to '%s': cannot open destination file for writing.", - SrcFNm.CStr(), DstFNm.CStr())); - } else { - return; - } - } - - - filesize = lseek(input, 0, SEEK_END); - lseek(output, filesize - 1, SEEK_SET); - write(output, '\0', 1); - - if((source = mmap(0, filesize, PROT_READ, MAP_SHARED, input, 0)) == (void *) -1) { - close(input); - close(output); - if (ThrowExceptP) { - TExcept::Throw(TStr::Fmt( - "Error copying file '%s' to '%s': cannot mmap input file.", - SrcFNm.CStr(), DstFNm.CStr())); - } else { - return; - } - } - - if((target = mmap(0, filesize, PROT_WRITE, MAP_SHARED, output, 0)) == (void *) -1) { - munmap(source, filesize); - close(input); - close(output); - if (ThrowExceptP) { - TExcept::Throw(TStr::Fmt( - "Error copying file '%s' to '%s': cannot mmap output file.", - SrcFNm.CStr(), DstFNm.CStr())); - } else { - return; - } - } - - memcpy(target, source, filesize); - - munmap(source, filesize); - munmap(target, filesize); - - close(input); - close(output); - -} - - - -#endif - -void TFile::Del(const TStr& FNm, const bool& ThrowExceptP){ - if (ThrowExceptP){ - EAssertR( - remove(FNm.CStr())==0, - "Error removing file '"+FNm+"'."); - } else { - remove(FNm.CStr()); - } -} - -void TFile::DelWc(const TStr& WcStr, const bool& RecurseDirP){ - // collect file-names - TStrV FNmV; - TFFile FFile(WcStr, RecurseDirP); TStr FNm; - while (FFile.Next(FNm)){ - FNmV.Add(FNm);} - // delete files - for (int FNmN=0; FNmN.#.txt --> ..txt - int Cnt=1; int ch; - TStr NewFNm; TStr TmpFNm=FNm; - if (FNm.SearchCh('#') == -1) { - for (ch = FNm.Len()-1; ch >= 0; ch--) if (FNm[ch] == '.') break; - if (ch != -1) TmpFNm.InsStr(ch, ".#"); - else TmpFNm += ".#"; - } - forever{ - NewFNm=TmpFNm; - NewFNm.ChangeStr("#", TStr::Fmt("%03d", Cnt)); Cnt++; - if (!TFile::Exists(NewFNm)){break;} - } - return NewFNm; -} - -#ifdef GLib_WIN - -uint64 TFile::GetSize(const TStr& FNm) { - // open - HANDLE hFile = CreateFile( - FNm.CStr(), // file to open - GENERIC_READ, // open for reading - FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading - NULL, // default security - OPEN_EXISTING, // existing file only - FILE_ATTRIBUTE_NORMAL, // normal file - NULL); // no attr. template - // check if we could open it - if (hFile == INVALID_HANDLE_VALUE) { - TExcept::Throw("Can not open file " + FNm + "!"); } - // read file times - LARGE_INTEGER lpFileSizeHigh; - if (!GetFileSizeEx(hFile, &lpFileSizeHigh)) { - TExcept::Throw("Can not read size of file " + FNm + "!"); } - // close file - CloseHandle(hFile); - // convert to uint64 - return uint64(lpFileSizeHigh.QuadPart); -} - -uint64 TFile::GetCreateTm(const TStr& FNm) { - // open - HANDLE hFile = CreateFile( - FNm.CStr(), // file to open - GENERIC_READ, // open for reading - FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading - NULL, // default security - OPEN_EXISTING, // existing file only - FILE_ATTRIBUTE_NORMAL, // normal file - NULL); // no attr. template - // check if we could open it - if (hFile == INVALID_HANDLE_VALUE) { - TExcept::Throw("Can not open file " + FNm + "!"); } - // read file times - FILETIME lpCreationTime; - if (!GetFileTime(hFile, &lpCreationTime, NULL, NULL)) { - TExcept::Throw("Can not read time from file " + FNm + "!"); } - // close file - CloseHandle(hFile); - // convert to uint64 - TUInt64 UInt64(uint(lpCreationTime.dwHighDateTime), - uint(lpCreationTime.dwLowDateTime)); - return UInt64.Val / uint64(10000); -} - -uint64 TFile::GetLastAccessTm(const TStr& FNm) { - // open - HANDLE hFile = CreateFile( - FNm.CStr(), // file to open - GENERIC_READ, // open for reading - FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading - NULL, // default security - OPEN_EXISTING, // existing file only - FILE_ATTRIBUTE_NORMAL, // normal file - NULL); // no attr. template - // check if we could open it - if (hFile == INVALID_HANDLE_VALUE) { - TExcept::Throw("Can not open file " + FNm + "!"); } - // read file times - FILETIME lpLastAccessTime; - if (!GetFileTime(hFile, NULL, &lpLastAccessTime, NULL)) { - TExcept::Throw("Can not read time from file " + FNm + "!"); } - // close file - CloseHandle(hFile); - // convert to uint64 - TUInt64 UInt64(uint(lpLastAccessTime.dwHighDateTime), - uint(lpLastAccessTime.dwLowDateTime)); - return UInt64.Val / uint64(10000); -} - -uint64 TFile::GetLastWriteTm(const TStr& FNm) { - // open - HANDLE hFile = CreateFile( - FNm.CStr(), // file to open - GENERIC_READ, // open for reading - FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading - NULL, // default security - OPEN_EXISTING, // existing file only - FILE_ATTRIBUTE_NORMAL, // normal file - NULL); // no attr. template - // check if we could open it - if (hFile == INVALID_HANDLE_VALUE) { - TExcept::Throw("Can not open file " + FNm + "!"); } - // read file times - FILETIME lpLastWriteTime; - if (!GetFileTime(hFile, NULL, NULL, &lpLastWriteTime)) { - TExcept::Throw("Can not read time from file " + FNm + "!"); } - // close file - CloseHandle(hFile); - // convert to uint64 - TUInt64 UInt64(uint(lpLastWriteTime.dwHighDateTime), - uint(lpLastWriteTime.dwLowDateTime)); - return UInt64.Val / uint64(10000); -} - -#elif defined(GLib_LINUX) - -uint64 TFile::GetSize(const TStr& FNm) { - Fail; return 0; -} - -uint64 TFile::GetCreateTm(const TStr& FNm) { - return GetLastWriteTm(FNm); -} - -uint64 TFile::GetLastWriteTm(const TStr& FNm) { - struct stat st; - if (stat(FNm.CStr(), &st) != 0) { - TExcept::Throw("Cannot read tile from file " + FNm + "!"); - } - return uint64(st.st_mtime); -} - - -#endif diff --git a/inst/include/glib-core/fl.h b/inst/include/glib-core/fl.h deleted file mode 100644 index 5d1d07d..0000000 --- a/inst/include/glib-core/fl.h +++ /dev/null @@ -1,660 +0,0 @@ -#include "bd.h" - -///////////////////////////////////////////////// -// Forward-Definitions -class TMem; -class TChA; -class TStr; - -///////////////////////////////////////////////// -// Check-Sum -class TCs{ -private: - static const int MxMask; - int Val; -public: - TCs(): Val(0){} - TCs(const TCs& Cs): Val(Cs.Val&MxMask){} - TCs(const int& Int): Val(Int&MxMask){} - - TCs& operator=(const TCs& Cs){Val=Cs.Val; return *this;} - bool operator==(const TCs& Cs) const {return Val==Cs.Val;} - TCs& operator+=(const TCs& Cs){Val=(Val+Cs.Val)&MxMask; return *this;} - TCs& operator+=(const char& Ch){Val=(Val+Ch)&MxMask; return *this;} - TCs& operator+=(const int& Int){Val=(Val+Int)&MxMask; return *this;} - int Get() const {return Val;} - - static TCs GetCsFromBf(char* Bf, const int& BfL); -}; - -///////////////////////////////////////////////// -// Output-stream-manipulator -class TSOutMnp { -public: - virtual TSOut& operator()(TSOut& SOut) const=0; - virtual ~TSOutMnp(); -}; - -///////////////////////////////////////////////// -// Stream-base -class TSBase{ -protected: - TCRef CRef; - TSStr SNm; - TCs Cs; -//protected: -// TSBase(); -// TSBase(const TSBase&); -// TSBase& operator=(const TSBase&); -public: - TSBase(const TSStr& Nm): SNm(Nm){} - virtual ~TSBase(){} - - virtual TStr GetSNm() const; -}; - -///////////////////////////////////////////////// -// Input-Stream -class TSIn: virtual public TSBase{ -private: - bool FastMode; -private: - TSIn(const TSIn&); - TSIn& operator=(const TSIn&); -public: - TSIn(): TSBase("Input-Stream"), FastMode(false){} - TSIn(const TStr& Str); - virtual ~TSIn(){} - - virtual bool Eof()=0; // if end-of-file - virtual int Len() const=0; // get number of bytes till eof - virtual char GetCh()=0; // get one char and advance - virtual char PeekCh()=0; // get one char and do NOT advance - virtual int GetBf(const void* Bf, const TSize& BfL)=0; // get BfL chars and advance - virtual bool GetNextLnBf(TChA& LnChA)=0; // get the next line and advance - virtual void Reset(){Fail;} - - bool IsFastMode() const {return FastMode;} - void SetFastMode(const bool& _FastMode){FastMode=_FastMode;} - - virtual void LoadCs(); - void LoadBf(const void* Bf, const TSize& BfL){Cs+=GetBf(Bf, BfL);} - void* LoadNewBf(const int& BfL){ - void* Bf=(void*)new char[BfL]; Cs+=GetBf(Bf, BfL); return Bf;} - void Load(bool& Bool){Cs+=GetBf(&Bool, sizeof(Bool));} - void Load(uchar& UCh){Cs+=GetBf(&UCh, sizeof(UCh));} - void Load(char& Ch){Cs+=GetBf(&Ch, sizeof(Ch));} - void Load(short& Short){Cs+=GetBf(&Short, sizeof(Short));} //J: - void Load(ushort& UShort){Cs+=GetBf(&UShort, sizeof(UShort));} //J: - void Load(int& Int){Cs+=GetBf(&Int, sizeof(Int));} - void Load(uint& UInt){Cs+=GetBf(&UInt, sizeof(UInt));} - void Load(int64& Int){Cs+=GetBf(&Int, sizeof(Int));} - void Load(uint64& UInt){Cs+=GetBf(&UInt, sizeof(UInt));} - void Load(double& Flt){Cs+=GetBf(&Flt, sizeof(Flt));} - void Load(sdouble& SFlt){Cs+=GetBf(&SFlt, sizeof(SFlt));} - void Load(ldouble& LFlt){Cs+=GetBf(&LFlt, sizeof(LFlt));} - void Load(char*& CStr, const int& MxCStrLen, const int& CStrLen){ - CStr=new char[MxCStrLen+1]; Cs+=GetBf(CStr, CStrLen+1);} - void Load(char*& CStr); - - TSIn& operator>>(bool& Bool){Cs+=GetBf(&Bool, sizeof(Bool)); return *this;} - TSIn& operator>>(uchar& UCh){Cs+=GetBf(&UCh, sizeof(UCh)); return *this;} - TSIn& operator>>(char& Ch){Cs+=GetBf(&Ch, sizeof(Ch)); return *this;} - TSIn& operator>>(short& Sh){Cs+=GetBf(&Sh, sizeof(Sh)); return *this;} - TSIn& operator>>(ushort& USh){Cs+=GetBf(&USh, sizeof(USh)); return *this;} - TSIn& operator>>(int& Int){Cs+=GetBf(&Int, sizeof(Int)); return *this;} - TSIn& operator>>(uint& UInt){Cs+=GetBf(&UInt, sizeof(UInt)); return *this;} - TSIn& operator>>(int64& Int){Cs+=GetBf(&Int, sizeof(Int)); return *this;} - TSIn& operator>>(uint64& UInt){Cs+=GetBf(&UInt, sizeof(UInt)); return *this;} - TSIn& operator>>(float& Flt){Cs+=GetBf(&Flt, sizeof(Flt)); return *this;} - TSIn& operator>>(double& Double){Cs+=GetBf(&Double, sizeof(Double)); return *this;} - TSIn& operator>>(long double& LDouble){Cs+=GetBf(&LDouble, sizeof(LDouble)); return *this;} - - bool GetNextLn(TStr& LnStr); - bool GetNextLn(TChA& LnChA); - - static const TPt StdIn; - friend class TPt; -}; -typedef TPt PSIn; - -template -TSIn& operator>>(TSIn& SIn, T& Val) { - Val.Load(SIn); return SIn; -} - -///////////////////////////////////////////////// -// Output-Stream -class TSOut: virtual public TSBase{ -private: - int MxLnLen, LnLen; - int UpdateLnLen(const int& StrLen, const bool& ForceInLn=false); -private: - TSOut(const TSIn&); - TSOut& operator = (const TSOut&); -public: - TSOut(): TSBase("Output-Stream"), MxLnLen(-1), LnLen(0){} - TSOut(const TStr& Str); - virtual ~TSOut(){} - - void EnableLnTrunc(const int& _MxLnLen){MxLnLen=_MxLnLen;} - void DisableLnTrunc(){MxLnLen=-1;} - - virtual int PutCh(const char& Ch)=0; - virtual int PutBf(const void* LBf, const TSize& LBfL)=0; - virtual void Flush()=0; - virtual TFileId GetFileId() const {return NULL;} - - int PutMem(const TMem& Mem); - int PutCh(const char& Ch, const int& Chs); - int PutBool(const bool& Bool); - int PutInt(const int& Int); - int PutInt(const int& Int, const char* FmtStr); - int PutUInt(const uint& Int); - int PutUInt(const uint& Int, const char* FmtStr); - int PutFlt(const double& Flt); - int PutFlt(const double& Flt, const char* FmtStr); - int PutStr(const char* CStr); - int PutStr(const TChA& ChA); - int PutStr(const TStr& Str, const char* FmtStr); - int PutStr(const TStr& Str, const bool& ForceInLn=false); - int PutStrLn(const TStr& Str, const bool& ForceInLn=false){ - int Cs=PutStr(Str,ForceInLn); Cs+=PutLn(); return Cs;} - int PutStrFmt(const char *FmtStr, ...); - int PutStrFmtLn(const char *FmtStr, ...); - int PutIndent(const int& IndentLev=1); - int PutLn(const int& Lns=1); - int PutDosLn(const int& Lns=1); - int PutSep(const int& NextStrLen=0); - int PutSepLn(const int& Lns=0); - - void SaveCs(){Cs+=PutBf(&Cs, sizeof(Cs));} - void SaveBf(const void* Bf, const TSize& BfL){Cs+=PutBf(Bf, BfL);} - void Save(const bool& Bool){Cs+=PutBf(&Bool, sizeof(Bool));} - void Save(const char& Ch){Cs+=PutBf(&Ch, sizeof(Ch));} - void Save(const uchar& UCh){Cs+=PutBf(&UCh, sizeof(UCh));} - void Save(const short& Short){Cs+=PutBf(&Short, sizeof(Short));} - void Save(const ushort& UShort){Cs+=PutBf(&UShort, sizeof(UShort));} - void Save(const int& Int){Cs+=PutBf(&Int, sizeof(Int));} - void Save(const uint& UInt){Cs+=PutBf(&UInt, sizeof(UInt));} - void Save(const int64& Int){Cs+=PutBf(&Int, sizeof(Int));} - void Save(const uint64& UInt){Cs+=PutBf(&UInt, sizeof(UInt));} - void Save(const double& Flt){Cs+=PutBf(&Flt, sizeof(Flt));} - void Save(const sdouble& SFlt){Cs+=PutBf(&SFlt, sizeof(SFlt));} - void Save(const ldouble& LFlt){Cs+=PutBf(&LFlt, sizeof(LFlt));} - void Save(const char* CStr, const TSize& CStrLen){Cs+=PutBf(CStr, CStrLen+1);} - void Save(const char* CStr); - void Save(TSIn& SIn, const TSize& BfL=-1); - void Save(const PSIn& SIn, const TSize& BfL=-1){Save(*SIn, BfL);} - void Save(const void* Bf, const TSize& BfL){Cs+=PutBf(Bf, BfL);} - - TSOut& operator<<(const bool& Bool){Cs+=PutBf(&Bool, sizeof(Bool)); return *this;} - TSOut& operator<<(const uchar& UCh){Cs+=PutBf(&UCh, sizeof(UCh)); return *this;} - TSOut& operator<<(const char& Ch){Cs+=PutBf(&Ch, sizeof(Ch)); return *this;} - TSOut& operator<<(const short& Sh){Cs+=PutBf(&Sh, sizeof(Sh)); return *this;} - TSOut& operator<<(const ushort& USh){Cs+=PutBf(&USh, sizeof(USh)); return *this;} - TSOut& operator<<(const int& Int){Cs+=PutBf(&Int, sizeof(Int)); return *this;} - TSOut& operator<<(const uint& Int){Cs+=PutBf(&Int, sizeof(Int)); return *this;} - TSOut& operator<<(const int64& Int){Cs+=PutBf(&Int, sizeof(Int)); return *this;} - TSOut& operator<<(const uint64& UInt){Cs+=PutBf(&UInt, sizeof(UInt)); return *this;} - TSOut& operator<<(const float& Flt){Cs+=PutBf(&Flt, sizeof(Flt)); return *this;} - TSOut& operator<<(const double& Double){Cs+=PutBf(&Double, sizeof(Double)); return *this;} - TSOut& operator<<(const long double& LDouble){Cs+=PutBf(&LDouble, sizeof(LDouble)); return *this;} - TSOut& operator<<(const TSOutMnp& Mnp){return Mnp(*this);} - TSOut& operator<<(TSOut&(*FuncPt)(TSOut&)){return FuncPt(*this);} - TSOut& operator<<(TSIn& SIn); - TSOut& operator<<(PSIn& SIn){return operator<<(*SIn);} - - static const TPt StdOut; - friend class TPt; -}; -typedef TPt PSOut; - -template -TSOut& operator<<(TSOut& SOut, const T& Val){ - Val.Save(SOut); return SOut; -} - -///////////////////////////////////////////////// -// Input-Output-Stream-Base -class TSInOut: public TSIn, public TSOut{ -private: - TSInOut(const TSInOut&); - TSInOut& operator=(const TSInOut&); -public: - TSInOut(): TSBase("Input-Output-Stream"), TSIn(), TSOut() {} - virtual ~TSInOut(){} - - virtual void SetPos(const int& Pos)=0; - virtual void MovePos(const int& DPos)=0; - virtual int GetPos() const=0; - virtual int GetSize() const=0; // size of whole stream - virtual void Clr()=0; // clear IO buffer - - friend class TPt; -}; -typedef TPt PSInOut; - -///////////////////////////////////////////////// -// Standard-Input -class TStdIn: public TSIn{ -private: - TStdIn(const TStdIn&); - TStdIn& operator=(const TStdIn&); -public: - TStdIn(); - static TPt New(){return new TStdIn();} - - bool Eof(){return feof(stdin)!=0;} - int Len() const {return -1;} - char GetCh(){return char(getchar());} - char PeekCh(){ - int Ch=getchar(); ungetc(Ch, stdin); return char(Ch);} - int GetBf(const void* LBf, const TSize& LBfL); - void Reset(){Cs=TCs();} - bool GetNextLnBf(TChA& LnChA); -}; - -///////////////////////////////////////////////// -// Standard-Output -class TStdOut: public TSOut{ -private: - TStdOut(const TStdOut&); - TStdOut& operator=(const TStdOut&); -public: - TStdOut(); - static TPt New(){return new TStdOut();} - - int PutCh(const char& Ch){putchar(Ch); return Ch;} - int PutBf(const void *LBf, const TSize& LBfL); - void Flush(){fflush(stdout);} -}; - -///////////////////////////////////////////////// -// Input-File -class TFIn: public TSIn{ -private: - static const int MxBfL; - TFileId FileId; - char* Bf; - int BfC, BfL; -private: - void SetFPos(const int& FPos) const; - int GetFPos() const; - int GetFLen() const; - void FillBf(); - int FindEol(int& BfN, bool& CrEnd); -private: - TFIn(); - TFIn(const TFIn&); - TFIn& operator=(const TFIn&); -public: - TFIn(const TStr& FNm); - TFIn(const TStr& FNm, bool& OpenedP); - static PSIn New(const TStr& FNm); - static PSIn New(const TStr& FNm, bool& OpenedP); - ~TFIn(); - - bool Eof(){ - if ((BfC==BfL)&&(BfL==MxBfL)){FillBf();} - return (BfC==BfL)&&(BfL New(const TStr& Str, bool FromFile); - //static TPt New(const TStr& Str, uint64); - - ~TMIn(); - - bool Eof(){return BfC==BfL;} - int Len() const {return static_cast(BfL-BfC);} - char GetCh(); - char PeekCh(); - int GetBf(const void* LBf, const TSize& LBfL); - void Reset(){Cs=TCs(); BfC=0;} - bool GetNextLnBf(TChA& LnChA); - - uint64 GetBfC(); - uint64 GetBfL(); - void SetBfC(uint64 Pos); - - /// Finds number of new line chars in interval [Lb, Ub) - uint64 CountNewLinesInRange(uint64 Lb, uint64 Ub); - /// Finds beginning of line in which Ind is present - uint64 GetLineStartPos(uint64 Ind); - /// Finds end of line in which Ind is present - uint64 GetLineEndPos(uint64 Ind); - char* GetLine(uint64 Ind); - /// Move stream pointer along until a non commented line is found - void SkipCommentLines(); - - char* GetBfAddr(){return Bf;} - - friend class TPt; -}; - -typedef TPt PMIn; - -///////////////////////////////////////////////// -// Output-Memory -class TMOut: public TSOut{ -private: - char* Bf; - int BfL, MxBfL; - bool OwnBf; - void Resize(const int& ReqLen = -1); -private: - TMOut(const TMOut&); - TMOut& operator=(const TMOut&); -public: - TMOut(const int& _MxBfL=1024); - static PSOut New(const int& MxBfL=1024){ - return PSOut(new TMOut(MxBfL));} - TMOut(char* _Bf, const int& _MxBfL); - ~TMOut(){if (OwnBf&&(Bf!=NULL)){delete[] Bf;}} - - int PutCh(const char& Ch){if (BfL==MxBfL){ - Resize();} return Bf[BfL++]=Ch;} - int PutBf(const void* LBf, const TSize& LBfL); - void AppendBf(const void* LBf, const TSize& LBfL); - void Flush(){} - - int Len() const {return BfL;} - void Clr(){BfL=0;} - char GetCh(const int& ChN) const { - IAssert((0<=ChN)&&(ChNEof()){return Ch=EofCh;} else {return Ch=SIn->GetCh();}} - char operator()(){return Ch;} -}; - -///////////////////////////////////////////////// -// Line-Returner -// J: after talking to BlazF -- can be removed from GLib -class TLnRet{ -private: - PSIn SIn; - UndefDefaultCopyAssign(TLnRet); -public: - TLnRet(const PSIn& _SIn): SIn(_SIn) {} - - bool NextLn(TStr& LnStr); -}; - -///////////////////////////////////////////////// -// Random-Access-File -ClassTP(TFRnd, PFRnd)//{ -private: - TFileId FileId; - TSStr FNm; - bool RecAct; - int HdLen, RecLen; -private: - void RefreshFPos(); -private: - TFRnd(const TFRnd&); - TFRnd& operator=(const TFRnd&); -public: - TFRnd(const TStr& _FNm, const TFAccess& FAccess, - const bool& CreateIfNo=true, const int& _HdLen=-1, const int& _RecLen=-1); - static PFRnd New(const TStr& FNm, - const TFAccess& FAccess, const bool& CreateIfNo=true, - const int& HdLen=-1, const int& RecLen=-1){ - return new TFRnd(FNm, FAccess, CreateIfNo, HdLen, RecLen);} - ~TFRnd(); - - TStr GetFNm() const; - void SetHdRecLen(const int& _HdLen, const int& _RecLen){ - HdLen=_HdLen; RecLen=_RecLen; RecAct=(HdLen>=0)&&(RecLen>0);} - - void SetFPos(const int& FPos); - void MoveFPos(const int& DFPos); - int GetFPos(); - int GetFLen(); - bool Empty(){return GetFLen()==0;} - bool Eof(){return GetFPos()==GetFLen();} - - void SetRecN(const int& RecN); - int GetRecN(); - int GetRecs(); - - void GetBf(void* Bf, const TSize& BfL); - void PutBf(const void* Bf, const TSize& BfL); - void Flush(); - - void GetHd(void* Hd){IAssert(RecAct); - int FPos=GetFPos(); SetFPos(0); GetBf(Hd, HdLen); SetFPos(FPos);} - void PutHd(const void* Hd){IAssert(RecAct); - int FPos=GetFPos(); SetFPos(0); PutBf(Hd, HdLen); SetFPos(FPos);} - void GetRec(void* Rec, const int& RecN=-1){ - IAssert(RecAct); if (RecN!=-1){SetRecN(RecN);} GetBf(Rec, RecLen);} - void PutRec(const void* Rec, const int& RecN=-1){ - IAssert(RecAct); if (RecN!=-1){SetRecN(RecN);} PutBf(Rec, RecLen);} - - void PutCs(const TCs& Cs){PutBf(&Cs, sizeof(Cs));} - TCs GetCs(){TCs Cs; GetBf(&Cs, sizeof(Cs)); return Cs;} - void PutCh(const char& Ch){PutBf(&Ch, sizeof(Ch));} - void PutCh(const char& Ch, const int& Chs); - char GetCh(){char Ch; GetBf(&Ch, sizeof(Ch)); return Ch;} - void PutUCh(const uchar& UCh){PutBf(&UCh, sizeof(UCh));} - uchar GetUCh(){uchar UCh; GetBf(&UCh, sizeof(UCh)); return UCh;} - void PutInt(const int& Int){PutBf(&Int, sizeof(Int));} - int GetInt(){int Int; GetBf(&Int, sizeof(Int)); return Int;} - void PutUInt(const uint& UInt){PutBf(&UInt, sizeof(UInt));} - uint GetUInt(){uint UInt; GetBf(&UInt, sizeof(UInt)); return UInt;} - void PutStr(const TStr& Str); - TStr GetStr(const int& StrLen); - TStr GetStr(const int& MxStrLen, bool& IsOk); - void PutSIn(const PSIn& SIn, TCs& Cs); - PSIn GetSIn(const int& SInLen, TCs& Cs); - - static TStr GetStrFromFAccess(const TFAccess& FAccess); - static TFAccess GetFAccessFromStr(const TStr& Str); -}; - -///////////////////////////////////////////////// -// Files -class TFile{ -public: - static const TStr TxtFExt; - static const TStr HtmlFExt; - static const TStr HtmFExt; - static const TStr GifFExt; - static const TStr JarFExt; -public: - static bool Exists(const TStr& FNm); - static void Copy(const TStr& SrcFNm, const TStr& DstFNm, - const bool& ThrowExceptP=true, const bool& FailIfExistsP=false); - static void Del(const TStr& FNm, const bool& ThrowExceptP=true); - static void DelWc(const TStr& WcStr, const bool& RecurseDirP=false); - static void Rename(const TStr& SrcFNm, const TStr& DstFNm); - static TStr GetUniqueFNm(const TStr& FNm); - static uint64 GetSize(const TStr& FNm); - static uint64 GetCreateTm(const TStr& FNm); - static uint64 GetLastAccessTm(const TStr& FNm); - static uint64 GetLastWriteTm(const TStr& FNm); -}; - diff --git a/inst/include/glib-core/gnuplot.cpp b/inst/include/glib-core/gnuplot.cpp deleted file mode 100644 index 4cfa520..0000000 --- a/inst/include/glib-core/gnuplot.cpp +++ /dev/null @@ -1,894 +0,0 @@ -#include "stdafx.h" -#include "gnuplot.h" - -///////////////////////////////////////////////// -// GNU-Plot-Chart - -// Set path to gnuplot -#if defined(GLib_WIN) - TStr TGnuPlot::GnuPlotPath = "C:\\gnuplot"; - TStr TGnuPlot::GnuPlotFNm = "wgnuplot.exe"; -#elif defined(GLib_CYGWIN) - TStr TGnuPlot::GnuPlotPath = "/usr/bin"; - TStr TGnuPlot::GnuPlotFNm = "gnuplot.exe"; -#elif defined(GLib_MACOSX) - TStr TGnuPlot::GnuPlotPath = "/usr/local/bin"; - TStr TGnuPlot::GnuPlotFNm = "gnuplot"; -#else - TStr TGnuPlot::GnuPlotPath = "/usr/bin"; - TStr TGnuPlot::GnuPlotFNm = "gnuplot"; -#endif - -// Determines the gnuplot version and the tics command syntax. -// Gnuplot changed the syntax with version 4.2: -// - before 4.2: set ticscale 2 1 -// - 4.2 and later: set tics 2 -int TGnuPlot::GetTics42() { -#ifdef GLib_WIN - return -1; -#else - FILE* p; - char Buf[1024]; - char Version[1024]; - size_t n; - - // get gnuplot version - p = popen(TStr::Fmt("%s -V", TGnuPlot::GnuPlotFNm.CStr()).CStr(), "r"); - if (p == NULL) { // try running using the path - p = popen(TStr::Fmt("%s/%s -V", TGnuPlot::GnuPlotPath.CStr(), TGnuPlot::GnuPlotFNm.CStr()).CStr(), "r"); - if (p == NULL) { return -1; } - } - n = fread(Buf, 1, 100, p); - if (n <= 0) { return -1; } - Buf[n] = '\0'; - pclose(p); - //printf("Buf %d .%s.\n", n, Buf); - n = sscanf(Buf, "gnuplot %s", Version); - if (n <= 0) { return -1; } - // printf("Version %d .%s.\n", n, Version); - if ((strlen(Version) < 3) || (Version[1] != '.')) { return -1; } - // test version < 4.2 - if ((Version[0] < '4') || ((Version[0] == '4') && (Version[2] < '2'))) { - // printf("TGnuPlot::GetTics42 0\n"); - return 0; - } - // printf("TGnuPlot::GetTics42 1\n"); - return 1; -#endif -} - -int TGnuPlot::Tics42 = -2; -TStr TGnuPlot::DefPlotFNm = "GnuPlot.plt"; -TStr TGnuPlot::DefDataFNm = "GnuPlot.tab"; - -TGnuPlot::TGpSeries::TGpSeries(const TGnuPlot::TGpSeries& Gps) : - SeriesTy(Gps.SeriesTy), XYValV(Gps.XYValV), ZValV(Gps.ZValV), - Label(Gps.Label), WithStyle(Gps.WithStyle), DataFNm(Gps.DataFNm), - XCol(Gps.XCol), YCol(Gps.YCol), ZCol(Gps.ZCol) { -} - -TGnuPlot::TGpSeries& TGnuPlot::TGpSeries::operator = (const TGnuPlot::TGpSeries& Gps) { - if(this != &Gps) { - SeriesTy = Gps.SeriesTy; - XYValV = Gps.XYValV; ZValV = Gps.ZValV; - Label = Gps.Label; - DataFNm = Gps.DataFNm; - WithStyle = Gps.WithStyle; - XCol = Gps.XCol; YCol = Gps.YCol; ZCol = Gps.ZCol; - } - return *this; -} - -bool TGnuPlot::TGpSeries::operator < (const TGpSeries& Gps) const { - return (XYValV < Gps.XYValV) || ((XYValV == Gps.XYValV) && (Label < Gps.Label)); -} - -TGnuPlot::TGnuPlot(const TStr& FileNm, const TStr& PlotTitle, const bool& Grid) : - DataFNm(FileNm+".tab"), PlotFNm(FileNm+".plt"), Title(PlotTitle), LblX(), LblY(), ScaleTy(gpsAuto), - YRange(0, 0), XRange(0, 0), SetGrid(Grid), SetPause(true), - SeriesV(), MoreCmds() { - IAssert(! FileNm.Empty()); -} - -TGnuPlot::TGnuPlot(const TStr& DataFileNm, const TStr& PlotFileNm, const TStr& PlotTitle, const bool& Grid) : - DataFNm(DataFileNm.Empty() ? DefDataFNm : DataFileNm), - PlotFNm(PlotFileNm.Empty() ? DefPlotFNm : PlotFileNm), - Title(PlotTitle), LblX(), LblY(), ScaleTy(gpsAuto), - YRange(0, 0), XRange(0, 0), SetGrid(Grid), SetPause(true), SeriesV(), MoreCmds() { -} - -TGnuPlot::TGnuPlot(const TGnuPlot& GnuPlot) : DataFNm(GnuPlot.DataFNm), PlotFNm(GnuPlot.PlotFNm), - Title(GnuPlot.Title), LblX(GnuPlot.LblX), LblY(GnuPlot.LblY), ScaleTy(GnuPlot.ScaleTy), YRange(GnuPlot.YRange), - XRange(GnuPlot.XRange), SetGrid(GnuPlot.SetGrid), SetPause(GnuPlot.SetPause), SeriesV(GnuPlot.SeriesV), - MoreCmds(GnuPlot.MoreCmds) { -} - -TGnuPlot& TGnuPlot::operator = (const TGnuPlot& GnuPlot) { - if (this != &GnuPlot) { - DataFNm = GnuPlot.DataFNm; - PlotFNm = GnuPlot.PlotFNm; - Title = GnuPlot.Title; - LblX = GnuPlot.LblX; - LblY = GnuPlot.LblY; - ScaleTy = GnuPlot.ScaleTy; - YRange = GnuPlot.YRange; - XRange = GnuPlot.XRange; - SetGrid = GnuPlot.SetGrid; - SetPause = GnuPlot.SetPause; - SeriesV = GnuPlot.SeriesV; - MoreCmds = GnuPlot.MoreCmds; - } - return *this; -} - -TStr TGnuPlot::GetSeriesPlotStr(const int& SeriesId) { - TChA PlotStr; - TGpSeries& Series = SeriesV[SeriesId]; - if (SeriesId != 0) PlotStr += ",\\\n\t"; - if (Series.XCol >= 0) { - PlotStr += "\"" + Series.DataFNm + "\" using " + TInt::GetStr(Series.XCol); - if (Series.YCol != 0) { PlotStr += ":" + TInt::GetStr(Series.YCol); } - if (Series.ZCol != 0) { PlotStr += ":" + TInt::GetStr(Series.ZCol); } - else if (Series.SeriesTy==gpwFilledCurves) { PlotStr += ":(0)"; } // filled curves requres 3rd column - } else { - // function - //IAssertR(Series.DataFNm.SearchCh('=') != -1, TStr::Fmt("Expression %s is not a function", Series.DataFNm.CStr())); - PlotStr += Series.DataFNm; - } - if (Series.SeriesTy == gpwErrBars) { - PlotStr += " notitle"; - } else { - PlotStr += " title \"" + Series.Label + "\""; - } - // hard coded line style - if (Series.WithStyle.Empty()) { - if (Series.SeriesTy == gpwLines) Series.WithStyle = "lw 1"; - if (Series.SeriesTy == gpwPoints) Series.WithStyle = "pt 6"; // circles - if (Series.SeriesTy == gpwLinesPoints) Series.WithStyle = "pt 6"; // circles - if (Series.SeriesTy == gpwBoxes) Series.WithStyle = "fill solid 0.3"; - } - PlotStr += " with " + GetSeriesTyStr(Series.SeriesTy) + " " + Series.WithStyle; - return PlotStr; -} - -//GP.AddFunc("2*x**-2+4", gpwLines, "2*x^-2+4"); -int TGnuPlot::AddFunc(const TStr& FuncStr, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - const int Id = SeriesV.Len(); - TGpSeries Plot; - Plot.SeriesTy = SeriesTy; - Plot.Label = Label; - if (! FuncStr.Empty()) { Plot.DataFNm = TStr::Fmt("f%d(x)=%s, f%d(x)", Id, FuncStr.CStr(), Id); } - else { Plot.DataFNm = TStr::Fmt("f%d(x)", Id); } - Plot.XCol = -1; - Plot.WithStyle = Style; - SeriesV.Add(Plot); - return Id; -} - -int TGnuPlot::AddPlot(const TStr& DataFNm, const int& ColY, - const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - return AddPlot(DataFNm, 0, ColY, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TStr& DataFNm, const int& ColX, const int& ColY, - const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - IAssert(ColY > 0); IAssert(ColX >= 0); - TGpSeries Plot; - Plot.SeriesTy = SeriesTy; - Plot.Label = Label; - Plot.DataFNm = DataFNm; Plot.DataFNm.ChangeStrAll("\\", "\\\\"); - Plot.XCol = ColX; Plot.YCol = ColY; Plot.ZCol = 0; - Plot.WithStyle = Style; - SeriesV.Add(Plot); - return SeriesV.Len() - 1; -} - -int TGnuPlot::AddPlot(const TIntV& YValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - TFltKdV XYValV(YValV.Len(), 0); - for (int i = 0; i < YValV.Len(); i++) { - XYValV.Add(TFltKd(TFlt(i+1), TFlt(YValV[i]))); - } - return AddPlot(XYValV, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TFltV& YValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - TFltKdV XYValV(YValV.Len(), 0); - for (int i = 0; i < YValV.Len(); i++) { - XYValV.Add(TFltKd(TFlt(i+1), TFlt(YValV[i]))); - } - return AddPlot(XYValV, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TFltV& XValV, const TFltV& YValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - IAssert(XValV.Len() == YValV.Len()); - TFltKdV XYValV(XValV.Len(), 0); - for (int i = 0; i < YValV.Len(); i++) { - XYValV.Add(TFltKd(TFlt(XValV[i]), TFlt(YValV[i]))); - } - return AddPlot(XYValV, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TIntPrV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - TFltKdV XYFltValV(XYValV.Len(), 0); - for (int i = 0; i < XYValV.Len(); i++) { - XYFltValV.Add(TFltKd(TFlt(XYValV[i].Val1), TFlt(XYValV[i].Val2))); - } - return AddPlot(XYFltValV, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TFltPrV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - TFltKdV XYFltValV(XYValV.Len(), 0); - for (int i = 0; i < XYValV.Len(); i++) { - XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); - } - return AddPlot(XYFltValV, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TIntKdV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - TFltKdV XYFltValV(XYValV.Len(), 0); - for (int i = 0; i < XYValV.Len(); i++) { - XYFltValV.Add(TFltKd(TFlt(XYValV[i].Key), TFlt(XYValV[i].Dat))); - } - return AddPlot(XYFltValV, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TIntFltKdV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - TFltKdV XYFltValV(XYValV.Len(), 0); - for (int i = 0; i < XYValV.Len(); i++) { - XYFltValV.Add(TFltKd(TFlt(XYValV[i].Key), TFlt(XYValV[i].Dat))); - } - return AddPlot(XYFltValV, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TIntFltPrV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - TFltKdV XYFltValV(XYValV.Len(), 0); - for (int i = 0; i < XYValV.Len(); i++) { - XYFltValV.Add(TFltKd(TFlt(XYValV[i].Val1), TFlt(XYValV[i].Val2))); - } - return AddPlot(XYFltValV, SeriesTy, Label, Style); -} - -int TGnuPlot::AddPlot(const TFltKdV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { - if (XYValV.Empty()) { - printf("***AddPlot: empty plot (%s) %s\n", DataFNm.CStr(), Title.CStr()); - return -1; - } - TGpSeries Plot; - Plot.SeriesTy = SeriesTy; - Plot.Label = Label; - Plot.XYValV = XYValV; - Plot.WithStyle = Style; - SeriesV.Add(Plot); - return SeriesV.Len() - 1; -} - -int TGnuPlot::AddErrBar(const TFltTrV& XYDValV, const TStr& Label) { - TFltKdV XYFltValV(XYDValV.Len(), 0); - TFltV DeltaV(XYDValV.Len(), 0); - for (int i = 0; i < XYDValV.Len(); i++) { - XYFltValV.Add(TFltKd(XYDValV[i].Val1, XYDValV[i].Val2)); - DeltaV.Add(XYDValV[i].Val3); - } - return AddErrBar(XYFltValV, DeltaV, Label); -} - -int TGnuPlot::AddErrBar(const TFltTrV& XYDValV, const TStr& DatLabel, const TStr& ErrLabel) { - TFltKdV XYFltValV(XYDValV.Len(), 0); - TFltV DeltaV(XYDValV.Len(), 0); - for (int i = 0; i < XYDValV.Len(); i++) { - XYFltValV.Add(TFltKd(XYDValV[i].Val1, XYDValV[i].Val2)); - DeltaV.Add(XYDValV[i].Val3); - } - const int PlotId = AddPlot(XYFltValV, gpwLinesPoints, DatLabel); - AddErrBar(XYFltValV, DeltaV, ErrLabel); - return PlotId; -} - -int TGnuPlot::AddErrBar(const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label) { - IAssert(YValV.Len() == DeltaYV.Len()); - TFltKdV XYFltValV(YValV.Len(), 0); - for (int i = 0; i < YValV.Len(); i++) { - XYFltValV.Add(TFltKd(TFlt(i+1), YValV[i])); - } - return AddErrBar(XYFltValV, DeltaYV, Label); -} - -int TGnuPlot::AddErrBar(const TFltV& XValV, const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label) { - IAssert(XValV.Len() == YValV.Len()); - IAssert(XValV.Len() == DeltaYV.Len()); - TFltKdV XYFltValV(XValV.Len(), 0); - for (int i = 0; i < XValV.Len(); i++) { - XYFltValV.Add(TFltKd(XValV[i], YValV[i])); - } - return AddErrBar(XYFltValV, DeltaYV, Label); -} - -int TGnuPlot::AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& Label) { - TFltKdV XYFltValV(XYValV.Len(), 0); - for (int i = 0; i < XYValV.Len(); i++) { - XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); - } - return AddErrBar(XYFltValV, DeltaYV, Label); -} - -int TGnuPlot::AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaV, const TStr& DatLabel, const TStr& ErrLabel) { - TFltKdV XYFltValV(XYValV.Len(), 0); - for (int i = 0; i < XYValV.Len(); i++) { - XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); - } - const int PlotId = AddPlot(XYFltValV, gpwLinesPoints, DatLabel); - AddErrBar(XYFltValV, DeltaV, ErrLabel); - return PlotId; -} - -int TGnuPlot::AddErrBar(const TFltKdV& XYValV, const TFltV& DeltaYV, const TStr& Label) { - if (XYValV.Empty()) { - printf("***AddErrBar: empty plot (%s) %s\n", DataFNm.CStr(), Title.CStr()); - return -1; - } - IAssert(XYValV.Len() == DeltaYV.Len()); - TGpSeries Plot; - Plot.SeriesTy = gpwErrBars; - Plot.Label = Label; - Plot.XYValV = XYValV; - Plot.ZValV = DeltaYV; - SeriesV.Add(Plot); - return SeriesV.Len() - 1; -} - -int TGnuPlot::AddLinFit(const int& PlotId, const TGpSeriesTy& SeriesTy, const TStr& Style) { - if (PlotId < 0 || PlotId >= SeriesV.Len()) return -1; - const TGpSeries& Plot = SeriesV[PlotId]; - if(Plot.XYValV.Empty()) return -1; - const TFltKdV& XY = Plot.XYValV; - double A, B, R2, SigA, SigB, Chi2; - // linear fit - TFltPrV XYPr; - int s; - for (s = 0; s < XY.Len(); s++) { - XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat)); - } - TSpecFunc::LinearFit(XYPr, A, B, SigA, SigB, Chi2, R2); - TStr StyleStr=Style; - if (StyleStr.Empty()) { StyleStr = "linewidth 3"; } - const int FitId = AddFunc(TStr::Fmt("%f+%f*x", A, B), - SeriesTy, TStr::Fmt("%.4g + %.4g x R^2:%.2g", A, B, R2), StyleStr); - return FitId; - /*SeriesV.Add(); - TGpSeries& NewPlot = SeriesV.Last(); - TFltKdV& EstXY = NewPlot.XYValV; - for (s = 0; s < XY.Len(); s++) { - EstXY.Add(TFltKd(XY[s].Key, A + B*XYPr[s].Val1)); - } - NewPlot.Label = TStr::Fmt("%.4g + %.4g x R^2:%.2g", A, B, R2); - NewPlot.SeriesTy = SeriesTy; - if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; } - else { NewPlot.WithStyle = Style; } - return SeriesV.Len() - 1;*/ -} - -int TGnuPlot::AddPwrFit(const int& PlotId, const TGpSeriesTy& SeriesTy, const TStr& Style) { - const int PlotId1 = AddPwrFit3(PlotId, SeriesTy); - AddPwrFit2(PlotId, SeriesTy, 5.0); - return PlotId1; -} - -// linear fit on log-log scales{% -int TGnuPlot::AddPwrFit1(const int& PlotId, const TGpSeriesTy& SeriesTy, const TStr& Style) { - if (PlotId < 0 || PlotId >= SeriesV.Len()) return -1; - const TGpSeries& Plot = SeriesV[PlotId]; - if(Plot.XYValV.Empty()) return -1; - const TFltKdV& XY = Plot.XYValV; - double A, B, R2, SigA, SigB, Chi2, MinY = TFlt::Mx, MinX = TFlt::Mx; - // power fit - TFltPrV XYPr; - int s; - for (s = 0; s < XY.Len(); s++) { - if (XY[s].Key > 0) { - XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat)); //!!! skip zero values - MinX = TMath::Mn(MinX, XY[s].Key()); - MinY = TMath::Mn(MinY, XY[s].Dat()); - } - } - MinY = TMath::Mn(1.0, MinY); - TSpecFunc::PowerFit(XYPr, A, B, SigA, SigB, Chi2, R2); - TStr StyleStr=Style; - if (StyleStr.Empty()) { StyleStr = "linewidth 3"; } - const int FitId = AddFunc(TStr::Fmt("%f*x**%f", A, B), - SeriesTy, TStr::Fmt("%.1g * x^{%.4g} R^2:%.2g", A, B, R2), StyleStr); - return FitId; - /*SeriesV.Add(); - TGpSeries& NewPlot = SeriesV.Last(); - const int FitId = SeriesV.Len() - 1; - NewPlot.DataFNm = ; - TFltKdV& EstXY = NewPlot.XYValV; - for (s = 0; s < XYPr.Len(); s++) { - const double YVal = A*pow(XYPr[s].Val1(), B); - if (YVal < MinY) continue; - EstXY.Add(TFltKd(XYPr[s].Val1, YVal)); - } - NewPlot.Label = ; - NewPlot.SeriesTy = SeriesTy; - if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; } - else { NewPlot.WithStyle = Style; } - //if (MinX < 5.0) MinX = 5.0; - //AddPwrFit2(PlotId, SeriesTy, MinX);*/ -} - -// MLE power-coefficient -int TGnuPlot::AddPwrFit2(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style) { - const TGpSeries& Plot = SeriesV[PlotId]; - if(Plot.XYValV.Empty()) return -1; - const TFltKdV& XY = Plot.XYValV; - // power fit - TFltPrV XYPr; - double MinY = TFlt::Mx; - for (int s = 0; s < XY.Len(); s++) { - if (XY[s].Key > 0.0) { - XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat)); - MinY = TMath::Mn(MinY, XY[s].Dat()); - } - } - if (XYPr.Empty()) return -1; - MinY = TMath::Mn(1.0, MinY); - // determine the sign of power coefficient - double CoefSign = 0.0; - { double A, B, R2, SigA, SigB, Chi2; - TSpecFunc::PowerFit(XYPr, A, B, SigA, SigB, Chi2, R2); - CoefSign = B > 0.0 ? +1.0 : -1.0; } - const double PowerCf = CoefSign * TSpecFunc::GetPowerCoef(XYPr, MinX); - int Mid = (int) exp(log((double)XYPr.Len())/2.0); - if (Mid >= XYPr.Len()) { Mid = XYPr.Len()-1; } - const double MidX = XYPr[Mid].Val1(); - const double MidY = XYPr[Mid].Val2(); - const double B = MidY / pow(MidX, PowerCf); - TStr StyleStr=Style; - if (StyleStr.Empty()) { StyleStr = "linewidth 3"; } - const int FitId = AddFunc(TStr::Fmt("%f*x**%f", B, PowerCf), - SeriesTy, TStr::Fmt("MLE = x^{%.4g}", PowerCf), StyleStr); - return FitId; - /*SeriesV.Add(); - TGpSeries& NewPlot = SeriesV.Last(); - TFltKdV& XYFit = NewPlot.XYValV; - XYFit.Gen(XYPr.Len(), 0); - for (int s = 0; s < XYPr.Len(); s++) { - const double XVal = XYPr[s].Val1; - const double YVal = B * pow(XYPr[s].Val1(), PowerCf); - if (YVal < MinY || XVal < MinX) continue; - XYFit.Add(TFltKd(XVal, YVal)); - } - NewPlot.Label = TStr::Fmt("PowerFit: %g", PowerCf); - NewPlot.SeriesTy = SeriesTy; - if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; } - else { NewPlot.WithStyle = Style; } - return SeriesV.Len() - 1;*/ -} - -int TGnuPlot::AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style) { - double Intercept, Slope, R2; - return AddPwrFit3(PlotId, SeriesTy, MinX, Style, Intercept, Slope, R2); -} - -// some kind of least squares power-law fitting that cutts the tail until the fit is good -int TGnuPlot::AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style, double& Intercept, double& Slope, double& R2) { - if (PlotId < 0 || PlotId >= SeriesV.Len()) return -1; - const TGpSeries& Plot = SeriesV[PlotId]; - if(Plot.XYValV.Empty()) return -1; - double A, B, SigA, SigB, Chi2, MinY=TFlt::Mx; - const TFltKdV& XY = Plot.XYValV; - //SeriesV.Add(); - //TGpSeries& NewPlot = SeriesV.Last(); - //TFltKdV& EstXY = NewPlot.XYValV; - TFltPrV FitXY, NewFitXY; - for (int s = 0; s < XY.Len(); s++) { - if (XY[s].Key > 0 && XY[s].Key >= MinX) { - FitXY.Add(TFltPr(XY[s].Key, XY[s].Dat)); //!!! skip zero values - MinY = TMath::Mn(MinY, XY[s].Dat()); - } - } - MinY = TMath::Mn(1.0, MinY); - // power fit (if tail is too fat, cut everything where - // extrapolation sets the value < MinY - while (true) { - TSpecFunc::PowerFit(FitXY, A, B, SigA, SigB, Chi2, R2); - NewFitXY.Clr(false); - //EstXY.Clr(false); - for (int s = 0; s < FitXY.Len(); s++) { - const double YVal = A*pow(FitXY[s].Val1(), B); - if (YVal < MinY) continue; - //EstXY.Add(TFltKd(FitXY[s].Val1, YVal)); - NewFitXY.Add(TFltPr(FitXY[s].Val1, FitXY[s].Val2)); - } - if (NewFitXY.Len() < 10 || FitXY.Last().Val1 < 1.2 * NewFitXY.Last().Val1) { break; } - else { FitXY.Swap(NewFitXY); } - } - TStr StyleStr=Style; - if (StyleStr.Empty()) { StyleStr = "linewidth 3"; } - const int FitId = AddFunc(TStr::Fmt("%f*x**%f", A, B), - SeriesTy, TStr::Fmt("%.1g * x^{%.4g} R^2:%.2g", A, B, R2), StyleStr); - return FitId; - /*NewPlot.Label = TStr::Fmt("%.1g * x^{%.4g} R^2:%.2g", A, B, R2); - Intercept = A; - Slope = B; - NewPlot.SeriesTy = SeriesTy; - if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; } - else { NewPlot.WithStyle = Style; } - return SeriesV.Len() - 1;*/ -} - -int TGnuPlot::AddLogFit(const int& PlotId, const TGpSeriesTy& SeriesTy, const TStr& Style) { - const TGpSeries& Plot = SeriesV[PlotId]; - if(Plot.XYValV.Empty()) return -1; - const TFltKdV& XY = Plot.XYValV; - double A, B, R2, SigA, SigB, Chi2; - // power fit - TFltPrV XYPr; - int s; - for (s = 0; s < XY.Len(); s++) { - if (XY[s].Key > 0) { - XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat)); } //!!! skip zero values - } - TSpecFunc::LogFit(XYPr, A, B, SigA, SigB, Chi2, R2); - TStr StyleStr=Style; - if (StyleStr.Empty()) { StyleStr = "linewidth 3"; } - const int FitId = AddFunc(TStr::Fmt("%f+%f*log(x)", A, B), - SeriesTy, TStr::Fmt("%.4g + %.4g log(x) R^2:%.2g", A, B, R2), StyleStr); - return FitId; - /*SeriesV.Add(); - TGpSeries& NewPlot = SeriesV.Last(); - TFltKdV& EstXY = NewPlot.XYValV; - for (s = 0; s < XYPr.Len(); s++) { - EstXY.Add(TFltKd(XYPr[s].Val1, A+B*log((double)XYPr[s].Val1))); - } - NewPlot.Label = TStr::Fmt("%.4g + %.4g log(x) R^2:%.2g", A, B, R2); - NewPlot.SeriesTy = SeriesTy; - if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; } - else { NewPlot.WithStyle = Style; } - return SeriesV.Len() - 1;*/ -} - -int TGnuPlot::AddExpFit(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& FitXOffset, const TStr& Style) { - const TGpSeries& Plot = SeriesV[PlotId]; - if(Plot.XYValV.Empty()) return -1; - const TFltKdV& XY = Plot.XYValV; - double A, B, R2, SigA, SigB, Chi2; - // power fit - TFltPrV XYPr; - int s; - for (s = 0; s < XY.Len(); s++) { - if (XY[s].Key-FitXOffset > 0) { - XYPr.Add(TFltPr(XY[s].Key-FitXOffset, XY[s].Dat)); } //!!! skip zero values - } - TSpecFunc::ExpFit(XYPr, A, B, SigA, SigB, Chi2, R2); - TStr Label, StyleStr=Style; - if (FitXOffset == 0) { Label = TStr::Fmt("%.4g exp(%.4g x) R^2:%.2g", A, B, R2); } - else { Label = TStr::Fmt("%.4g exp(%.4g x - %g) R^2:%.2g", A, B, FitXOffset, R2); } - if (StyleStr.Empty()) { StyleStr = "linewidth 3"; } - const int FitId = AddFunc(TStr::Fmt("%f*exp(%f*x-%f)", A, B, FitXOffset), - SeriesTy, Label, StyleStr); - return FitId; - /*SeriesV.Add(); - TGpSeries& NewPlot = SeriesV.Last(); - TFltKdV& EstXY = NewPlot.XYValV; - for (s = 0; s < XYPr.Len(); s++) { - EstXY.Add(TFltKd(XYPr[s].Val1+FitXOffset, A*exp(B*XYPr[s].Val1))); - } - NewPlot.SeriesTy = SeriesTy; - if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; } - else { NewPlot.WithStyle = Style; } - return SeriesV.Len() - 1;*/ -} - -void TGnuPlot::SavePng(const TStr& FNm, const int& SizeX, const int& SizeY, const TStr& Comment, const TStr& Terminal) { - if (Terminal.Empty()) { - //#ifdef GLib_WIN - //#ifndef GLib_MACOSX // The standard GNUPlot for MacOS does not support PNG (Jure: actually version 4.6 DOES!) - // RS 2014/06/17 standard GNUPlot is tricky to configure for PNG on MacOS - AddCmd(TStr::Fmt("set terminal png font arial 10 size %d,%d", SizeX, SizeY)); - AddCmd(TStr::Fmt("set output '%s'", FNm.CStr())); - //#else // EPS - //AddCmd("set terminal postscript eps 10 enhanced color"); - //AddCmd(TStr::Fmt("set output '%s%s.eps'", FNm.GetFPath().CStr(), FNm.GetFMid().CStr())); - //#endif - } else { - AddCmd(Terminal); - AddCmd(TStr::Fmt("set output '%s'", FNm.CStr())); - } - Pause(false); - CreatePlotFile(Comment.Empty()? Title : Comment); - RunGnuPlot(); - MoreCmds.DelLast(); - MoreCmds.DelLast(); -} - -void TGnuPlot::SaveEps(const TStr& FNm, const int& FontSz, const TStr& Comment) { - AddCmd(TStr::Fmt("set terminal postscript enhanced eps %d color", FontSz)); - AddCmd(TStr::Fmt("set output '%s'", FNm.CStr())); - Pause(false); - CreatePlotFile(Comment.Empty()? Title : Comment); - RunGnuPlot(); - MoreCmds.DelLast(); - MoreCmds.DelLast(); -} - -void TGnuPlot::MakeExpBins(const TFltPrV& XYValV, TFltPrV& ExpXYValV, const double& BinFactor, const double& MinYVal) { - TFltKdV KdV(XYValV.Len(), 0), OutV; - for (int i = 0; i < XYValV.Len(); i++) { - KdV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); } - KdV.Sort(); - TGnuPlot::MakeExpBins(KdV, OutV, BinFactor, MinYVal); - ExpXYValV.Gen(OutV.Len(), 0); - for (int i = 0; i < OutV.Len(); i++) { - ExpXYValV.Add(TFltPr(OutV[i].Key, OutV[i].Dat)); } -} - -void TGnuPlot::MakeExpBins(const TFltKdV& XYValV, TFltKdV& ExpXYValV, const double& BinFactor, const double& MinYVal) { - if (XYValV.Empty()) { ExpXYValV.Clr(false); return; } - IAssert(! XYValV.Empty()); - IAssert(XYValV.IsSorted()); - const TFlt MxX = XYValV.Last().Key; - // find buckets - TFltV BucketEndV; BucketEndV.Add(1); - double PrevBPos = 1, BPos = 1; - while (BPos <= MxX) { - PrevBPos = (uint) floor(BPos); - BPos *= BinFactor; - if (floor(BPos) == PrevBPos) { - BPos = PrevBPos + 1; } - BucketEndV.Add(floor(BPos)); - } - //printf("buckets:\n"); for (int i = 0; i < BucketEndV.Len(); i++) { printf("\t%g\n", BucketEndV[i]);} - ExpXYValV.Gen(BucketEndV.Len(), 0); - int CurB = 0; - double AvgPos=0, Cnt=0, AvgVal=0; - for (int v = 0; v < XYValV.Len(); v++) { - if (XYValV[v].Key() == 0.0) { continue; } - AvgPos += XYValV[v].Key ;//* XYValV[v].Dat; // x - AvgVal += XYValV[v].Dat; // y - Cnt++; - if (v+1 == XYValV.Len() || XYValV[v+1].Key > BucketEndV[CurB]) { - if (Cnt != 0) { - //AvgPos /= AvgVal; - //AvgVal /= (BucketEndV[CurB]-BucketEndV[CurB-1]); - AvgPos /= (double) Cnt; - AvgVal /= (double) Cnt; - if (AvgVal < MinYVal) { AvgVal = MinYVal; } - ExpXYValV.Add(TFltKd(AvgPos, AvgVal)); - //printf("b: %6.2f\t%6.2f\n", AvgPos, AvgVal); - AvgPos = 0; AvgVal = 0; Cnt = 0; - } - CurB++; - } - } -} - -void TGnuPlot::LoadTs(const TStr& FNm, TStrV& ColNmV, TVec& ColV) { - PSs Ss = TSs::LoadTxt(ssfTabSep, FNm); - int row = 0; - ColNmV.Clr(); - while (Ss->At(0, row)[0] == '#') { row++; } - for (int c = 1; c < Ss->GetXLen(row); c+=2) { - ColNmV.Add(Ss->At(c, row)); - } - row++; - ColV.Gen(ColNmV.Len(), ColNmV.Len()); - for (; row < Ss->GetYLen(); row++) { - for (int c = 0; c < Ss->GetXLen(row); c+=2) { - if (Ss->At(c,row).Empty()) break; - ColV[c/2].Add(TFltKd(Ss->At(c,row).GetFlt(), Ss->At(c+1,row).GetFlt())); - } - } -} - -TStr TGnuPlot::GetScaleStr(const TGpScaleTy& ScaleTy) { - switch(ScaleTy){ - case gpsNoAuto: return TStr("set noautoscale"); - case gpsAuto: return TStr("set autoscale"); - case gpsLog: return TStr("set logscale"); - case gpsLog2X: return TStr("set logscale x 2"); - case gpsLog2Y: return TStr("set logscale y 2"); - case gpsLog2XY: return TStr("set logscale xy 2"); - case gpsLog10X: return TStr("set logscale x 10"); - case gpsLog10Y: return TStr("set logscale y 10"); - case gpsLog10XY: return TStr("set logscale xy 10"); - default: Fail; - } - return TStr(); -} - -TStr TGnuPlot::GetSeriesTyStr(const TGpSeriesTy& SeriesTy) { - switch(SeriesTy) { - case gpwLines: return TStr("lines"); - case gpwPoints: return TStr("points"); - case gpwLinesPoints: return TStr("linespoints"); - case gpwImpulses: return TStr("impulses"); - case gpwDots: return TStr("dots"); - case gpwSteps: return TStr("steps"); - case gpwFSteps: return TStr("fsteps"); - case gpwHiSteps: return TStr("histeps"); - case gpwBoxes: return TStr("boxes"); - case gpwErrBars: return TStr("errorbars"); - case gpwFilledCurves: return TStr("filledcurves"); - default: Fail; - } - return TStr(); -} - -void TGnuPlot::SaveTs(const TIntKdV& KdV, const TStr& FNm, const TStr& HeadLn) { - FILE *F = fopen(FNm.CStr(), "wt"); - EAssert(F); - if (! HeadLn.Empty()) fprintf(F, "# %s\n", HeadLn.CStr()); - for (int i = 0; i < KdV.Len(); i++) { - fprintf(F, "%d\t%d\n", KdV[i].Key(), KdV[i].Dat()); } - fclose(F); -} - - -void TGnuPlot::SaveTs(const TIntFltKdV& KdV, const TStr& FNm, const TStr& HeadLn) { - FILE *F = fopen(FNm.CStr(), "wt"); - EAssert(F); - if (! HeadLn.Empty()) fprintf(F, "# %s\n", HeadLn.CStr()); - for (int i = 0; i < KdV.Len(); i++) - fprintf(F, "%d\t%g\n", KdV[i].Key(), KdV[i].Dat()); - fclose(F); -} - -void TGnuPlot::Test() { - TFltV DeltaY; - TFltPrV ValV1, ValV2, ValV3; - for (int i = 1; i < 30; i++) { - ValV1.Add(TFltPr(i, pow(double(i), 1.2))); - DeltaY.Add(5*TInt::Rnd.GetUniDev()); - ValV2.Add(TFltPr(i, 5*i-1)); - } - for (int i = -10; i < 20; i++) { - ValV3.Add(TFltPr(i, 2*i + 2 + TInt::Rnd.GetUniDev())); - } - TGnuPlot GnuPlot("testDat", "TestPlot", true); - GnuPlot.SetXYLabel("X", "Y"); - const int id2 = GnuPlot.AddPlot(ValV2, gpwPoints, "y=5*x-1"); - const int id3 = GnuPlot.AddPlot(ValV3, gpwPoints, "y=2*x+2"); - GnuPlot.AddErrBar(ValV1, DeltaY, "y=x^2", "Error bar"); - GnuPlot.AddLinFit(id2, gpwLines); - GnuPlot.AddLinFit(id3, gpwLines); - GnuPlot.Plot(); - GnuPlot.SavePng("testPlot.png"); -} - -int TGnuPlot::IsSameXCol(const int& CurId, const int& PrevId) const { - //if (SerId < 1) { return -1; } - if (SeriesV[CurId].XYValV.Len() != SeriesV[PrevId].XYValV.Len()) { return -1; } - for (int x = 0; x < SeriesV[CurId].XYValV.Len(); x++) { - if (SeriesV[CurId].XYValV[x] != SeriesV[PrevId].XYValV[x]) { return -1; } - } - IAssert(SeriesV[PrevId].XCol > 0); - return SeriesV[PrevId].XCol; -} - -void TGnuPlot::CreatePlotFile(const TStr& Comment) { - time_t ltime; time(<ime); - char* TimeStr = ctime(<ime); TimeStr[strlen(TimeStr) - 1] = 0; - // rearrange columns so that longest are on the left - //SeriesV.Sort(false); - TIntV SerIdV(SeriesV.Len(), 0); - for (int i = 0; i < SeriesV.Len(); i++) { SerIdV.Add(i); } - SerIdV.SortCmp(TGpSeriesCmp(SeriesV)); - // set columns - int ColCnt = 1; - bool SaveData = false; - for (int s = 0; s < SeriesV.Len(); s++) { - TGpSeries& Plt = SeriesV[SerIdV[s]]; - if (Plt.XYValV.Empty()) { continue; } - Plt.DataFNm = DataFNm; - // plots use same X column - const int PrevCol = s > 0 ? IsSameXCol(SerIdV[s], SerIdV[s-1]) : -1; - if (PrevCol != -1) { Plt.XCol = PrevCol; } - else { Plt.XCol = ColCnt; ColCnt++; } - Plt.YCol = ColCnt; ColCnt++; - if (! Plt.ZValV.Empty()) { Plt.ZCol = ColCnt; ColCnt++; } - if (! Plt.XYValV.Empty()) { SaveData=true; } - } - // save data file (skip duplicate X columns) - if (SaveData) { - FILE *F = fopen(DataFNm.CStr(), "wt"); - EAssertR(F != NULL, TStr("Can not open data file ")+DataFNm); - fprintf(F, "#\n"); - fprintf(F, "# %s (%s)\n", Comment.CStr(), TimeStr); - fprintf(F, "#\n"); - // column names - for (int i = 0; i < SerIdV.Len(); i++) { - const TGpSeries& Ser = SeriesV[SerIdV[i]]; - if (Ser.XYValV.Empty()) { continue; } - if (i == 0) { fprintf(F, "# "); } else { fprintf(F, "\t"); } - if (Ser.SaveXVals()) { - if (! LblX.Empty()) { fprintf(F, "%s\t", LblX.CStr()); } - else { fprintf(F, "XVals\t"); } - } - if (Ser.Label.Empty()) { fprintf(F, "%s", LblY.CStr()); } - else { fprintf(F, "%s", SeriesV[SerIdV[i]].Label.CStr()); } - if (Ser.ZCol > 0) fprintf(F, "\tDeltaY"); - } - fprintf(F, "\n"); - // data - for (int row = 0; row < SeriesV[SerIdV[0]].XYValV.Len(); row++) { - for (int i = 0; i < SeriesV.Len(); i++) { - const TGpSeries& Ser = SeriesV[SerIdV[i]]; - if (row < Ser.XYValV.Len()) { - if (i > 0) { fprintf(F, "\t"); } - if (Ser.SaveXVals()) { fprintf(F, "%g\t%g", Ser.XYValV[row].Key(), Ser.XYValV[row].Dat()); } - else { fprintf(F, "%g", Ser.XYValV[row].Dat()); } - if (! Ser.ZValV.Empty()) { fprintf(F, "\t%g", Ser.ZValV[row]()); } - } - } - fprintf(F, "\n"); - } - fclose(F); - } - // save plot file - FILE *F = fopen(PlotFNm.CStr(), "wt"); - EAssertR(F != 0, TStr("Can not open plot file ")+PlotFNm); - TStr CurDir = TDir::GetCurDir(); - CurDir.ChangeStrAll("\\", "\\\\"); - fprintf(F, "#\n"); - fprintf(F, "# %s (%s)\n", Comment.CStr(), TimeStr); - fprintf(F, "#\n\n"); - if (! Title.Empty()) fprintf(F, "set title \"%s\"\n", Title.CStr()); - fprintf(F, "set key bottom right\n"); - fprintf(F, "%s\n", GetScaleStr(ScaleTy).CStr()); - if (ScaleTy==gpsLog || ScaleTy==gpsLog10X || ScaleTy==gpsLog10XY) { - fprintf(F, "set format x \"10^{%%L}\"\n"); - fprintf(F, "set mxtics 10\n"); } - if (ScaleTy==gpsLog || ScaleTy==gpsLog10Y || ScaleTy==gpsLog10XY) { - fprintf(F, "set format y \"10^{%%L}\"\n"); - fprintf(F, "set mytics 10\n"); } - if (ScaleTy==gpsLog2X || ScaleTy==gpsLog2XY) { fprintf(F, "set format x \"2^{%%L}\"\n"); } - if (ScaleTy==gpsLog2Y || ScaleTy==gpsLog2XY) { fprintf(F, "set format y \"2^{%%L}\"\n"); } - if (SetGrid) fprintf(F, "set grid\n"); - if (XRange.Val1 != XRange.Val2) fprintf(F, "set xrange [%g:%g]\n", XRange.Val1(), XRange.Val2()); - if (YRange.Val1 != YRange.Val2) fprintf(F, "set yrange [%g:%g]\n", YRange.Val1(), YRange.Val2()); - if (! LblX.Empty()) fprintf(F, "set xlabel \"%s\"\n", LblX.CStr()); - if (! LblY.Empty()) fprintf(F, "set ylabel \"%s\"\n", LblY.CStr()); - if (Tics42 < -1) { - Tics42 = GetTics42(); - } - if (Tics42) { - fprintf(F, "set tics scale 2\n"); // New in version 4.2 - } else { - fprintf(F, "set ticscale 2 1\n"); // Old (deprecated) - } - // custom commands - for (int i = 0; i < MoreCmds.Len(); i++) { - fprintf(F, "%s\n", MoreCmds[i].CStr()); } - // plot - if (! SeriesV.Empty()) { - fprintf(F, "plot \t"); - for (int i = 0; i < SeriesV.Len(); i++) { - fprintf(F, "%s", GetSeriesPlotStr(i).CStr()); } - fprintf(F, "\n"); - } - if (SetPause) fprintf(F, "pause -1 \"Hit return to exit. %s\"\n", PlotFNm.CStr()); - fclose(F); -} - -void TGnuPlot::RunGnuPlot() const { - // try running gnuplot - if (system(TStr::Fmt("%s %s", GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; } - if (! GnuPlotPath.Empty()) { - #if defined(GLib_WIN) - if (system(TStr::Fmt("%s\\%s %s", GnuPlotPath.CStr(), GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; } - #else - if (system(TStr::Fmt("%s/%s %s", GnuPlotPath.CStr(), GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; } - #endif - } - //Old - //#if defined(GLib_WIN) - //if (system(TStr::Fmt(".\\%s %s", GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; } - //#else - //if (system(TStr::Fmt("./%s %s", GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; } - //#endif - //if (system(TStr::Fmt("%s%s %s", GpPath.CStr(), GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; } - //FailR(TStr::Fmt("Cannot find GnuPlot (%s) for plot %s. Set the PATH.", GpFNm.CStr(), PlotFNm.CStr()).CStr()); - //ErrNotify(TStr::Fmt("Cannot find GnuPlot (%s) for plot %s. Set the PATH.", GpFNm.CStr(), PlotFNm.CStr()).CStr()); - fprintf(stderr, "[%s:%d] Cannot find GnuPlot (%s) for plot %s. Set the $$PATH variable or TGnuPlot::GnuPlotPath. (%s)\n", __FILE__, __LINE__, GnuPlotFNm.CStr(), PlotFNm.CStr(), TGnuPlot::GnuPlotPath.CStr()); -} - diff --git a/inst/include/glib-core/gnuplot.h b/inst/include/glib-core/gnuplot.h deleted file mode 100644 index 034e41b..0000000 --- a/inst/include/glib-core/gnuplot.h +++ /dev/null @@ -1,666 +0,0 @@ -#ifndef gnuplot_h -#define gnuplot_h - -///////////////////////////////////////////////// -// GNU-Plot-Chart -typedef enum { - gpsNoAuto, gpsAuto, gpsLog, gpsLog2X, gpsLog2Y, gpsLog2XY, gpsLog10X, - gpsLog10Y, gpsLog10XY -} TGpScaleTy; - -typedef enum { - gpwUndef, gpwLines, gpwPoints, gpwLinesPoints, gpwImpulses, gpwDots, - gpwSteps, gpwFSteps, gpwHiSteps, gpwBoxes, gpwErrBars, gpwFilledCurves, gpwMax -} TGpSeriesTy; - -class TGnuPlot { -public: - /// Path to GnuPlot executable. Set if gnuplot is not found in the PATH. - static TStr GnuPlotPath; - /// GnuPlot executable file name. Set if different than the standard wgnuplot/gnuplot. - static TStr GnuPlotFNm; - static TStr DefPlotFNm; - static TStr DefDataFNm; -private: - class TGpSeries { - public: - TGpSeriesTy SeriesTy; - TFltKdV XYValV; - TFltV ZValV; // error bar and 3d plots - TStr Label, WithStyle, DataFNm; - int XCol, YCol, ZCol; - public: - TGpSeries() : SeriesTy(gpwLines), XYValV(), ZValV(), Label(), WithStyle(), DataFNm(), XCol(0), YCol(0), ZCol(0) { } - TGpSeries(const TGpSeries& Gps); - TGpSeries& operator = (const TGpSeries& Gps); - bool operator < (const TGpSeries& Gps) const; - bool SaveXVals() const { return (YCol-XCol)==1; } - }; - class TGpSeriesCmp { - private: - const TVec& SeriesV; - public: - TGpSeriesCmp(const TVec& _SeriesV) : SeriesV(_SeriesV) { } - bool operator () (const int& Left, const int& Right) const { - return SeriesV[Left] > SeriesV[Right]; } - }; -private: - static int Tics42; // 1 - "set ticks", 0 - "set ticscale" - // -1 - unknown, -2 - not initialized - TStr DataFNm, PlotFNm; - TStr Title, LblX, LblY; - TGpScaleTy ScaleTy; - TFltPr YRange, XRange; - bool SetGrid, SetPause; - TVec SeriesV; - TStrV MoreCmds; -public: - static int GetTics42(); -public: - TStr GetSeriesPlotStr(const int& PlotN); - int IsSameXCol(const int& CurId, const int& PrevId) const; - void CreatePlotFile(const TStr& Comment = TStr()); - void RunGnuPlot() const; -public: - TGnuPlot(const TStr& FileNm="gplot", const TStr& PlotTitle=TStr(), const bool& Grid=true); - TGnuPlot(const TStr& DataFileNm, const TStr& PlotFileNm, const TStr& PlotTitle, const bool& Grid); - TGnuPlot(const TGnuPlot& GnuPlot); - TGnuPlot& operator = (const TGnuPlot& GnuPlot); - - void SetTitle(const TStr& PlotTitle) { Title = PlotTitle; } - void SetXLabel(const TStr& XLabel) { LblX = XLabel; } - void SetYLabel(const TStr& YLabel) { LblY = YLabel; } - void SetXYLabel(const TStr& XLabel, const TStr& YLabel) { LblX = XLabel; LblY = YLabel; } - void SetDataPlotFNm(const TStr& DatFNm, const TStr& PltFNm) { DataFNm = DatFNm; PlotFNm = PltFNm; } - - void ShowGrid(const bool& Show) { SetGrid = Show; } - void Pause(const bool& DoPause) { SetPause = DoPause; } - void SetScale(const TGpScaleTy& GpScaleTy) { ScaleTy = GpScaleTy;} - void SetXRange(const double& Min, const double& Max) { XRange = TFltPr(Min, Max); } - void SetYRange(const double& Min, const double& Max) { YRange = TFltPr(Min, Max); } - void AddCmd(const TStr& Cmd) { MoreCmds.Add(Cmd); } - TStr GetLineStyle(const int& PlotId) const { return SeriesV[PlotId].WithStyle; } - void SetLineStyle(const int& PlotId, const TStr& StyleStr) { SeriesV[PlotId].WithStyle = StyleStr; } - - int AddFunc(const TStr& FuncStr, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TIntV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TFltV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TFltV& XValV, const TFltV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TIntPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TFltPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TIntKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TFltKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TIntFltKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TIntFltPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TStr& DataFNm, const int& ColY, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - int AddPlot(const TStr& DataFNm, const int& ColX, const int& ColY, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr()); - template - int AddPlot(const THash& XYValH, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr(), const bool& ExpBucket = false); - template - int AddPlot(const THash& ValMomH, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr(), - bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=false, const bool& ExpBucket=false); - - int AddErrBar(const TFltTrV& XYDValV, const TStr& Label=TStr()); - int AddErrBar(const TFltTrV& XYDValV, const TStr& DatLabel, const TStr& ErrLabel); - int AddErrBar(const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label=TStr()); - int AddErrBar(const TFltV& XValV, const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label=TStr()); - int AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& Label=TStr()); - int AddErrBar(const TFltKdV& XYValV, const TFltV& DeltaYV, const TStr& Label=TStr()); - int AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& DatLabel, const TStr& ErrLabel); - - int AddLinFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr()); - int AddPwrFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr()); - int AddPwrFit1(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr()); - int AddPwrFit2(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& MinX=-1.0, const TStr& Style=TStr()); - int AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& MinX=-1.0, const TStr& Style=TStr()); - int AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style, double& Intercept, double& Slope, double& R2); - int AddLogFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr()); - int AddExpFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& FitXOffset=0.0, const TStr& Style=TStr()); - - void SavePng(const int& SizeX=1000, const int& SizeY=800, const TStr& Comment=TStr()) { - SavePng(PlotFNm.GetFPath()+PlotFNm.GetFMid()+".png", SizeX, SizeY, Comment); } - void SavePng(const TStr& FNm, const int& SizeX=1000, const int& SizeY=800, const TStr& Comment=TStr(), const TStr& Terminal=TStr()); - void SaveEps(const int& FontSz=30, const TStr& Comment=TStr()) { - SaveEps(PlotFNm.GetFPath()+PlotFNm.GetFMid()+".eps", FontSz, Comment); } - void SaveEps(const TStr& FNm, const int& FontSz=30, const TStr& Comment=TStr()); - void Plot(const TStr& Comment=TStr()) { CreatePlotFile(Comment); RunGnuPlot(); } - - static void MakeExpBins(const TFltPrV& XYValV, TFltPrV& ExpXYValV, - const double& BinFactor = 2, const double& MinYVal = 1); - static void MakeExpBins(const TFltKdV& XYValV, TFltKdV& ExpXYValV, - const double& BinFactor = 2, const double& MinYVal = 1); - static void LoadTs(const TStr& FNm, TStrV& ColNmV, TVec& ColV); - - static TStr GetScaleStr(const TGpScaleTy& ScaleTy); - static TStr GetSeriesTyStr(const TGpSeriesTy& SeriesTy); - - // save tab separated - static void SaveTs(const TIntKdV& KdV, const TStr& FNm, const TStr& HeadLn = TStr()); - static void SaveTs(const TIntFltKdV& KdV, const TStr& FNm, const TStr& HeadLn = TStr()); - template - static void SaveTs(const TVec >& ValV, const TStr& FNm, const TStr& HeadLn = TStr()); - template - static void SaveTs(const TVec >& ValV, const TStr& FNm, const TStr& HeadLn = TStr()); - template - static void SaveTs(const TVec >& ValV, const TStr& FNm, const TStr& HeadLn = TStr()); - static void Test(); - - // plot value-count tables, and pair vectors - template - static void PlotValV(const TVec& ValV, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, - const TGpSeriesTy& SeriesTy=gpwLinesPoints); - template - static void PlotValV(const TVec >& ValV, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, - const TGpSeriesTy& SeriesTy=gpwLinesPoints); - template - static void PlotValV(const TVec >& ValV, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, - const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& ErrBarStr = ""); - template - static void PlotValV(const TVec >& ValV1, const TStr& Name1, - const TVec >& ValV2, const TStr& Name2, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, - const TGpSeriesTy& SeriesTy=gpwLinesPoints); - template - static void PlotValRank(const THash& ValCntH, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, - const TGpSeriesTy& SeriesTy=gpwLinesPoints); - template - static void PlotValCntH(const THash& ValCntH, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, - const TGpSeriesTy& SeriesTy=gpwLinesPoints, const bool& PlotCCDF=false, const bool& ExpBucket=false); - template - static void PlotValCntH(const THash& ValCntH1, const TStr& Label1, - const THash& ValCntH2, const TStr& Label2, - const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, - const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints); - template - static void PlotValCntH(const THash& ValCntH1, const TStr& Label1, - const THash& ValCntH2, const TStr& Label2, - const THash& ValCntH3, const TStr& Label3, - const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, - const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints); - template - static void PlotValMomH(const THash& ValMomH, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints, - bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=true, bool PlotScatter=false); - template - static void PlotValMomH(const THash& ValMomH1, const TStr& Label1, const THash& ValMomH2, const TStr& Label2, - const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints, - bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=true, bool PlotScatter=false); - template - static void PlotValOverTm(const TVec >& ValV, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints); - template - static void PlotCntOverTm(const THash& CntH, const TStr& OutFNmPref, const TStr& Desc="", - const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints); - -}; - -//--------------------------------------------------------- -// useful commands -// set terminal png small size 800,600 -// set terminal postscript enhanced eps 22 -// set output 'hopsasa.png' -// set size 1,0.5 -// set pointsize 0.9 -// set key right bottom Left -// set style fill solid 0.2 -// set ticscale 3 1 # longer axis tics -// plot ... with points pointtype 6 pointsize 1 -// 1 + 7 (f) circle -// 2 cross 8 (o) triangle -// 3 * 9 (f) triangle -// 4 (o) square 10 (o) inverse-triangle -// 5 (f) square 11 (f) inverse-triangle -// 6 (o) circle 12 (o) diamond -// 13 (f) diamond -// set label "Text" at 23,47000 -// set arrow from 28,45000 to 28,31000 lw 3 -// axes x1y2: http://t16web.lanl.gov/Kawano/gnuplot/plot1-e.html#5.2 - -template -void TGnuPlot::SaveTs(const TVec >& ValV, const TStr& FNm, const TStr& HeadLn) { - FILE *F = fopen(FNm.CStr(), "wt"); - EAssert(F); - if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); } - for (int i = 0; i < ValV.Len(); i++) { - fprintf(F, "%g\t%g\n", double(ValV[i].Val1), double(ValV[i].Val2)); } - fclose(F); -} - -template -void TGnuPlot::SaveTs(const TVec >& ValV, const TStr& FNm, const TStr& HeadLn) { - FILE *F = fopen(FNm.CStr(), "wt"); - EAssert(F); - if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); } - for (int i = 0; i < ValV.Len(); i++) { - fprintf(F, "%g\t%g\t%g\n", double(ValV[i].Val1), double(ValV[i].Val2), double(ValV[i].Val3)); } - fclose(F); -} - -template -void TGnuPlot::SaveTs(const TVec >& ValV, const TStr& FNm, const TStr& HeadLn) { - FILE *F = fopen(FNm.CStr(), "wt"); - EAssert(F); - if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); } - for (int i = 0; i < ValV.Len(); i++) { - fprintf(F, "%g", double(ValV[i][0])); - for (int v = 1; v < Vals; v++) { - fprintf(F, "\t%g", double(ValV[i][v])); } - fprintf(F, "\n"); - } - fclose(F); -} - -template -int TGnuPlot::AddPlot(const THash& XYValH, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style, const bool& ExpBucket) { - TFltPrV XYFltValV(XYValH.Len(), 0); - for (int k = XYValH.FFirstKeyId(); XYValH.FNextKeyId(k); ) { - XYFltValV.Add(TFltPr(TFlt(XYValH.GetKey(k)), TFlt(XYValH[k]))); - } - XYFltValV.Sort(); - if (ExpBucket) { - TFltPrV BucketV; - TGnuPlot::MakeExpBins(XYFltValV, BucketV); - BucketV.Swap(XYFltValV); - } - return AddPlot(XYFltValV, SeriesTy, Label, Style); -} - -template -int TGnuPlot::AddPlot(const THash& ValMomH, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style, bool PlotAvg, bool PlotMed, bool PlotMin, bool PlotMax, bool PlotSDev, bool PlotStdErr, const bool& ExpBucket) { - TFltTrV AvgV, StdErrV; - TFltPrV AvgV2, MedV, MinV, MaxV, BucketV; - for (int i = ValMomH.FFirstKeyId(); ValMomH.FNextKeyId(i); ) { - TMom Mom(ValMomH[i]); - if (! Mom.IsDef()) { Mom.Def(); } - const double x = ValMomH.GetKey(i); - if (PlotAvg) { - if (PlotSDev) { - AvgV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation - else if (PlotStdErr) { - StdErrV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); - } else { - AvgV2.Add(TFltPr(x, Mom.GetMean())); - } - } - if (PlotMed) { MedV.Add(TFltPr(x, Mom.GetMedian())); } - if (PlotMin) { MinV.Add(TFltPr(x, Mom.GetMn())); } - if (PlotMax) { MaxV.Add(TFltPr(x, Mom.GetMx())); } - } - AvgV.Sort(); AvgV2.Sort(); - MedV.Sort(); MinV.Sort(); MaxV.Sort(); - int PlotId=0; - // exponential bucketing - if (ExpBucket) { - if (! AvgV2.Empty()) { TGnuPlot::MakeExpBins(AvgV2, BucketV); BucketV.Swap(AvgV2); } - if (! MedV.Empty()) { TGnuPlot::MakeExpBins(MedV, BucketV); BucketV.Swap(MedV); } - if (! MinV.Empty()) { TGnuPlot::MakeExpBins(MinV, BucketV); BucketV.Swap(MinV); } - if (! MaxV.Empty()) { TGnuPlot::MakeExpBins(MaxV, BucketV); BucketV.Swap(MaxV); } - } - // plot - if (! AvgV.Empty()) { PlotId = AddErrBar(AvgV, Label+" Average", Label+" StdDev"); } - if (! AvgV2.Empty()) { PlotId = AddPlot(AvgV2, SeriesTy, Label+" Average", Style); } - if (! MedV.Empty()) { PlotId = AddPlot(MedV, SeriesTy, Label+" Median", Style); } - if (! MinV.Empty()) { PlotId = AddPlot(MinV, SeriesTy, Label+" Min", Style); } - if (! MaxV.Empty()) { PlotId = AddPlot(MaxV, SeriesTy, Label+" Max", Style); } - if (! StdErrV.Empty()) { PlotId = AddErrBar(StdErrV, Label+" Average", Label+" StdErr"); } - return PlotId; -} - -template -void TGnuPlot::PlotValV(const TVec >& ValV, const TStr& OutFNmPref, const TStr& Desc, - const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) { - TFltKdV IdCntV(ValV.Len(), 0); - for (int i = 0; i < ValV.Len(); i++) { - IdCntV.Add(TFltKd(double(ValV[i].Val1), double(ValV[i].Val2))); } - if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr()); return; } - IdCntV.Sort(); - TGnuPlot GP(OutFNmPref, Desc); - GP.SetXYLabel(XLabel, YLabel); - GP.SetScale(ScaleTy); - const int Id = GP.AddPlot(IdCntV, SeriesTy); - if (PowerFit) { - GP.AddPwrFit3(Id); - double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat; - if (MaxY < MinY) { Swap(MaxY, MinY); } - //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0)); - GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY)); - } - GP.SavePng(); -} - -template -void TGnuPlot::PlotValV(const TVec >& ValV, const TStr& OutFNmPref, const TStr& Desc, - const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, - const TGpSeriesTy& SeriesTy, const TStr& ErrBarStr) { - TFltKdV IdCntV(ValV.Len(), 0); - TFltV DeltaYV(ValV.Len(), 0); - for (int i = 0; i < ValV.Len(); i++) { - IdCntV.Add(TFltKd(double(ValV[i].Val1), double(ValV[i].Val2))); - DeltaYV.Add(double(ValV[i].Val3)); - } - if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr()); return; } - IdCntV.Sort(); - TGnuPlot GP(OutFNmPref, Desc); - GP.SetXYLabel(XLabel, YLabel); - GP.SetScale(ScaleTy); - const int Id = GP.AddPlot(IdCntV, SeriesTy); - GP.AddErrBar(IdCntV, DeltaYV, ErrBarStr); - if (PowerFit) { - GP.AddPwrFit3(Id); - double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat; - if (MaxY < MinY) { Swap(MaxY, MinY); } - //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0)); - GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY)); - } - GP.SavePng(); -} - -template -void TGnuPlot::PlotValV(const TVec >& ValV1, const TStr& Name1, - const TVec >& ValV2, const TStr& Name2, - const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, - const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) { - TFltKdV IdCntV1(ValV1.Len(), 0), IdCntV2(ValV2.Len(), 0); - for (int i = 0; i < ValV1.Len(); i++) { - IdCntV1.Add(TFltKd(double(ValV1[i].Val1), double(ValV1[i].Val2))); } - for (int i = 0; i < ValV2.Len(); i++) { - IdCntV2.Add(TFltKd(double(ValV2[i].Val1), double(ValV2[i].Val2))); } - if (IdCntV1.Empty() || IdCntV2.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr()); return; } - IdCntV1.Sort(); - IdCntV2.Sort(); - TGnuPlot GP(OutFNmPref, Desc); - GP.SetXYLabel(XLabel, YLabel); - GP.SetScale(ScaleTy); - { const int Id = GP.AddPlot(IdCntV1, SeriesTy, Name1); - if (PowerFit) { - GP.AddPwrFit3(Id); - double MaxY = IdCntV1.Last().Dat, MinY = IdCntV1[0].Dat; - if (MaxY < MinY) { Swap(MaxY, MinY); } - GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY)); - } } - { const int Id = GP.AddPlot(IdCntV2, SeriesTy, Name2); - if (PowerFit) { - GP.AddPwrFit3(Id); - double MaxY = IdCntV2.Last().Dat, MinY = IdCntV2[0].Dat; - if (MaxY < MinY) { Swap(MaxY, MinY); } - GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY)); - } } - GP.SavePng(); -} - -template -void TGnuPlot::PlotValV(const TVec& ValV, const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, - const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) { - TFltKdV IdCntV(ValV.Len(), 0); - for (int i = 0; i < ValV.Len(); i++) { - IdCntV.Add(TFltKd(double(i+1), double(ValV[i]))); } - if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr()); return; } - IdCntV.Sort(); - TGnuPlot GP(OutFNmPref, Desc); - GP.SetXYLabel(XLabel, YLabel); - GP.SetScale(ScaleTy); - const int Id = GP.AddPlot(IdCntV, SeriesTy); - if (PowerFit) { - GP.AddPwrFit3(Id); - double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat; - if (MaxY < MinY) { Swap(MaxY, MinY); } - //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0)); - GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY)); - } - GP.SavePng(); -} - -template -void TGnuPlot::PlotValRank(const THash& ValCntH, const TStr& OutFNmPref, const TStr& Desc, - const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) { - TFltPrV IdCntV(ValCntH.Len(), 0); - TFltV ValV(ValCntH.Len(), 0); - for (int i = ValCntH.FFirstKeyId(); ValCntH.FNextKeyId(i); ) { - ValV.Add(double(ValCntH[i])); } - ValV.Sort(false); - for (int i = 0; i < ValV.Len(); i++) { - IdCntV.Add(TFltPr(i+1, ValV[i])); - } - PlotValV(IdCntV, OutFNmPref, Desc, XLabel, YLabel, ScaleTy, PowerFit, SeriesTy); -} - -template -void TGnuPlot::PlotValCntH(const THash& ValCntH, const TStr& OutFNmPref, const TStr& Desc, - const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, - const TGpSeriesTy& SeriesTy, const bool& PlotCCDF, const bool& ExpBucket) { - TFltPrV IdCntV(ValCntH.Len(), 0), BucketV; - for (int i = ValCntH.FFirstKeyId(); ValCntH.FNextKeyId(i); ) { - IdCntV.Add(TFltPr(double(ValCntH.GetKey(i)), double(ValCntH[i]))); } - IdCntV.Sort(); - if (ExpBucket) { - TGnuPlot::MakeExpBins(IdCntV, BucketV); - BucketV.Swap(IdCntV); - } - if (PlotCCDF) { - TFltPrV NCdfV = IdCntV; - for (int i = NCdfV.Len()-2; i >= 0; i--) { - NCdfV[i].Val2 = NCdfV[i].Val2 + NCdfV[i+1].Val2; - } - PlotValV(NCdfV, OutFNmPref, Desc, "NCDF "+XLabel, "NCDF "+YLabel, ScaleTy, PowerFit, SeriesTy); - } else { - PlotValV(IdCntV, OutFNmPref, Desc, XLabel, YLabel, ScaleTy, PowerFit, SeriesTy); - } -} - -template -void TGnuPlot::PlotValCntH(const THash& ValCntH1, const TStr& Label1, - const THash& ValCntH2, const TStr& Label2, - const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, - const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) { - PlotValCntH(ValCntH1, Label1, ValCntH2, Label2, THash(), "", OutFNmPref, Desc, XLabel, YLabel, - ScaleTy, SeriesTy); -} - -template -void TGnuPlot::PlotValCntH(const THash& ValCntH1, const TStr& Label1, - const THash& ValCntH2, const TStr& Label2, - const THash& ValCntH3, const TStr& Label3, - const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, - const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) { - TFltPrV IdCntV1(ValCntH1.Len(), 0), IdCntV2(ValCntH2.Len(), 0), IdCntV3(ValCntH3.Len(), 0); - for (int i = ValCntH1.FFirstKeyId(); ValCntH1.FNextKeyId(i); ) { - IdCntV1.Add(TFltPr(double(ValCntH1.GetKey(i)), double(ValCntH1[i]))); } - for (int i = ValCntH2.FFirstKeyId(); ValCntH2.FNextKeyId(i); ) { - IdCntV2.Add(TFltPr(double(ValCntH2.GetKey(i)), double(ValCntH2[i]))); } - for (int i = ValCntH3.FFirstKeyId(); ValCntH3.FNextKeyId(i); ) { - IdCntV3.Add(TFltPr(double(ValCntH3.GetKey(i)), double(ValCntH3[i]))); } - IdCntV1.Sort(); - IdCntV2.Sort(); - IdCntV3.Sort(); - TGnuPlot GP(OutFNmPref, Desc); - GP.SetXYLabel(XLabel, YLabel); - GP.SetScale(ScaleTy); - if (! IdCntV1.Empty()) { GP.AddPlot(IdCntV1, SeriesTy, Label1); } - if (! IdCntV2.Empty()) { GP.AddPlot(IdCntV2, SeriesTy, Label2); } - if (! IdCntV3.Empty()) { GP.AddPlot(IdCntV3, SeriesTy, Label3); } - GP.SavePng(); -} - -template -void TGnuPlot::PlotValMomH(const THash& ValMomH, const TStr& OutFNmPref, const TStr& Desc, - const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy, - bool PlotAvg, bool PlotMed, bool PlotMin, bool PlotMax, bool PlotSDev, bool PlotStdErr, - bool PlotScatter) { - TFltTrV AvgV, StdErrV; - TFltPrV AvgV2, MedV, MinV, MaxV; - TFltPrV ScatterV; - for (int i = ValMomH.FFirstKeyId(); ValMomH.FNextKeyId(i); ) { - TMom Mom(ValMomH[i]); - if (! Mom.IsDef()) { Mom.Def(); } - const double x = ValMomH.GetKey(i); - if (PlotAvg) { - if (PlotSDev) { - AvgV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation - else { - AvgV2.Add(TFltPr(x, Mom.GetMean())); - } - if (PlotStdErr) { - StdErrV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); - } - } - if (PlotMed) { MedV.Add(TFltPr(x, Mom.GetMedian())); } - if (PlotMin) { MinV.Add(TFltPr(x, Mom.GetMn())); } - if (PlotMax) { MaxV.Add(TFltPr(x, Mom.GetMx())); } - if (PlotScatter) { - THashSet PointSet; - for (int xi = 0; xi < ValMomH[i].GetVals(); xi++) { - PointSet.AddKey(ValMomH[i].GetVal(xi)); } - //ScatterV.Add(TFltPr(x, ValMomH[i].GetVal(xi))); } - for (int xi = 0; xi < PointSet.Len(); xi++) { - ScatterV.Add(TFltPr(x, PointSet[xi])); } - //printf("S%d %d %d.", ValMomH[i].GetVals(), PointSet.Len(), ScatterV.Len()); - } - } - AvgV.Sort(); AvgV2.Sort(); - MedV.Sort(); MinV.Sort(); MaxV.Sort(); StdErrV.Sort(); - TGnuPlot GP(OutFNmPref, Desc); - GP.SetScale(ScaleTy); - GP.SetXYLabel(XLabel, YLabel); - if (! ScatterV.Empty()) { GP.AddPlot(ScatterV, gpwPoints, "Scatter"); } - if (! AvgV.Empty()) { GP.AddErrBar(AvgV, "Average", "StdDev"); } - if (! AvgV2.Empty()) { GP.AddPlot(AvgV2, SeriesTy, "Average"); } - if (! MedV.Empty()) { GP.AddPlot(MedV, SeriesTy, "Median"); } - if (! MinV.Empty()) { GP.AddPlot(MinV, SeriesTy, "Min"); } - if (! MaxV.Empty()) { GP.AddPlot(MaxV, SeriesTy, "Max"); } - if (! StdErrV.Empty()) { GP.AddErrBar(StdErrV, "Standard error"); } - GP.SavePng(); -} - -template -void TGnuPlot::PlotValMomH(const THash& ValMomH1, const TStr& Label1, - const THash& ValMomH2, const TStr& Label2, - const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, - const TStr& YLabel, const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy, bool PlotAvg, - bool PlotMed, bool PlotMin, bool PlotMax, bool PlotSDev, bool PlotStdErr, bool PlotScatter) { - TFltTrV AvgV1, AvgV2, StdErrV1, StdErrV2; - TFltPrV AvgVM1, MedV1, MinV1, MaxV1; - TFltPrV AvgVM2, MedV2, MinV2, MaxV2; - TFltPrV ScatterV1, ScatterV2; - // ValMom1 - for (int i = ValMomH1.FFirstKeyId(); ValMomH1.FNextKeyId(i); ) { - TMom Mom(ValMomH1[i]); - if (! Mom.IsDef()) { Mom.Def(); } - const double x = ValMomH1.GetKey(i); - if (PlotAvg) { - if (PlotSDev) { - AvgV1.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation - else { - AvgVM1.Add(TFltPr(x, Mom.GetMean())); - } - if (PlotStdErr) { - StdErrV1.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); - } - } - if (PlotMed) { MedV1.Add(TFltPr(x, Mom.GetMedian())); } - if (PlotMin) { MinV1.Add(TFltPr(x, Mom.GetMn())); } - if (PlotMax) { MaxV1.Add(TFltPr(x, Mom.GetMx())); } - if (PlotScatter) { - THashSet PointSet; - for (int xi = 0; xi < ValMomH1[i].GetVals(); xi++) { - PointSet.AddKey(ValMomH1[i].GetVal(xi)); } - for (int xi = 0; xi < PointSet.Len(); xi++) { - ScatterV1.Add(TFltPr(x, PointSet[xi])); } - } - } - AvgV1.Sort(); AvgVM1.Sort(); MedV1.Sort(); MinV1.Sort(); MaxV1.Sort(); StdErrV1.Sort(); - // ValMom2 - for (int i = ValMomH2.FFirstKeyId(); ValMomH2.FNextKeyId(i); ) { - TMom Mom(ValMomH2[i]); - if (! Mom.IsDef()) { Mom.Def(); } - const double x = ValMomH2.GetKey(i); - if (PlotAvg) { - if (PlotSDev) { - AvgV2.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation - else { - AvgVM2.Add(TFltPr(x, Mom.GetMean())); - } - if (PlotStdErr) { - StdErrV2.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); - } - } - if (PlotMed) { MedV2.Add(TFltPr(x, Mom.GetMedian())); } - if (PlotMin) { MinV2.Add(TFltPr(x, Mom.GetMn())); } - if (PlotMax) { MaxV2.Add(TFltPr(x, Mom.GetMx())); } - if (PlotScatter) { - THashSet PointSet; - for (int xi = 0; xi < ValMomH2[i].GetVals(); xi++) { - PointSet.AddKey(ValMomH2[i].GetVal(xi)); } - for (int xi = 0; xi < PointSet.Len(); xi++) { - ScatterV2.Add(TFltPr(x, PointSet[xi])); } - } - } - AvgV2.Sort(); AvgVM2.Sort(); MedV2.Sort(); MinV2.Sort(); MaxV2.Sort(); StdErrV2.Sort(); - // plot - TGnuPlot GP(OutFNmPref, Desc); - GP.SetScale(ScaleTy); - GP.SetXYLabel(XLabel, YLabel); - // ValMom1 - if (! ScatterV1.Empty()) { GP.AddPlot(ScatterV1, gpwPoints, Label1+": Scatter"); } - if (! AvgV1.Empty()) { GP.AddErrBar(AvgV1, Label1+": Average", Label1+": StdDev"); } - if (! AvgVM1.Empty()) { GP.AddPlot(AvgVM1, SeriesTy, Label1+": Average"); } - if (! MedV1.Empty()) { GP.AddPlot(MedV1, SeriesTy, Label1+": Median"); } - if (! MinV1.Empty()) { GP.AddPlot(MinV1, SeriesTy, Label1+": Min"); } - if (! MaxV1.Empty()) { GP.AddPlot(MaxV1, SeriesTy, Label1+": Max"); } - if (! StdErrV1.Empty()) { GP.AddErrBar(StdErrV1, Label1+": Std error"); } - // ValMom2 - if (! ScatterV2.Empty()) { GP.AddPlot(ScatterV2, gpwPoints, Label2+": Scatter"); } - if (! AvgV2.Empty()) { GP.AddErrBar(AvgV2, Label2+": Average", Label2+": StdDev"); } - if (! AvgVM2.Empty()) { GP.AddPlot(AvgVM2, SeriesTy, Label2+": Average"); } - if (! MedV2.Empty()) { GP.AddPlot(MedV2, SeriesTy, Label2+": Median"); } - if (! MinV2.Empty()) { GP.AddPlot(MinV2, SeriesTy, Label2+": Min"); } - if (! MaxV2.Empty()) { GP.AddPlot(MaxV2, SeriesTy, Label2+": Max"); } - if (! StdErrV2.Empty()) { GP.AddErrBar(StdErrV2, Label2+": Std error"); } - GP.SavePng(); -} - -template -void TGnuPlot::PlotValOverTm(const TVec >& ValV, const TStr& OutFNmPref, const TStr& Desc, - const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) { - if (ValV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr()); return; } - TVec > TmpV = ValV; - TmpV.Sort(); - TGnuPlot GP(OutFNmPref, Desc); - GP.SetXYLabel(XLabel, YLabel); - GP.SetScale(ScaleTy); - GP.AddCmd("set xdata time"); - GP.AddCmd("set timefmt \"%Y-%m-%d-%H:%M:%S\""); - GP.AddCmd("set format x \"%Y-%m-%d\\n%H:%M:%S\""); - - // save file - time_t ltime; time(<ime); - char* TimeStr = ctime(<ime); TimeStr[strlen(TimeStr) - 1] = 0; - FILE *F = fopen(GP.DataFNm.CStr(), "wt"); - fprintf(F, "#\n"); - fprintf(F, "# %s (%s)\n", Desc.CStr(), TimeStr); - fprintf(F, "#\n"); - fprintf(F, "#Time\t%s\n", XLabel.CStr()); - for (int i = 0; i < TmpV.Len(); i++) { - fprintf(F, "%s\t%g\n", TmpV[i].Val1.GetYmdTmStr2().CStr(), double(TmpV[i].Val2())); - } - fclose(F); - // plot data - GP.AddPlot(GP.DataFNm, 1, 2); - GP.SavePng(); -} - -template -void TGnuPlot::PlotCntOverTm(const THash& CntH, const TStr& OutFNmPref, const TStr& Desc, - const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) { - TVec > TmpV; - CntH.GetKeyDatPrV(TmpV); - PlotValOverTm(TmpV, OutFNmPref, Desc, XLabel, YLabel, ScaleTy, SeriesTy); -} - -#endif - diff --git a/inst/include/glib-core/hash.cpp b/inst/include/glib-core/hash.cpp deleted file mode 100644 index 34d47f8..0000000 --- a/inst/include/glib-core/hash.cpp +++ /dev/null @@ -1,137 +0,0 @@ -///////////////////////////////////////////////// -// Big-String-Pool -void TBigStrPool::Resize(TSize _MxBfL) { - TSize newSize = MxBfL; - while (newSize < _MxBfL) { - if (newSize >= GrowBy && GrowBy > 0) newSize += GrowBy; - else if (newSize > 0) newSize *= 2; - else newSize = TInt::GetMn(GrowBy, 1024); - } - if (newSize > MxBfL) { - if (IsShM) { - Bf = (char*) malloc(newSize); - IsShM = false; - } else { - Bf = (char *) realloc(Bf, newSize); - } - IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr()); - MxBfL = newSize; - } - IAssert(MxBfL >= _MxBfL); -} - -TBigStrPool::TBigStrPool(TSize MxBfLen, uint _GrowBy) : MxBfL(MxBfLen), BfL(0), GrowBy(_GrowBy), Bf(0), IsShM(false) { - //IAssert(MxBfL >= 0); IAssert(GrowBy >= 0); - if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); } - AddStr(""); // add empty string -} - -TBigStrPool::TBigStrPool(TSIn& SIn, bool LoadCompact) : MxBfL(0), BfL(0), GrowBy(0), Bf(0), IsShM(false) { - uint64 Tmp; - SIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); MxBfL=TSize(Tmp); - SIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); BfL=TSize(Tmp); - SIn.Load(GrowBy); - IAssert(MxBfL >= BfL); IAssert(BfL >= 0); IAssert(GrowBy >= 0); - if (LoadCompact) MxBfL = BfL; - if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); } - if (BfL > 0) { SIn.LoadBf(Bf, BfL); } - SIn.LoadCs(); - int NStr=0; SIn.Load(NStr); - IdOffV.Gen(NStr, 0); - for (int i = 0; i < NStr; i++) { - SIn.Load(Tmp); - IAssert(Tmp <= uint64(TSizeMx)); - IdOffV.Add(TSize(Tmp)); - } -} - -void TBigStrPool::LoadPoolShM(TShMIn& ShMIn, bool LoadCompact) { - uint64 Tmp; - ShMIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); MxBfL=TSize(Tmp); - ShMIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); BfL=TSize(Tmp); - ShMIn.Load(GrowBy); IAssert(MxBfL >= BfL); IAssert(BfL >= 0); IAssert(GrowBy >= 0); - IsShM = true; - if (LoadCompact) { - MxBfL = BfL; - Bf = (char*)(ShMIn.AdvanceCursor(BfL)); - IsShM = true; - } else { - if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); IsShM = false;} - if (BfL > 0) { ShMIn.LoadBf(Bf, BfL); } - IsShM = false; - } - ShMIn.LoadCs(); - int NStr=0; - ShMIn.Load(NStr); - IdOffV.Gen(NStr, 0); - for (int i = 0; i < NStr; i++) { - ShMIn.Load(Tmp); - IAssert(Tmp <= uint64(TSizeMx)); - IdOffV.Add(TSize(Tmp)); - } -} - - -void TBigStrPool::Save(TSOut& SOut) const { - SOut.Save(uint64(MxBfL)); SOut.Save(uint64(BfL)); SOut.Save(GrowBy); - if (BfL > 0) { SOut.SaveBf(Bf, BfL); } - SOut.SaveCs(); - SOut.Save(IdOffV.Len()); - for (int i = 0; i < IdOffV.Len(); i++) { - SOut.Save(uint64(IdOffV[i])); - } -} - -TBigStrPool& TBigStrPool::operator = (const TBigStrPool& Pool) { - if (this != &Pool) { - GrowBy = Pool.GrowBy; MxBfL = Pool.MxBfL; BfL = Pool.BfL; - if (Bf) free(Bf); else IAssert(MxBfL == 0); - Bf = (char *) malloc(MxBfL); IAssert(Bf); memcpy(Bf, Pool.Bf, BfL); - } - return *this; -} - -// Adds Len characters to pool. To append a null terminated string Len must be equal to strlen(s) + 1 -int TBigStrPool::AddStr(const char *Str, uint Len) { - IAssertR(Len > 0, "String too short (lenght includes the null character)"); //J: if (! Len) return -1; - Assert(Str); Assert(Len > 0); - if (Len == 1 && IdOffV.Len() > 0) { return 0; } // empty string - if (BfL + Len > MxBfL) { Resize(BfL + Len); } - memcpy(Bf + BfL, Str, Len); - TSize Pos = BfL; BfL += Len; - IdOffV.Add(Pos); - return IdOffV.Len()-1; -} - -int TBigStrPool::GetPrimHashCd(const char *CStr) { - return TStrHashF_DJB::GetPrimHashCd(CStr); -} - -int TBigStrPool::GetSecHashCd(const char *CStr) { - return TStrHashF_DJB::GetSecHashCd(CStr); -} - -///////////////////////////////////////////////// -// String-Hash-Functions - -// Md5-Hash-Function -int TStrHashF_Md5::GetPrimHashCd(const char *p) { - TMd5Sig sig = TStr(p); - return sig.GetPrimHashCd(); -} - -int TStrHashF_Md5::GetSecHashCd(const char *p) { - TMd5Sig sig = TStr(p); - return sig.GetSecHashCd(); -} - -int TStrHashF_Md5::GetPrimHashCd(const TStr& s) { - TMd5Sig sig(s); - return sig.GetPrimHashCd(); -} - -int TStrHashF_Md5::GetSecHashCd(const TStr& s) { - TMd5Sig sig(s); - return sig.GetSecHashCd(); -} - diff --git a/inst/include/glib-core/hash.h b/inst/include/glib-core/hash.h deleted file mode 100644 index 8390a61..0000000 --- a/inst/include/glib-core/hash.h +++ /dev/null @@ -1,1285 +0,0 @@ -#include "bd.h" -#include - -///////////////////////////////////////////////// -// Hash-Table-Key-Data -#pragma pack(push, 1) // pack class size -template -class THashKeyDat{ -public: - TInt Next; - TInt HashCd; - TKey Key; - TDat Dat; -public: - THashKeyDat(): - Next(-1), HashCd(-1), Key(), Dat(){} - THashKeyDat(const int& _Next, const int& _HashCd, const TKey& _Key): - Next(_Next), HashCd(_HashCd), Key(_Key), Dat(){} - explicit THashKeyDat(TSIn& SIn): - Next(SIn), HashCd(SIn), Key(SIn), Dat(SIn){} - void Save(TSOut& SOut) const { - Next.Save(SOut); HashCd.Save(SOut); Key.Save(SOut); Dat.Save(SOut);} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); - void SaveXml(TSOut& SOut, const TStr& Nm) const; - - template - void LoadShM(TShMIn& ShMIn, TDatFunctor LoadDatFromShared) { - Next = TInt(ShMIn); - HashCd = TInt(ShMIn); - TKey K(ShMIn); - Key = K; - LoadDatFromShared(&Dat, ShMIn); - } - bool operator==(const THashKeyDat& HashKeyDat) const { - if (this==&HashKeyDat || (HashCd==HashKeyDat.HashCd - && Key==HashKeyDat.Key && Dat==HashKeyDat.Dat)){return true;} - return false;} - THashKeyDat& operator=(const THashKeyDat& HashKeyDat){ - if (this!=&HashKeyDat){ - Next=HashKeyDat.Next; HashCd=HashKeyDat.HashCd; - Key=HashKeyDat.Key; Dat=HashKeyDat.Dat;} - return *this;} -}; -#pragma pack(pop) - -///////////////////////////////////////////////// -// Hash-Table-Key-Data-Iterator -template -class THashKeyDatI{ -public: - typedef THashKeyDat THKeyDat; -private: - THKeyDat* KeyDatI; - THKeyDat* EndI; -public: - THashKeyDatI(): KeyDatI(NULL), EndI(NULL){} - THashKeyDatI(const THashKeyDatI& _HashKeyDatI): - KeyDatI(_HashKeyDatI.KeyDatI), EndI(_HashKeyDatI.EndI){} - THashKeyDatI(const THKeyDat* _KeyDatI, const THKeyDat* _EndI): - KeyDatI((THKeyDat*)_KeyDatI), EndI((THKeyDat*)_EndI){} - - THashKeyDatI& operator=(const THashKeyDatI& HashKeyDatI){ - KeyDatI=HashKeyDatI.KeyDatI; EndI=HashKeyDatI.EndI; return *this;} - bool operator==(const THashKeyDatI& HashKeyDatI) const { - return KeyDatI==HashKeyDatI.KeyDatI;} - bool operator<(const THashKeyDatI& HashKeyDatI) const { - return KeyDatIHashCd==-1) { KeyDatI++; } return *this; } - THashKeyDatI& operator--(int){ do { KeyDatI--; } while (KeyDatI->HashCd==-1); return *this;} - THKeyDat& operator*() const { return *KeyDatI; } - THKeyDat& operator()() const { return *KeyDatI; } - THKeyDat* operator->() const { return KeyDatI; } - THashKeyDatI& Next(){ operator++(1); return *this; } - - /// Tests whether the iterator has been initialized. - bool IsEmpty() const { return KeyDatI == NULL; } - /// Tests whether the iterator is pointing to the past-end element. - bool IsEnd() const { return EndI == KeyDatI; } - - const TKey& GetKey() const {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Key;} - const TDat& GetDat() const {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Dat;} - TDat& GetDat() {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Dat;} -}; - -///////////////////////////////////////////////// -// Default-Hash-Function -template -class TDefaultHashFunc { -public: - static inline int GetPrimHashCd(const TKey& Key) { return Key.GetPrimHashCd(); } - static inline int GetSecHashCd(const TKey& Key) { return Key.GetSecHashCd(); } -}; - -///////////////////////////////////////////////// -// Hash-Table -template > -class THash{ -public: - enum {HashPrimes=32}; - static const unsigned int HashPrimeT[HashPrimes]; -public: - typedef THashKeyDatI TIter; -private: - typedef THashKeyDat THKeyDat; - typedef TPair TKeyDatP; - TIntV PortV; - TVec KeyDatV; - TBool AutoSizeP; - TInt FFreeKeyId, FreeKeys; -private: - class THashKeyDatCmp { - public: - const THash& Hash; - bool CmpKey, Asc; - THashKeyDatCmp(THash& _Hash, const bool& _CmpKey, const bool& _Asc) : - Hash(_Hash), CmpKey(_CmpKey), Asc(_Asc) { } - bool operator () (const int& KeyId1, const int& KeyId2) const { - if (CmpKey) { - if (Asc) { return Hash.GetKey(KeyId1) < Hash.GetKey(KeyId2); } - else { return Hash.GetKey(KeyId2) < Hash.GetKey(KeyId1); } } - else { - if (Asc) { return Hash[KeyId1] < Hash[KeyId2]; } - else { return Hash[KeyId2] < Hash[KeyId1]; } } } - }; - - template - class TLoadTHKeyDatInitializer { - private: - TDatInitFn DatInitFn; - public: - TLoadTHKeyDatInitializer(TDatInitFn Fn) { DatInitFn = Fn;} - void operator() (THKeyDat* HKeyDat, TShMIn& ShMIn) { HKeyDat->LoadShM(ShMIn, DatInitFn);} - }; - -private: - THKeyDat& GetHashKeyDat(const int& KeyId){ - THKeyDat& KeyDat=KeyDatV[KeyId]; - Assert(KeyDat.HashCd!=-1); return KeyDat;} - const THKeyDat& GetHashKeyDat(const int& KeyId) const { - const THKeyDat& KeyDat=KeyDatV[KeyId]; - Assert(KeyDat.HashCd!=-1); return KeyDat;} - uint GetNextPrime(const uint& Val) const; - void Resize(); -public: - THash(): - PortV(), KeyDatV(), - AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0){} - THash(const THash& Hash): - PortV(Hash.PortV), KeyDatV(Hash.KeyDatV), AutoSizeP(Hash.AutoSizeP), - FFreeKeyId(Hash.FFreeKeyId), FreeKeys(Hash.FreeKeys){} - explicit THash(const int& ExpectVals, const bool& _AutoSizeP=false); - explicit THash(TSIn& SIn): - PortV(SIn), KeyDatV(SIn), - AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){ - SIn.LoadCs();} - /// Load THash from shared memory file. Copying/Deleting Keys is illegal - void LoadShM(TShMIn& ShMIn) { - PortV.LoadShM(ShMIn); - KeyDatV.Load(ShMIn); - AutoSizeP=TBool(ShMIn); - FFreeKeyId=TInt(ShMIn); - FreeKeys=TInt(ShMIn); - ShMIn.LoadCs(); - } - /// Load THash from shared memory passing in the Dat initializer - template - void LoadShM(TShMIn& ShMIn, TDatInitFn Fn) { - TLoadTHKeyDatInitializer HKeyDatFn(Fn); - PortV.LoadShM(ShMIn); - KeyDatV.LoadShM(ShMIn, HKeyDatFn); - AutoSizeP=TBool(ShMIn); - FFreeKeyId=TInt(ShMIn); - FreeKeys=TInt(ShMIn); - ShMIn.LoadCs(); - } - - void Load(TSIn& SIn){ - PortV.Load(SIn); KeyDatV.Load(SIn); - AutoSizeP=TBool(SIn); FFreeKeyId=TInt(SIn); FreeKeys=TInt(SIn); - SIn.LoadCs(); - } - - void Save(TSOut& SOut) const { - PortV.Save(SOut); KeyDatV.Save(SOut); - AutoSizeP.Save(SOut); FFreeKeyId.Save(SOut); FreeKeys.Save(SOut); - SOut.SaveCs();} - void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); - void SaveXml(TSOut& SOut, const TStr& Nm); - - THash& operator=(const THash& Hash){ - if (this!=&Hash){ - PortV=Hash.PortV; KeyDatV=Hash.KeyDatV; AutoSizeP=Hash.AutoSizeP; - FFreeKeyId=Hash.FFreeKeyId; FreeKeys=Hash.FreeKeys;} - return *this;} - bool operator==(const THash& Hash) const; //J: zdaj tak kot je treba - bool operator < (const THash& Hash) const { Fail; return true; } - /// The [] operator takes KeyId, use GetDat() if you need value access via the key. - const TDat& operator[](const int& KeyId) const {return GetHashKeyDat(KeyId).Dat;} - TDat& operator[](const int& KeyId){return GetHashKeyDat(KeyId).Dat;} - TDat& operator()(const TKey& Key){return AddDat(Key);} - ::TSize GetMemUsed() const { - // return PortV.GetMemUsed()+KeyDatV.GetMemUsed()+sizeof(bool)+2*sizeof(int);} - int64 MemUsed = sizeof(bool)+2*sizeof(int); - MemUsed += int64(PortV.Reserved()) * int64(sizeof(TInt)); - for (int KeyDatN = 0; KeyDatN < KeyDatV.Len(); KeyDatN++) { - MemUsed += int64(2 * sizeof(TInt)); - MemUsed += int64(KeyDatV[KeyDatN].Key.GetMemUsed()); - MemUsed += int64(KeyDatV[KeyDatN].Dat.GetMemUsed()); - } - return ::TSize(MemUsed); - } - - TIter BegI() const { - if (Len() == 0){return TIter(KeyDatV.EndI(), KeyDatV.EndI());} - if (IsKeyIdEqKeyN()) { return TIter(KeyDatV.BegI(), KeyDatV.EndI());} - int FKeyId=-1; FNextKeyId(FKeyId); - return TIter(KeyDatV.BegI()+FKeyId, KeyDatV.EndI()); } - TIter EndI() const {return TIter(KeyDatV.EndI(), KeyDatV.EndI());} - //TIter GetI(const int& KeyId) const {return TIter(&KeyDatV[KeyId], KeyDatV.EndI());} - TIter GetI(const TKey& Key) const {return TIter(&KeyDatV[GetKeyId(Key)], KeyDatV.EndI());} - - void Gen(const int& ExpectVals){ - PortV.Gen(GetNextPrime(ExpectVals/2)); KeyDatV.Gen(ExpectVals, 0); - FFreeKeyId=-1; FreeKeys=0; PortV.PutAll(TInt(-1));} - - void Clr(const bool& DoDel=true, const int& NoDelLim=-1, const bool& ResetDat=true); - bool Empty() const {return Len()==0;} - int Len() const {return KeyDatV.Len()-FreeKeys;} - int GetPorts() const {return PortV.Len();} - bool IsAutoSize() const {return AutoSizeP;} - int GetMxKeyIds() const {return KeyDatV.Len();} - int GetReservedKeyIds() const {return KeyDatV.Reserved();} - bool IsKeyIdEqKeyN() const {return FreeKeys==0;} - - int AddKey(const TKey& Key); - TDat& AddDatId(const TKey& Key){ - int KeyId=AddKey(Key); return KeyDatV[KeyId].Dat=KeyId;} - TDat& AddDat(const TKey& Key){return KeyDatV[AddKey(Key)].Dat;} - TDat& AddDat(const TKey& Key, const TDat& Dat){ - return KeyDatV[AddKey(Key)].Dat=Dat;} - - void DelKey(const TKey& Key); - bool DelIfKey(const TKey& Key){ - int KeyId; if (IsKey(Key, KeyId)){DelKeyId(KeyId); return true;} return false;} - void DelKeyId(const int& KeyId){DelKey(GetKey(KeyId));} - void DelKeyIdV(const TIntV& KeyIdV){ - for (int KeyIdN=0; KeyIdN= 0 ? KeyDatV[KeyId].Dat : DefaultValue; - } -// TKeyDatP GetKeyDat(const int& KeyId) const { -// TKeyDat& KeyDat=GetHashKeyDat(KeyId); -// return TKeyDatP(KeyDat.Key, KeyDat.Dat);} - void GetKeyDat(const int& KeyId, TKey& Key, TDat& Dat) const { - const THKeyDat& KeyDat=GetHashKeyDat(KeyId); - Key=KeyDat.Key; Dat=KeyDat.Dat;} - bool IsKeyGetDat(const TKey& Key, TDat& Dat) const {int KeyId; - if (IsKey(Key, KeyId)){Dat=GetHashKeyDat(KeyId).Dat; return true;} - else {return false;}} - - int FFirstKeyId() const {return 0-1;} - bool FNextKeyId(int& KeyId) const; - void GetKeyV(TVec& KeyV) const; - void GetDatV(TVec& DatV) const; - void GetKeyDatPrV(TVec >& KeyDatPrV) const; - void GetDatKeyPrV(TVec >& DatKeyPrV) const; - void GetKeyDatKdV(TVec >& KeyDatKdV) const; - void GetDatKeyKdV(TVec >& DatKeyKdV) const; - - void Swap(THash& Hash); - void Defrag(); - void Pack(){KeyDatV.Pack();} - void Sort(const bool& CmpKey, const bool& Asc); - void SortByKey(const bool& Asc=true) { Sort(true, Asc); } - void SortByDat(const bool& Asc=true) { Sort(false, Asc); } -}; - -template -const unsigned int THash::HashPrimeT[HashPrimes]={ - 3ul, 5ul, 11ul, 23ul, - 53ul, 97ul, 193ul, 389ul, 769ul, - 1543ul, 3079ul, 6151ul, 12289ul, 24593ul, - 49157ul, 98317ul, 196613ul, 393241ul, 786433ul, - 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, - 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, - 1610612741ul, 3221225473ul, 4294967291ul -}; - -template -uint THash::GetNextPrime(const uint& Val) const { - const uint* f=(const uint*)HashPrimeT, *m, *l=(const uint*)HashPrimeT + (int)HashPrimes; - int h, len = (int)HashPrimes; - while (len > 0) { - h = len >> 1; m = f + h; - if (*m < Val) { f = m; f++; len = len - h - 1; } - else len = h; - } - return f == l ? *(l - 1) : *f; -} - -template -void THash::Resize(){ - // resize & initialize port vector - //if (PortV.Len()==0){PortV.Gen(17);} - //else {PortV.Gen(2*PortV.Len()+1);} - if (PortV.Len()==0){ - PortV.Gen(17); - } else if (AutoSizeP&&(KeyDatV.Len()>2*PortV.Len())){ - PortV.Gen(GetNextPrime(PortV.Len()+1)); - } else { - return; - } - PortV.PutAll(TInt(-1)); - // rehash keys - for (int KeyId=0; KeyId -THash::THash(const int& ExpectVals, const bool& _AutoSizeP): - PortV(GetNextPrime(ExpectVals/2)), KeyDatV(ExpectVals, 0), - AutoSizeP(_AutoSizeP), FFreeKeyId(-1), FreeKeys(0){ - PortV.PutAll(TInt(-1)); -} - -template -bool THash::operator==(const THash& Hash) const { - if (Len() != Hash.Len()) { return false; } - for (int i = FFirstKeyId(); FNextKeyId(i); ) { - const TKey& Key = GetKey(i); - if (! Hash.IsKey(Key)) { return false; } - if (GetDat(Key) != Hash.GetDat(Key)) { return false; } - } - return true; -} - -template -void THash::Clr(const bool& DoDel, const int& NoDelLim, const bool& ResetDat){ - if (DoDel){ - PortV.Clr(); KeyDatV.Clr(); - } else { - PortV.PutAll(TInt(-1)); - KeyDatV.Clr(DoDel, NoDelLim); - if (ResetDat){KeyDatV.PutAll(THKeyDat());} - } - FFreeKeyId=TInt(-1); FreeKeys=TInt(0); -} - -template -int THash::AddKey(const TKey& Key){ - if ((KeyDatV.Len()>2*PortV.Len())||PortV.Empty()){Resize();} - const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len()); - const int HashCd=abs(THashFunc::GetSecHashCd(Key)); - int PrevKeyId=-1; - int KeyId=PortV[PortN]; - while ((KeyId!=-1) && - !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){ - PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;} - - if (KeyId==-1){ - if (FFreeKeyId==-1){ - KeyId=KeyDatV.Add(THKeyDat(-1, HashCd, Key)); - } else { - KeyId=FFreeKeyId; FFreeKeyId=KeyDatV[FFreeKeyId].Next; FreeKeys--; - //KeyDatV[KeyId]=TKeyDat(-1, HashCd, Key); // slow version - KeyDatV[KeyId].Next=-1; - KeyDatV[KeyId].HashCd=HashCd; - KeyDatV[KeyId].Key=Key; - //KeyDatV[KeyId].Dat=TDat(); // already empty - } - if (PrevKeyId==-1){ - PortV[PortN]=KeyId; - } else { - KeyDatV[PrevKeyId].Next=KeyId; - } - } - return KeyId; -} - -template -void THash::DelKey(const TKey& Key){ - IAssert(!PortV.Empty()); - const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len()); - const int HashCd=abs(THashFunc::GetSecHashCd(Key)); - int PrevKeyId=-1; - int KeyId=PortV[PortN]; - - while ((KeyId!=-1) && - !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){ - PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;} - - //IAssertR(KeyId!=-1, Key.GetStr()); //J: some classes do not provide GetStr()? - IAssert(KeyId!=-1); //J: some classes do not provide GetStr()? - if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;} - else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;} - KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++; - KeyDatV[KeyId].HashCd=TInt(-1); - KeyDatV[KeyId].Key=TKey(); - KeyDatV[KeyId].Dat=TDat(); -} - -template -void THash::MarkDelKey(const TKey& Key){ - // MarkDelKey is same as Delkey except last two lines - IAssert(!PortV.Empty()); - const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len()); - const int HashCd=abs(THashFunc::GetSecHashCd(Key)); - int PrevKeyId=-1; - int KeyId=PortV[PortN]; - while ((KeyId!=-1) && - !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){ - PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;} - IAssertR(KeyId!=-1, Key.GetStr()); - if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;} - else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;} - KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++; - KeyDatV[KeyId].HashCd=TInt(-1); -} - -template -int THash::GetRndKeyId(TRnd& Rnd) const { - IAssert(! Empty()); - int KeyId = abs(Rnd.GetUniDevInt(KeyDatV.Len())); - while (KeyDatV[KeyId].HashCd == -1) { // if the index is empty, just try again - KeyId = abs(Rnd.GetUniDevInt(KeyDatV.Len())); } - return KeyId; -} - -// return random KeyId even if the hash table contains deleted keys -// defrags the table if necessary -template -int THash::GetRndKeyId(TRnd& Rnd, const double& EmptyFrac) { - IAssert(! Empty()); - if (FreeKeys/double(Len()+FreeKeys) > EmptyFrac) { Defrag(); } - int KeyId = Rnd.GetUniDevInt(KeyDatV.Len()); - while (KeyDatV[KeyId].HashCd == -1) { // if the index is empty, just try again - KeyId = Rnd.GetUniDevInt(KeyDatV.Len()); - } - return KeyId; -} - -template -int THash::GetKeyId(const TKey& Key) const { - if (PortV.Empty()){return -1;} - const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len()); - const int HashCd=abs(THashFunc::GetSecHashCd(Key)); - int KeyId=PortV[PortN]; - while ((KeyId!=-1) && - !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){ - KeyId=KeyDatV[KeyId].Next;} - return KeyId; -} - -template -bool THash::FNextKeyId(int& KeyId) const { - do {KeyId++;} while ((KeyId -void THash::GetKeyV(TVec& KeyV) const { - KeyV.Gen(Len(), 0); - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - KeyV.Add(GetKey(KeyId));} -} - -template -void THash::GetDatV(TVec& DatV) const { - DatV.Gen(Len(), 0); - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - DatV.Add(GetHashKeyDat(KeyId).Dat);} -} - -template -void THash::GetKeyDatPrV(TVec >& KeyDatPrV) const { - KeyDatPrV.Gen(Len(), 0); - TKey Key; TDat Dat; - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - KeyDatPrV.Add(TPair(Key, Dat)); - } -} - -template -void THash::GetDatKeyPrV(TVec >& DatKeyPrV) const { - DatKeyPrV.Gen(Len(), 0); - TKey Key; TDat Dat; - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - DatKeyPrV.Add(TPair(Dat, Key)); - } -} - -template -void THash::GetKeyDatKdV(TVec >& KeyDatKdV) const { - KeyDatKdV.Gen(Len(), 0); - TKey Key; TDat Dat; - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - KeyDatKdV.Add(TKeyDat(Key, Dat)); - } -} - -template -void THash::GetDatKeyKdV(TVec >& DatKeyKdV) const { - DatKeyKdV.Gen(Len(), 0); - TKey Key; TDat Dat; - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - DatKeyKdV.Add(TKeyDat(Dat, Key)); - } -} - -template -void THash::Swap(THash& Hash) { - if (this!=&Hash){ - PortV.Swap(Hash.PortV); - KeyDatV.Swap(Hash.KeyDatV); - ::Swap(AutoSizeP, Hash.AutoSizeP); - ::Swap(FFreeKeyId, Hash.FFreeKeyId); - ::Swap(FreeKeys, Hash.FreeKeys); - } -} - -template -void THash::Defrag(){ - if (!IsKeyIdEqKeyN()){ - THash Hash(PortV.Len()); - int KeyId=FFirstKeyId(); TKey Key; TDat Dat; - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - Hash.AddDat(Key, Dat); - } - Pack(); - operator=(Hash); - IAssert(IsKeyIdEqKeyN()); - } -} - -template -void THash::Sort(const bool& CmpKey, const bool& Asc) { - IAssertR(IsKeyIdEqKeyN(), "THash::Sort only works when table has no deleted keys."); - TIntV TargV(Len()), MapV(Len()), StateV(Len()); - for (int i = 0; i < TargV.Len(); i++) { - TargV[i] = i; MapV[i] = i; StateV[i] = i; - } - // sort KeyIds - THashKeyDatCmp HashCmp(*this, CmpKey, Asc); - TargV.SortCmp(HashCmp); - // now sort the update vector - THashKeyDat Tmp; - for (int i = 0; i < TargV.Len()-1; i++) { - const int SrcPos = MapV[TargV[i]]; - const int Loc = i; - // swap data - Tmp = KeyDatV[SrcPos]; - KeyDatV[SrcPos] = KeyDatV[Loc]; - KeyDatV[Loc] = Tmp; - // swap keys - MapV[StateV[i]] = SrcPos; - StateV.Swap(Loc, SrcPos); - } - for (int i = 0; i < TargV.Len(); i++) { - MapV[TargV[i]] = i; } - for (int p = 0; p < PortV.Len(); p++) { - if (PortV[p] != -1) { - PortV[p] = MapV[PortV[p]]; } } - for (int i = 0; i < KeyDatV.Len(); i++) { - if (KeyDatV[i].Next != -1) { - KeyDatV[i].Next = MapV[KeyDatV[i].Next]; } - } -} - -///////////////////////////////////////////////// -// Common-Hash-Types -typedef THash TChChH; -typedef THash TChTrIntH; -typedef THash TIntH; -typedef THash TUInt64H; -typedef THash TIntBoolH; -typedef THash TIntIntH; -typedef THash TIntUInt64H; -typedef THash TIntIntFltPrH; -typedef THash TIntIntVH; -typedef THash TIntIntHH; -typedef THash TIntFltH; -typedef THash TIntFltPrH; -typedef THash TIntFltTrH; -typedef THash TIntFltVH; -typedef THash TIntStrH; -typedef THash TIntStrVH; -typedef THash TIntStrPrVH; -typedef THash TIntIntPrH; -typedef THash TIntIntPrVH; -typedef THash TIntIntStrPrH; -typedef THash TIntIntVFltVPrH; -typedef THash TUInt64StrVH; -typedef THash TIntPrIntH; -typedef THash TIntPrH; -typedef THash TIntPrIntVH; -typedef THash TIntPrIntPrVH; -typedef THash TIntTrIntH; -typedef THash TIntVIntH; -typedef THash TUIntH; -typedef THash TIntPrIntH; -typedef THash TIntPrIntVH; -typedef THash TIntPrFltH; -typedef THash TIntTrFltH; -typedef THash TIntPrStrH; -typedef THash TIntPrStrVH; -typedef THash TIntStrPrIntH; -typedef THash TIntIntPrPrIntH; -typedef THash TIntIntPrPrFltH; -typedef THash TIntIntPrPrStrH; -typedef THash TFltFltH; -typedef THash TStrH; -typedef THash TStrBoolH; -typedef THash TStrIntH; -typedef THash TStrIntPrH; -typedef THash TStrIntVH; -typedef THash TStrUInt64H; -typedef THash TStrUInt64VH; -typedef THash TStrIntPrVH; -typedef THash TStrFltH; -typedef THash TStrFltVH; -typedef THash TStrStrH; -typedef THash TStrStrPrH; -typedef THash TStrStrVH; -typedef THash TStrStrPrVH; -typedef THash TStrStrKdVH; -typedef THash TStrIntFltPrH; -typedef THash TStrStrIntPrVH; -typedef THash TStrStrIntKdVH; -typedef THash TDbStrIntH; -typedef THash TDbStrStrH; -typedef THash TStrPrBoolH; -typedef THash TStrPrIntH; -typedef THash TStrPrFltH; -typedef THash TStrPrStrH; -typedef THash TStrPrStrVH; -typedef THash TStrTrIntH; -typedef THash TStrIntPrIntH; -typedef THash TStrVH; -typedef THash TStrVIntH; -typedef THash TStrVIntVH; -typedef THash TStrVStrH; -typedef THash TStrVStrVH; - -///////////////////////////////////////////////// -// Hash-Pointer -template -class PHash{ -private: - TCRef CRef; -public: - THash H; -public: - PHash(): H(){} - static TPt > New(){ - return new PHash();} - PHash(const int& MxVals, const int& Vals): H(MxVals, Vals){} - static TPt > New(const int& MxVals, const int& Vals){ - return new PHash(MxVals, Vals);} - PHash(const THash& _V): H(_V){} - static TPt > New(const THash& H){ - return new PHash(H);} - explicit PHash(TSIn& SIn): H(SIn){} - static TPt > Load(TSIn& SIn){return new PHash(SIn);} - void Save(TSOut& SOut) const {H.Save(SOut);} - - PHash& operator=(const PHash& Vec){ - if (this!=&Vec){H=Vec.H;} return *this;} - bool operator==(const PHash& Vec) const {return H==Vec.H;} - bool operator<(const PHash& Vec) const {return H >; -}; - -///////////////////////////////////////////////// -// Big-String-Pool (holds up to 2 giga strings, storage overhead is 8(4) bytes per string) -//J: have to put it here since it uses TVec (can't be in dt.h) -ClassTP(TBigStrPool, PBigStrPool)//{ -private: - TSize MxBfL, BfL; - uint GrowBy; - char *Bf; - TVec IdOffV; // string ID to offset - bool IsShM; //True if BigStrPool backed by shared memory -private: - void Resize(TSize _MxBfL); - void LoadPoolShM(TShMIn& ShMIn, bool LoadCompact = true); -public: - TBigStrPool(TSize MxBfLen = 0, uint _GrowBy = 16*1024*1024); - TBigStrPool(TSIn& SIn, bool LoadCompact = true); - TBigStrPool(const TBigStrPool& Pool) : MxBfL(Pool.MxBfL), BfL(Pool.BfL), GrowBy(Pool.GrowBy) { - Bf = (char *) malloc(Pool.MxBfL); IAssert(Bf); memcpy(Bf, Pool.Bf, Pool.BfL); } - ~TBigStrPool() { if (Bf && !IsShM) free(Bf); else IAssert(MxBfL == 0 || IsShM); MxBfL = 0; BfL = 0; } - - static PBigStrPool New(TSize _MxBfLen = 0, uint _GrowBy = 16*1024*1024) { return PBigStrPool(new TBigStrPool(_MxBfLen, _GrowBy)); } - static PBigStrPool New(TSIn& SIn) { return new TBigStrPool(SIn); } - static PBigStrPool New(const TStr& fileName) { PSIn SIn = TFIn::New(fileName); return new TBigStrPool(*SIn); } - static PBigStrPool Load(TSIn& SIn, bool LoadCompacted = true) { return PBigStrPool(new TBigStrPool(SIn, LoadCompacted)); } - /// Load the string pool with the buffer backed by shared memory - static PBigStrPool LoadShM(TShMIn& ShMIn, bool LoadCompact = true) { - TBigStrPool* StrPool = new TBigStrPool(); - StrPool->LoadPoolShM(ShMIn, LoadCompact); - return PBigStrPool(StrPool); - } - void Save(TSOut& SOut) const; - void Save(const TStr& fileName) { TFOut FOut(fileName); Save(FOut); } - - int GetStrs() const { return IdOffV.Len(); } - TSize Len() const { return BfL; } - TSize Size() const { return MxBfL; } - bool Empty() const { return ! Len(); } - char* operator () () const { return Bf; } - TBigStrPool& operator = (const TBigStrPool& Pool); - ::TSize GetMemUsed(){ - return 4 * sizeof(int) + IdOffV.GetMemUsed() + MxBfL; - } - - int AddStr(const char *Str, uint Len); - int AddStr(const char *Str) { return AddStr(Str, uint(strlen(Str)) + 1); } - int AddStr(const TStr& Str) { return AddStr(Str.CStr(), Str.Len() + 1); } - - TStr GetStr(const int& StrId) const { Assert(StrId < GetStrs()); - if (StrId == 0) return TStr::GetNullStr(); else return TStr(Bf + (TSize)IdOffV[StrId]); } - const char *GetCStr(const int& StrId) const { Assert(StrId < GetStrs()); - if (StrId == 0) return TStr::GetNullStr().CStr(); else return (Bf + (TSize)IdOffV[StrId]); } - - TStr GetStrFromOffset(const TSize& Offset) const { Assert(Offset < BfL); - if (Offset == 0) return TStr::GetNullStr(); else return TStr(Bf + Offset); } - const char *GetCStrFromOffset(const TSize& Offset) const { Assert(Offset < BfL); - if (Offset == 0) return TStr::GetNullStr().CStr(); else return Bf + Offset; } - - void Clr(bool DoDel = false) { - BfL = 0; if (DoDel && Bf) {if (!IsShM) { free(Bf);} Bf = 0; MxBfL = 0;}} - int Cmp(const int& StrId, const char *Str) const { Assert(StrId < GetStrs()); - if (StrId != 0) return strcmp(Bf + (TSize)IdOffV[StrId], Str); else return strcmp("", Str); } - - static int GetPrimHashCd(const char *CStr); - static int GetSecHashCd(const char *CStr); - int GetPrimHashCd(const int& StrId) { Assert(StrId < GetStrs()); - if (StrId != 0) return GetPrimHashCd(Bf + (TSize)IdOffV[StrId]); else return GetPrimHashCd(""); } - int GetSecHashCd(const int& StrId) { Assert(StrId < GetStrs()); - if (StrId != 0) return GetSecHashCd(Bf + (TSize)IdOffV[StrId]); else return GetSecHashCd(""); } -}; - -///////////////////////////////////////////////// -// String-Hash-Table -template > -class TStrHash{ -public: - //typedef typename PStringPool::TObj TStringPool; - typedef TPt PStringPool; -private: - typedef THashKeyDat THKeyDat; - typedef TPair TKeyDatP; - typedef TVec THKeyDatV; - TIntV PortV; - THKeyDatV KeyDatV; - TBool AutoSizeP; - TInt FFreeKeyId, FreeKeys; - PStringPool Pool; -private: - uint GetNextPrime(const uint& Val) const; - void Resize(); - const THKeyDat& GetHashKeyDat(const int& KeyId) const { - const THKeyDat& KeyDat = KeyDatV[KeyId]; Assert(KeyDat.HashCd != -1); return KeyDat; } - THKeyDat& GetHashKeyDat(const int& KeyId) { - THKeyDat& KeyDat = KeyDatV[KeyId]; Assert(KeyDat.HashCd != -1); return KeyDat; } -public: - TStrHash(): PortV(), KeyDatV(), AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0), Pool() { } - TStrHash(const PStringPool& StrPool): PortV(), KeyDatV(), AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0), Pool(StrPool) { } - TStrHash(const int& Ports, const bool& _AutoSizeP = false, const PStringPool& StrPool = PStringPool()) : - PortV(Ports), KeyDatV(Ports, 0), AutoSizeP(_AutoSizeP), FFreeKeyId(-1), FreeKeys(0), Pool(StrPool) { PortV.PutAll(-1); } - TStrHash(const TStrHash& Hash): PortV(Hash.PortV), KeyDatV(Hash.KeyDatV), AutoSizeP(Hash.AutoSizeP), - FFreeKeyId(Hash.FFreeKeyId), FreeKeys(Hash.FreeKeys), Pool() { - if (! Hash.Pool.Empty()) { Pool=PStringPool(new TStringPool(*Hash.Pool)); } } - TStrHash(TSIn& SIn, bool PoolToo = true): PortV(SIn), KeyDatV(SIn), AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){ SIn.LoadCs(); if (PoolToo) Pool = PStringPool(SIn); } - - void Load(TSIn& SIn, bool PoolToo = true) {PortV.Load(SIn); KeyDatV.Load(SIn); AutoSizeP.Load(SIn); FFreeKeyId.Load(SIn); - FreeKeys.Load(SIn); SIn.LoadCs(); if (PoolToo) Pool = PStringPool(SIn);} - - /// Load hash from shared memory. If shared pool is true load pool from shared memory - void LoadShM(TShMIn& ShMIn, bool SharedPool=true) { - PortV.LoadShM(ShMIn); - KeyDatV.Load(ShMIn); - AutoSizeP.Load(ShMIn); - FFreeKeyId.Load(ShMIn); - FreeKeys.Load(ShMIn); - ShMIn.LoadCs(); - if (SharedPool) { - TBool isNull; - isNull.Load(ShMIn); - if (!isNull) { - Pool = TStringPool::LoadShM(ShMIn); - } - } else { - Pool = PStringPool(ShMIn); - } - } - - void Save(TSOut& SOut, bool PoolToo = true) const { PortV.Save(SOut); KeyDatV.Save(SOut); - AutoSizeP.Save(SOut); FFreeKeyId.Save(SOut); FreeKeys.Save(SOut); SOut.SaveCs(); if (PoolToo) Pool.Save(SOut); } - - void SetPool(const PStringPool& StrPool) { IAssert(Pool.Empty() || Pool->Empty()); Pool = StrPool; } - PStringPool GetPool() const { return Pool; } - - TStrHash& operator = (const TStrHash& Hash); - - bool Empty() const {return ! Len(); } - int Len() const { return KeyDatV.Len() - FreeKeys; } - int Reserved() const { return KeyDatV.Reserved(); } - int GetPorts() const { return PortV.Len(); } - bool IsAutoSize() const { return AutoSizeP; } - int GetMxKeyIds() const { return KeyDatV.Len(); } - bool IsKeyIdEqKeyN() const {return ! FreeKeys; } - - int AddKey(const char *Key); - int AddKey(const TStr& Key) { return AddKey(Key.CStr()); } - int AddKey(const TChA& Key) { return AddKey(Key.CStr()); } - int AddDat(const char *Key, const TDat& Dat) { const int KeyId = AddKey(Key); KeyDatV[KeyId].Dat = Dat; return KeyId; } - int AddDat(const TStr& Key, const TDat& Dat) { const int KeyId = AddKey(Key.CStr()); KeyDatV[KeyId].Dat = Dat; return KeyId; } - int AddDat(const TChA& Key, const TDat& Dat) { const int KeyId = AddKey(Key.CStr()); KeyDatV[KeyId].Dat = Dat; return KeyId; } - TDat& AddDat(const char *Key) { return KeyDatV[AddKey(Key)].Dat; } - TDat& AddDat(const TStr& Key) { return KeyDatV[AddKey(Key.CStr())].Dat; } - TDat& AddDat(const TChA& Key) { return KeyDatV[AddKey(Key.CStr())].Dat; } - TDat& AddDatId(const char *Key) { const int KeyId = AddKey(Key); return KeyDatV[KeyId].Dat = KeyId; } - TDat& AddDatId(const TStr& Key) { const int KeyId = AddKey(Key.CStr()); return KeyDatV[KeyId].Dat = KeyId; } - TDat& AddDatId(const TChA& Key) { const int KeyId = AddKey(Key.CStr()); return KeyDatV[KeyId].Dat = KeyId; } - - const TDat& operator[](const int& KeyId) const {return GetHashKeyDat(KeyId).Dat;} - TDat& operator[](const int& KeyId){return GetHashKeyDat(KeyId).Dat;} - const TDat& operator () (const char *Key) const { return GetDat(Key);} - //TDat& operator ()(const char *Key){return AddDat(Key);} // add if not found - ::TSize GetMemUsed() const { - int64 MemUsed = sizeof(bool)+2*sizeof(int); - MemUsed += int64(PortV.Reserved()) * int64(sizeof(TInt)); - for (int KeyDatN = 0; KeyDatN < KeyDatV.Len(); KeyDatN++) { - MemUsed += int64(2 * sizeof(TInt)); - MemUsed += int64(KeyDatV[KeyDatN].Key.GetMemUsed()); - MemUsed += int64(KeyDatV[KeyDatN].Dat.GetMemUsed()); - } - // printf("TStrHash: Memory used for hash table: %s\n", TUInt64::GetStr(MemUsed).CStr()); - MemUsed += 8 + Pool->GetMemUsed(); - return ::TSize(MemUsed/1000); - } - - const TDat& GetDat(const char *Key) const { return KeyDatV[GetKeyId(Key)].Dat; } - const TDat& GetDat(const TStr& Key) const { return GetDat(Key.CStr()); } - TDat& GetDat(const char *Key) { return KeyDatV[GetKeyId(Key)].Dat; } - const TDat& GetDat(const TStr& Key) { return GetDat(Key.CStr()); } - const TDat& GetDat(const TChA& Key) { return GetDat(Key.CStr()); } - TDat& GetDatId(const int& KeyId) { return KeyDatV[KeyId].Dat; } - const TDat& GetDatId(const int& KeyId) const { return KeyDatV[KeyId].Dat; } - void GetKeyDat(const int& KeyId, int& KeyO, TDat& Dat) const { const THKeyDat& KeyDat = GetHashKeyDat(KeyId); KeyO = KeyDat.Key; Dat = KeyDat.Dat; } - void GetKeyDat(const int& KeyId, const char*& Key, TDat& Dat) const { const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat; } - void GetKeyDat(const int& KeyId, TStr& Key, TDat& Dat) const { const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat;} - void GetKeyDat(const int& KeyId, TChA& Key, TDat& Dat) const { const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat;} - - int GetKeyId(const char *Key) const; - int GetKeyId(const TStr& Key) const { return GetKeyId(Key.CStr()); } - const char *GetKey(const int& KeyId) const { return Pool->GetCStr(GetHashKeyDat(KeyId).Key); } - int GetKeyOfs(const int& KeyId) const { return GetHashKeyDat(KeyId).Key; } // pool string id - const char *KeyFromOfs(const int& KeyO) const { return Pool->GetCStr(KeyO); } - - bool IsKey(const char *Key) const { return GetKeyId(Key) != -1; } - bool IsKey(const TStr& Key) const { return GetKeyId(Key.CStr()) != -1; } - bool IsKey(const TChA& Key) const { return GetKeyId(Key.CStr()) != -1; } - bool IsKey(const char *Key, int& KeyId) const { KeyId = GetKeyId(Key); return KeyId != -1; } - bool IsKeyGetDat(const char *Key, TDat& Dat) const { const int KeyId = GetKeyId(Key); if (KeyId != -1) { Dat = KeyDatV[KeyId].Dat; return true; } else return false; } - bool IsKeyGetDat(const TStr& Key, TDat& Dat) const { const int KeyId = GetKeyId(Key.CStr()); if (KeyId != -1) { Dat = KeyDatV[KeyId].Dat; return true; } else return false; } - bool IsKeyGetDat(const TChA& Key, TDat& Dat) const { const int KeyId = GetKeyId(Key.CStr()); if (KeyId != -1) { Dat = KeyDatV[KeyId].Dat; return true; } else return false; } - bool IsKeyId(const int& KeyId) const { return 0 <= KeyId && KeyId < KeyDatV.Len() && KeyDatV[KeyId].HashCd != -1; } - - int FFirstKeyId() const {return 0-1;} - bool FNextKeyId(int& KeyId) const; - - void GetKeyV(TVec& KeyV) const; - void GetStrIdV(TIntV& StrIdV) const; - void GetDatV(TVec& DatV) const; - void GetKeyDatPrV(TVec >& KeyDatPrV) const; - void GetDatKeyPrV(TVec >& DatKeyPrV) const; - - void Pack(){KeyDatV.Pack();} -}; - -template -uint TStrHash::GetNextPrime(const uint& Val) const { - uint *f = (uint *) TIntH::HashPrimeT, *m, *l = (uint *) TIntH::HashPrimeT + (int) TIntH::HashPrimes; - int h, len = (int)TIntH::HashPrimes; - while (len > 0) { - h = len >> 1; m = f + h; - if (*m < Val) { f = m; f++; len = len - h - 1; } - else len = h; - } - return f == l ? *(l - 1) : *f; -} - -template -void TStrHash::Resize() { - // resize & initialize port vector - if (PortV.Empty()) { PortV.Gen(17); PortV.PutAll(-1); } - else - if (AutoSizeP && KeyDatV.Len() > 3 * PortV.Len()) { - const int NxPrime = GetNextPrime(KeyDatV.Len()); - //printf("%s resize PortV: %d -> %d, Len: %d\n", GetTypeNm(*this).CStr(), PortV.Len(), NxPrime, Len()); - PortV.Gen(NxPrime); PortV.PutAll(-1); } - else - return; - // rehash keys - const int NPorts = PortV.Len(); - for (int i = 0; i < KeyDatV.Len(); i++) { - THKeyDat& KeyDat = KeyDatV[i]; - if (KeyDat.HashCd != -1) { - const int Port = abs(THashFunc::GetPrimHashCd(Pool->GetCStr(KeyDat.Key)) % NPorts); - KeyDat.Next = PortV[Port]; - PortV[Port] = i; - } - } -} - -template -TStrHash& TStrHash:: operator = (const TStrHash& Hash) { - if (this != &Hash) { - PortV = Hash.PortV; - KeyDatV = Hash.KeyDatV; - AutoSizeP = Hash.AutoSizeP; - FFreeKeyId = Hash.FFreeKeyId; - FreeKeys = Hash.FreeKeys; - if (! Hash.Pool.Empty()) Pool = PStringPool(new TStringPool(*Hash.Pool)); - else Pool = NULL; - } - return *this; -} - -template -int TStrHash::AddKey(const char *Key) { - if (Pool.Empty()) Pool = TStringPool::New(); - if ((AutoSizeP && KeyDatV.Len() > PortV.Len()) || PortV.Empty()) Resize(); - const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.Len()); - const int HashCd = abs(THashFunc::GetSecHashCd(Key)); - int PrevKeyId = -1; - int KeyId = PortV[PortN]; - while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == HashCd && Pool->Cmp(KeyDatV[KeyId].Key, Key) == 0)) { - PrevKeyId = KeyId; KeyId = KeyDatV[KeyId].Next; } - if (KeyId == -1) { - const int StrId = Pool->AddStr(Key); - if (FFreeKeyId == -1) { - KeyId = KeyDatV.Add(THKeyDat(-1, HashCd, StrId)); - } else { - KeyId = FFreeKeyId; - FFreeKeyId = KeyDatV[FFreeKeyId].Next; - FreeKeys--; - KeyDatV[KeyId] = THKeyDat(-1, HashCd, StrId); - } - if (PrevKeyId == -1) PortV[PortN] = KeyId; - else KeyDatV[PrevKeyId].Next = KeyId; - } - return KeyId; -} - -template -int TStrHash::GetKeyId(const char *Key) const { - if (PortV.Empty()) return -1; - const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.Len()); - const int Hc = abs(THashFunc::GetSecHashCd(Key)); - int KeyId = PortV[PortN]; - while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == Hc && Pool->Cmp(KeyDatV[KeyId].Key, Key) == 0)) - KeyId = KeyDatV[KeyId].Next; - return KeyId; -} - -template -bool TStrHash::FNextKeyId(int& KeyId) const { - do KeyId++; while (KeyId < KeyDatV.Len() && KeyDatV[KeyId].HashCd == -1); - return KeyId < KeyDatV.Len(); -} - -template -void TStrHash::GetKeyV(TVec& KeyV) const { - KeyV.Gen(Len(), 0); - int KeyId = FFirstKeyId(); - while (FNextKeyId(KeyId)) - KeyV.Add(GetKey(KeyId)); -} - -template -void TStrHash::GetStrIdV(TIntV& StrIdV) const { - StrIdV.Gen(Len(), 0); - int KeyId = FFirstKeyId(); - while (FNextKeyId(KeyId)) - StrIdV.Add(GetKeyOfs(KeyId)); -} - -template -void TStrHash::GetDatV(TVec& DatV) const { - DatV.Gen(Len(), 0); - int KeyId = FFirstKeyId(); - while (FNextKeyId(KeyId)) - DatV.Add(GetHashKeyDat(KeyId).Dat); -} - -template -void TStrHash::GetKeyDatPrV(TVec >& KeyDatPrV) const { - KeyDatPrV.Gen(Len(), 0); - TStr Str; TDat Dat; - int KeyId = FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Str, Dat); - KeyDatPrV.Add(TPair(Str, Dat)); - } -} - -template -void TStrHash::GetDatKeyPrV(TVec >& DatKeyPrV) const { - DatKeyPrV.Gen(Len(), 0); - TStr Str; TDat Dat; - int KeyId = FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Str, Dat); - DatKeyPrV.Add(TPair(Dat, Str)); - } -} - -///////////////////////////////////////////////// -// Common-String-Hash-Types -typedef TStrHash TStrSH; -typedef TStrHash TStrIntSH; -typedef TStrHash TStrToIntVSH; - -///////////////////////////////////////////////// -// Cache -template > -class TCache{ -private: - typedef TLst TKeyL; typedef TLstNd* TKeyLN; - typedef TPair TKeyLNDatPr; - int64 MxMemUsed; - int64 CurMemUsed; - THash KeyDatH; - TKeyL TimeKeyL; - void* RefToBs; - void Purge(const int64& MemToPurge); -public: - TCache(){} - TCache(const TCache&); - TCache(const int64& _MxMemUsed, const int& Ports, void* _RefToBs): - MxMemUsed(_MxMemUsed), CurMemUsed(0), - KeyDatH(Ports), TimeKeyL(), RefToBs(_RefToBs){} - - TCache& operator=(const TCache&); - int64 GetMemUsed() const; - int64 GetMxMemUsed() const { return MxMemUsed; } - bool RefreshMemUsed(); - - void Put(const TKey& Key, const TDat& Dat); - bool Get(const TKey& Key, TDat& Dat); - void Del(const TKey& Key, const bool& DoEventCall=true); - void Flush(); - void FlushAndClr(); - void* FFirstKeyDat(); - bool FNextKeyDat(void*& KeyDatP, TKey& Key, TDat& Dat); - - void PutRefToBs(void* _RefToBs){RefToBs=_RefToBs;} - void* GetRefToBs(){return RefToBs;} -}; - -template -void TCache::Purge(const int64& MemToPurge){ - const int64 StartMemUsed = CurMemUsed; - while (!TimeKeyL.Empty()&&(StartMemUsed-CurMemUsedGetVal(); - Del(Key); - } -} - -template -int64 TCache::GetMemUsed() const { - int64 MemUsed=0; - int KeyId=KeyDatH.FFirstKeyId(); - while (KeyDatH.FNextKeyId(KeyId)){ - const TKey& Key=KeyDatH.GetKey(KeyId); - const TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId]; - TDat Dat=KeyLNDatPr.Val2; - MemUsed+=int64(Key.GetMemUsed()+Dat->GetMemUsed()); - } - return MemUsed; -} - -template -bool TCache::RefreshMemUsed(){ - CurMemUsed=GetMemUsed(); - if (CurMemUsed>MxMemUsed){ - Purge(CurMemUsed-MxMemUsed); - return true; - } - return false; -} - -template -void TCache::Put(const TKey& Key, const TDat& Dat){ - int KeyId=KeyDatH.GetKeyId(Key); - if (KeyId==-1){ - int64 KeyDatMem=int64(Key.GetMemUsed()+Dat->GetMemUsed()); - if (CurMemUsed+KeyDatMem>MxMemUsed){Purge(KeyDatMem);} - CurMemUsed+=KeyDatMem; - TKeyLN KeyLN=TimeKeyL.AddFront(Key); - TKeyLNDatPr KeyLNDatPr(KeyLN, Dat); - KeyDatH.AddDat(Key, KeyLNDatPr); - } else { - TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId]; - TKeyLN KeyLN=KeyLNDatPr.Val1; - KeyLNDatPr.Val2=Dat; - TimeKeyL.PutFront(KeyLN); - } -} - -template -bool TCache::Get(const TKey& Key, TDat& Dat){ - int KeyId=KeyDatH.GetKeyId(Key); - if (KeyId==-1){ - return false; - } else { - Dat=KeyDatH[KeyId].Val2; - return true; - } -} - -template -void TCache::Del(const TKey& Key, const bool& DoEventCall){ - int KeyId=KeyDatH.GetKeyId(Key); - if (KeyId!=-1){ - TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId]; - TKeyLN KeyLN=KeyLNDatPr.Val1; - TDat& Dat=KeyLNDatPr.Val2; - if (DoEventCall){ - Dat->OnDelFromCache(Key, RefToBs);} - CurMemUsed-=int64(Key.GetMemUsed()+Dat->GetMemUsed()); - Dat=NULL; - TimeKeyL.Del(KeyLN); - KeyDatH.DelKeyId(KeyId); - } -} - -template -void TCache::Flush(){ - printf("To flush: %d\n", KeyDatH.Len()); - int KeyId=KeyDatH.FFirstKeyId(); int Done = 0; - while (KeyDatH.FNextKeyId(KeyId)){ - if (Done%10000==0){printf("%d\r", Done);} - const TKey& Key=KeyDatH.GetKey(KeyId); - TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId]; - TDat Dat=KeyLNDatPr.Val2; - Dat->OnDelFromCache(Key, RefToBs); - Done++; - } - printf("Done %d\n", KeyDatH.Len()); -} - -template -void TCache::FlushAndClr(){ - Flush(); - CurMemUsed=0; - KeyDatH.Clr(); - TimeKeyL.Clr(); -} - -template -void* TCache::FFirstKeyDat(){ - return TimeKeyL.First(); -} - -template -bool TCache::FNextKeyDat(void*& KeyDatP, TKey& Key, TDat& Dat){ - if (KeyDatP==NULL){ - return false; - } else { - Key=TKeyLN(KeyDatP)->GetVal(); Dat=KeyDatH.GetDat(Key).Val2; - KeyDatP=TKeyLN(KeyDatP)->Next(); return true; - } -} - -///////////////////////////////////////////////// -// Old-Hash-Functions - -// Old-String-Hash-Function -class TStrHashF_OldGLib { -public: - inline static int GetPrimHashCd(const char *p) { - const int MulBy = 16; // even older version used MulBy=2 - int HashCd = 0; - while (*p) { HashCd = (MulBy * HashCd) + *p++; HashCd &= 0x0FFFFFFF; } - return HashCd; } - inline static int GetSecHashCd(const char *p) { - const int MulBy = 16; // even older version used MulBy=2 - int HashCd = 0; - while (*p) { HashCd = (MulBy * HashCd) ^ *p++; HashCd &= 0x0FFFFFFF; } - return HashCd; } - inline static int GetPrimHashCd(const TStr& s) { return GetPrimHashCd(s.CStr()); } - inline static int GetSecHashCd(const TStr& s) { return GetSecHashCd(s.CStr()); } -}; - -// Md5-Hash-Function -class TStrHashF_Md5 { -public: - static int GetPrimHashCd(const char *p); - static int GetSecHashCd(const char *p); - static int GetPrimHashCd(const TStr& s); - static int GetSecHashCd(const TStr& s); -}; - -// DJB-Hash-Function -class TStrHashF_DJB { -private: - inline static unsigned int DJBHash(const char* Str, const ::TSize& Len) { - unsigned int hash = 5381; - for(unsigned int i = 0; i < Len; Str++, i++) { - hash = ((hash << 5) + hash) + (*Str); } - return hash; - } -public: - inline static int GetPrimHashCd(const char *p) { - const char *r = p; while (*r) { r++; } - return (int) DJBHash((const char *) p, r - p) & 0x7fffffff; } - inline static int GetSecHashCd(const char *p) { - const char *r = p; while (*r) { r++; } - return (int) DJBHash((const char *) p, r - p) & 0x7fffffff; } - inline static int GetPrimHashCd(const TStr& s) { - return GetPrimHashCd(s.CStr()); } - inline static int GetSecHashCd(const TStr& s) { - return GetSecHashCd(s.CStr()); } - inline static int GetPrimHashCd(const char *p, const ::TSize& Len) { - return (int) DJBHash((const char *) p, Len) & 0x7fffffff; } - inline static int GetSecHashCd(const char *p, const ::TSize& Len) { - return (int) DJBHash((const char *) p, Len) & 0x7fffffff; } -}; - -// Old-Vector-Hash-Function -template -class TVecHashF_OldGLib { -public: - static inline int GetPrimHashCd(const TVec& Vec) { - int HashCd=0; - for (int ValN=0; ValN -class THashMPKeyDat { -public: - TInt HashCd; - TKey Key; - TDat Dat; -public: - THashMPKeyDat(): - HashCd(-1), Key(), Dat(){} - THashMPKeyDat(const int& _HashCd, const TKey& _Key): - HashCd(_HashCd), Key(_Key), Dat(){} - explicit THashMPKeyDat(TSIn& SIn): - HashCd(SIn), Key(SIn), Dat(SIn){} - void Save(TSOut& SOut) const { - HashCd.Save(SOut); Key.Save(SOut); Dat.Save(SOut);} - - bool operator==(const THashMPKeyDat& HashKeyDat) const { - if (this==&HashKeyDat || (HashCd==HashKeyDat.HashCd - && Key==HashKeyDat.Key && Dat==HashKeyDat.Dat)){return true;} - return false;} - THashMPKeyDat& operator=(const THashMPKeyDat& HashKeyDat){ - if (this!=&HashKeyDat){ - HashCd=HashKeyDat.HashCd; Key=HashKeyDat.Key; - Dat=HashKeyDat.Dat;} - return *this;} -}; -#pragma pack(pop) - -///////////////////////////////////////////////// -// PHash-Table-Key-Data-Iterator -template -class THashMPKeyDatI{ -public: - typedef THashMPKeyDat TPHKeyDat; -private: - TPHKeyDat* KeyDatI; - TPHKeyDat* EndI; -public: - THashMPKeyDatI(): KeyDatI(NULL), EndI(NULL){} - THashMPKeyDatI(const THashMPKeyDatI& _HashKeyDatI): - KeyDatI(_HashKeyDatI.KeyDatI), EndI(_HashKeyDatI.EndI){} - THashMPKeyDatI(const TPHKeyDat* _KeyDatI, const TPHKeyDat* _EndI): - KeyDatI((TPHKeyDat*)_KeyDatI), EndI((TPHKeyDat*)_EndI){} - - THashMPKeyDatI& operator=(const THashMPKeyDatI& HashKeyDatI){ - KeyDatI=HashKeyDatI.KeyDatI; EndI=HashKeyDatI.EndI; return *this;} - bool operator==(const THashMPKeyDatI& HashKeyDatI) const { - return KeyDatI==HashKeyDatI.KeyDatI;} - bool operator<(const THashMPKeyDatI& HashKeyDatI) const { - return KeyDatIHashCd==-1) { KeyDatI++; } return *this; } - THashMPKeyDatI& operator--(int){ do { KeyDatI--; } while (KeyDatI->HashCd==-1); return *this;} - TPHKeyDat& operator*() const { return *KeyDatI; } - TPHKeyDat& operator()() const { return *KeyDatI; } - TPHKeyDat* operator->() const { return KeyDatI; } - THashMPKeyDatI& Next(){ operator++(1); return *this; } - - /// Tests whether the iterator has been initialized. - bool IsEmpty() const { return KeyDatI == NULL; } - /// Tests whether the iterator is pointing to the past-end element. - bool IsEnd() const { return EndI == KeyDatI; } - - const TKey& GetKey() const {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Key;} - const TDat& GetDat() const {/*Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1));*/ return KeyDatI->Dat;} - TDat& GetDat() {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Dat;} -}; - -//#////////////////////////////////////////////// -/// Hash-Table with multiprocessing support. -template > -class THashMP { -public: - enum {HashPrimes=32}; - static const unsigned int HashPrimeT[HashPrimes]; -public: - typedef THashMPKeyDatI TIter; -private: - typedef THashMPKeyDat TPHKeyDat; - typedef TPair TKeyDatP; - TVec Table; - TInt NumVals; -private: - class THashMPKeyDatCmp { - public: - const THashMP& Hash; - bool CmpKey, Asc; - THashMPKeyDatCmp(THashMP& _Hash, const bool& _CmpKey, const bool& _Asc) : - Hash(_Hash), CmpKey(_CmpKey), Asc(_Asc) { } - bool operator () (const int& KeyId1, const int& KeyId2) const { - if (CmpKey) { - if (Asc) { return Hash.GetKey(KeyId1) < Hash.GetKey(KeyId2); } - else { return Hash.GetKey(KeyId2) < Hash.GetKey(KeyId1); } } - else { - if (Asc) { return Hash[KeyId1] < Hash[KeyId2]; } - else { return Hash[KeyId2] < Hash[KeyId1]; } } } - }; -private: - TPHKeyDat& GetPHashKeyDat(const int& KeyId){ - TPHKeyDat& KeyDat=Table[KeyId]; - Assert(KeyDat.HashCd!=-1); return KeyDat;} - const TPHKeyDat& GetPHashKeyDat(const int& KeyId) const { - const TPHKeyDat& KeyDat=Table[KeyId]; - Assert(KeyDat.HashCd!=-1); return KeyDat;} - uint GetNextPrime(const uint& Val) const; -// void Resize(); -public: - THashMP(): - Table(), NumVals(0){} - THashMP(const THashMP& PHash): - Table(PHash.Table), NumVals(PHash.NumVals){} - explicit THashMP(const int& ExpectVals) { - Gen(ExpectVals);} - explicit THashMP(TSIn& SIn): - Table(SIn), NumVals(SIn){ - SIn.LoadCs();} - void Load(TSIn& SIn){ - Table.Load(SIn); NumVals.Load(SIn); - SIn.LoadCs();} - void Save(TSOut& SOut) const { - Table.Save(SOut); NumVals.Save(SOut); - SOut.SaveCs();} - - THashMP& operator=(const THashMP& Hash){ - if (this!=&Hash){ - Table=Hash.Table; NumVals=Hash.NumVals;} - return *this;} - bool operator==(const THashMP& Hash) const; //J: zdaj tak kot je treba -//bool operator < (const THash& Hash) const { Fail; return true; } - const TDat& operator[](const int& KeyId) const {return GetPHashKeyDat(KeyId).Dat;} - TDat& operator[](const int& KeyId){return GetPHashKeyDat(KeyId).Dat;} - TDat& operator()(const TKey& Key){return AddDat(Key);} - ::TSize GetMemUsed() const { - // return PortV.GetMemUsed()+KeyDatV.GetMemUsed()+sizeof(bool)+2*sizeof(int);} - int64 MemUsed = sizeof(int); - for (int TableN = 0; TableN < Table.Len(); TableN++) { - MemUsed += int64(sizeof(TInt)); - MemUsed += int64(Table[TableN].Key.GetMemUsed()); - MemUsed += int64(Table[TableN].Dat.GetMemUsed()); - } - return ::TSize(MemUsed); - } - - TIter BegI() const { - if (Len() == 0){return TIter(Table.EndI(), Table.EndI());} - return TIter(Table.BegI(), Table.EndI());} - TIter EndI() const {return TIter(Table.EndI(), Table.EndI());} - //TIter GetI(const int& KeyId) const {return TIter(&KeyDatV[KeyId], KeyDatV.EndI());} - TIter GetI(const TKey& Key) const {return TIter(&Table[GetKeyId(Key)], Table.EndI());} - - void Gen(const int& ExpectVals){ - Table.Gen(GetNextPrime(2 * ExpectVals));} - - void Clr(const bool& DoDel=true); - bool Empty() const {return Len()==0;} - int Len() const {return NumVals;} - void SetLen(const int& Length) {NumVals=Length;} - int GetMxKeyIds() const {return Table.Len();} -//bool IsKeyIdEqKeyN() const {return FreeKeys==0;} - int GetReservedKeyIds() const {return Table.Reserved();} - - // TODO: Non-unique keys - int AddKey(const TKey& Key); - int AddKey11(const int& Idx, const TKey& Key, bool& Found); - int AddKey12(const int& Idx, const TKey& Key, bool& Found); - int AddKey13(const int& Idx, const TKey& Key); - int AddKey1(const TKey& Key, bool& Found); - int AddKey2(const int& Idx, const TKey& Key, bool& Found); - TDat& AddDatId(const TKey& Key){ - int KeyId=AddKey(Key); return Table[KeyId].Dat=KeyId;} - // TODO: Make Dat updatable - TDat& AddDat(const TKey& Key){return Table[AddKey(Key)].Dat;} - TDat& AddDat(const TKey& Key, const TDat& Dat){ - return Table[AddKey(Key)].Dat=Dat;} - - const TKey& GetKey(const int& KeyId) const { return GetPHashKeyDat(KeyId).Key;} - int GetKeyId(const TKey& Key) const; - /// Get an index of a random element. If the hash table has many deleted keys, this may take a long time. - int GetRndKeyId(TRnd& Rnd) const; - /// Get an index of a random element. If the hash table has many deleted keys, defrag the hash table first (that's why the function is non-const). - int GetRndKeyId(TRnd& Rnd, const double& EmptyFrac); - bool IsKey(const TKey& Key) const {return GetKeyId(Key)!=-1;} - bool IsKey(const TKey& Key, int& KeyId) const { KeyId=GetKeyId(Key); return KeyId!=-1;} - bool IsKeyId(const int& KeyId) const { - return (0<=KeyId)&&(KeyId& KeyV) const; - void GetDatV(TVec& DatV) const; - void GetKeyDatPrV(TVec >& KeyDatPrV) const; - void GetDatKeyPrV(TVec >& DatKeyPrV) const; - void GetKeyDatKdV(TVec >& KeyDatKdV) const; - void GetDatKeyKdV(TVec >& DatKeyKdV) const; - - void Swap(THashMP& Hash); -//void Defrag(); - void Pack(){Table.Pack();} -/*void Sort(const bool& CmpKey, const bool& Asc); - void SortByKey(const bool& Asc=true) { Sort(true, Asc); } - void SortByDat(const bool& Asc=true) { Sort(false, Asc); }*/ -}; - -template -const unsigned int THashMP::HashPrimeT[HashPrimes]={ - 3ul, 5ul, 11ul, 23ul, - 53ul, 97ul, 193ul, 389ul, 769ul, - 1543ul, 3079ul, 6151ul, 12289ul, 24593ul, - 49157ul, 98317ul, 196613ul, 393241ul, 786433ul, - 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, - 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, - 1610612741ul, 3221225473ul, 4294967291ul -}; - -template -uint THashMP::GetNextPrime(const uint& Val) const { - const uint* f=(const uint*)HashPrimeT, *m, *l=(const uint*)HashPrimeT + (int)HashPrimes; - int h, len = (int)HashPrimes; - while (len > 0) { - h = len >> 1; m = f + h; - if (*m < Val) { f = m; f++; len = len - h - 1; } - else len = h; - } - return f == l ? *(l - 1) : *f; -} - -template -bool THashMP::operator==(const THashMP& Hash) const { - if (Len() != Hash.Len()) { return false; } - for (int i = FFirstKeyId(); FNextKeyId(i); ) { - const TKey& Key = GetKey(i); - if (! Hash.IsKey(Key)) { return false; } - if (GetDat(Key) != Hash.GetDat(Key)) { return false; } - } - return true; -} - -template -int THashMP::AddKey(const TKey& Key) { - //int CurVals = __sync_fetch_and_add(&NumVals.Val, 1); - //IAssertR(CurVals < Table.Len(), "Table must not be full"); - - const int BegTableN=abs(Key.GetPrimHashCd()%Table.Len()); - const int HashCd=abs(Key.GetSecHashCd()); - - int TableN = BegTableN; - while (Table[TableN].HashCd != -1 || - (!__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, HashCd))) { - TableN = (TableN + 1) % Table.Len(); - } - Table[TableN].Key = Key; - __sync_fetch_and_add(&NumVals.Val, 1); - return TableN; -} - -template -int THashMP::AddKey11(const int& BegTableN, const TKey& Key, bool& Found) { - //int CurVals = __sync_fetch_and_add(&NumVals.Val, 1); - //IAssertR(CurVals < Table.Len(), "Table must not be full"); - - const int Length = Table.Len(); - const int HashCd=abs(Key.GetSecHashCd()); - - int TableN = BegTableN; - Found = false; - do { - if (Table[TableN].HashCd.Val != -1) { - if (Table[TableN].Key == Key) { - Found = true; - return TableN; - } - } else if (__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, HashCd)) { - break; - } - - TableN++; - if (TableN >= Length) { - TableN = 0; - } - } while (1); - - Table[TableN].Key = Key; - return TableN; -} - -template -int THashMP::AddKey12(const int& BegTableN, const TKey& Key, bool& Found) { - //int CurVals = __sync_fetch_and_add(&NumVals.Val, 1); - //IAssertR(CurVals < Table.Len(), "Table must not be full"); - - const int Length = Table.Len(); - //const int HashCd=abs(Key.GetSecHashCd()); - - // HashCd values: - // -1: empty slot - // 1: occupied slot, invalid key - // 2: occupied slot, valid key - - int TableN = BegTableN; - do { - int HashCd = Table[TableN].HashCd.Val; - if (HashCd == -1) { - // an empty slot - if (__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, 1)) { - // an empty slot has been claimed, key is invalid - break; - } - } else { - // slot is occupied - if (HashCd != 2) { - // key is not yet valid, wait for a valid key - while (!__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, 2, 2)) { - usleep(20); - } - } - if (Table[TableN].Key == Key) { - // the key matches - Found = true; - return TableN; - } - // move to the next slot - TableN++; - if (TableN >= Length) { - TableN = 0; - } - } - } while (1); - - // write the key, indicate a valid key - Table[TableN].Key = Key; - Table[TableN].HashCd.Val = 2; - - Found = false; - return TableN; -} - -template -int THashMP::AddKey13(const int& BegTableN, const TKey& Key) { - //int CurVals = __sync_fetch_and_add(&NumVals.Val, 1); - //IAssertR(CurVals < Table.Len(), "Table must not be full"); - - const int Length = Table.Len(); - //const int HashCd=abs(Key.GetSecHashCd()); - - // HashCd values: - // -1: empty slot - // 1: occupied slot, invalid key - // 2: occupied slot, valid key - - int TableN = BegTableN; - do { - int HashCd = Table[TableN].HashCd.Val; - if (HashCd == -1) { - // an empty slot - if (__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, 1)) { - // an empty slot has been claimed, key is invalid - break; - } - } else { - // slot is occupied, move to the next slot - TableN++; - if (TableN >= Length) { - TableN = 0; - } - } - } while (1); - - // write the key, indicate a valid key - Table[TableN].Key = Key; - Table[TableN].HashCd.Val = 2; - - return TableN; -} - -template -int THashMP::AddKey1(const TKey& Key, bool& Found) { - const int Length = Table.Len(); - //const int Length = 12582917; - const int BegTableN = abs(Key.GetPrimHashCd() % Length); - const int HashCd = abs(Key.GetSecHashCd()); - - //printf("AddKey1 %5d %5d\n", Length, BegTableN); - - int TableN = BegTableN; - while (Table[TableN].HashCd.Val != -1) { - if (Table[TableN].Key == Key) { - Found = true; - return TableN; - } - TableN++; - if (TableN >= Length) { - TableN = 0; - } - //(!__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, HashCd))) { - //TableN = (TableN + 1) % Table.Len(); - } - - NumVals.Val++; - Found = false; - Table[TableN].HashCd.Val = HashCd; - Table[TableN].Key = Key; - return TableN; - - // TODO:RS, need to set the length at the end -} - -template -int THashMP::AddKey2(const int& BegTableN, const TKey& Key, bool& Found) { - //const int Length = 12582917; - const int Length = Table.Len(); - const int HashCd = abs(Key.GetSecHashCd()); - - //printf("AddKey2 %5d %5d\n", Length, BegTableN); - - int TableN = BegTableN; - while (Table[TableN].HashCd.Val != -1) { - if (Table[TableN].Key == Key) { - Found = true; - return TableN; - } - TableN++; - if (TableN >= Length) { - TableN = 0; - } - //(!__sync_bool_compare_and_swap(&Table[TableN].HashCd.Val, -1, HashCd))) { - //TableN = (TableN + 1) % Table.Len(); - } - - //NumVals.Val++; - Found = false; - Table[TableN].HashCd.Val = HashCd; - Table[TableN].Key = Key; - return TableN; - - // TODO:RS, need to set the length at the end -} - -template -int THashMP::GetKeyId(const TKey& Key) const { - const int BegTableN=abs(Key.GetPrimHashCd()%Table.Len()); - //const int HashCd=abs(Key.GetSecHashCd()); - - int TableN = BegTableN; - while (Table[TableN].HashCd != -1) { - if (Table[TableN].Key == Key) { return TableN; } - TableN = (TableN + 1) % Table.Len(); - if (TableN == BegTableN) { return -1; } - } - - return -1; -} - -template -void THashMP::Clr(const bool& DoDel){ - if (DoDel){ - Table.Clr(); - } else { - Table.PutAll(TPHKeyDat()); - } - NumVals = TInt(0); -} - -template -bool THashMP::FNextKeyId(int& KeyId) const { - do {KeyId++;} while ((KeyId -void THashMP::GetKeyV(TVec& KeyV) const { - KeyV.Gen(Len(), 0); - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - KeyV.Add(GetKey(KeyId));} -} - -template -void THashMP::GetDatV(TVec& DatV) const { - DatV.Gen(Len(), 0); - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - DatV.Add(GetPHashKeyDat(KeyId).Dat);} -} - -template -void THashMP::GetKeyDatPrV(TVec >& KeyDatPrV) const { - KeyDatPrV.Gen(Len(), 0); - TKey Key; TDat Dat; - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - KeyDatPrV.Add(TPair(Key, Dat)); - } -} - -template -void THashMP::GetDatKeyPrV(TVec >& DatKeyPrV) const { - DatKeyPrV.Gen(Len(), 0); - TKey Key; TDat Dat; - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - DatKeyPrV.Add(TPair(Dat, Key)); - } -} - -template -void THashMP::GetKeyDatKdV(TVec >& KeyDatKdV) const { - KeyDatKdV.Gen(Len(), 0); - TKey Key; TDat Dat; - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - KeyDatKdV.Add(TKeyDat(Key, Dat)); - } -} - -template -void THashMP::GetDatKeyKdV(TVec >& DatKeyKdV) const { - DatKeyKdV.Gen(Len(), 0); - TKey Key; TDat Dat; - int KeyId=FFirstKeyId(); - while (FNextKeyId(KeyId)){ - GetKeyDat(KeyId, Key, Dat); - DatKeyKdV.Add(TKeyDat(Dat, Key)); - } -} - -template -void THashMP::Swap(THashMP& Hash) { - if (this!=&Hash){ - Table.Swap(Hash.Table); - ::Swap(NumVals, Hash.NumVals); - } -} - -template -int THashMP::GetRndKeyId(TRnd& Rnd) const { - printf("*** ERROR *** THashMP::GetRndKeyId called\n"); - return 0; -} - -template -int THashMP::GetRndKeyId(TRnd& Rnd, const double& EmptyFrac) { - printf("*** ERROR *** THashMP::GetRndKeyId called\n"); - return 0; -} - -#endif // GCC_ATOMIC - -#endif // hashmp_h diff --git a/inst/include/glib-core/html.cpp b/inst/include/glib-core/html.cpp deleted file mode 100644 index 6d84f11..0000000 --- a/inst/include/glib-core/html.cpp +++ /dev/null @@ -1,1325 +0,0 @@ -///////////////////////////////////////////////// -// Html-Lexical-Chars -void THtmlLxChDef::SetUcCh(const char& UcCh, const char& LcCh){ - // update upper-case (more lower cases may have one upper case) - IAssert( - (UcChV[LcCh-TCh::Mn]==TCh(0))|| - (UcChV[LcCh-TCh::Mn]==TCh(LcCh))); - UcChV[LcCh-TCh::Mn]=TCh(UcCh); - // update lower-case (one upper case may have only one lower case) - if ((LcChV[UcCh-TCh::Mn]==TCh(0))||(LcChV[UcCh-TCh::Mn]==TCh(UcCh))){ - LcChV[UcCh-TCh::Mn]=TCh(LcCh); - } -} - -void THtmlLxChDef::SetUcCh(const TStr& Str){ - // set type of characters as letters - SetChTy(hlctAlpha, Str); - // first char in string is upper-case, rest are lower-case - for (int ChN=1; ChN=2)&&(Str[0]=='&')&&(Str[1]=='#')){ - int ChCd=0; - for (int ChN=2; ChN/?"); - SetChTy(hlctLTag, "<"); SetChTy(hlctRTag, ">"); - SetChTy(hlctEof, TStr(TCh::EofCh)); - for (int Ch=TCh::Mn; Ch<=TCh::Mx; Ch++){ - if ((Ch<0)||(127"); - SetEscStr(" ", " "); - - SetEscStr("ä", "\xe4"); SetEscStr("Ä", "\xc4"); - SetEscStr("ö", "\xf6"); SetEscStr("Ö", "\xd6"); - SetEscStr("ü", "\xfc"); SetEscStr("Ü", "\xdc"); - SetEscStr("å", "\xe5"); SetEscStr("Å", "\xc5"); - SetEscStr("ø", "\xf8"); SetEscStr("Ø", "\xd8"); - SetEscStr("&Aelig", "\xc6"); SetEscStr("æ", "\xe6"); - - SetEscStr("é", "e"); SetEscStr("É", "E"); - SetEscStr("è", "e"); SetEscStr("È", "E"); - SetEscStr("à", "a"); SetEscStr("À", "A"); -} - -PHtmlLxChDef THtmlLxChDef::ChDef=PHtmlLxChDef(new THtmlLxChDef()); - -TStr THtmlLxChDef::GetCSZFromYuascii(const TChA& ChA){ - TChA DstChA; - for (int ChN=0; ChN'); - ChA=UcChA; - - if (DoParseArg){ - while ((Ch!='>')&&(Ch!=TCh::EofCh)){ - while ((!ChDef.IsAlpha(Ch))&&(Ch!='>')&&(Ch!=TCh::EofCh)){GetCh();} - if (ChDef.IsAlpha(Ch)){ - ArgNm.Clr(); ArgVal.Clr(); - while (ChDef.IsAlNum(Ch)||(Ch=='-')){ArgNm.AddCh(ChDef.GetUc(Ch)); GetCh();} - while (ChDef.IsWs(Ch)){GetCh();} - if (Ch=='='){ - GetCh(); while (ChDef.IsWs(Ch)){GetCh();} - if (Ch=='"'){ - GetCh(); - while ((Ch!=TCh::EofCh)&&(Ch!='"')&&(Ch!='>')){ - if (!ChDef.IsEoln(Ch)){ArgVal.AddCh(Ch);} GetCh();} - if (Ch=='"'){GetCh();} - } else if (Ch=='\''){ - GetCh(); - while ((Ch!=TCh::EofCh)&&(Ch!='\'')&&(Ch!='>')){ - if (!ChDef.IsEoln(Ch)){ArgVal.AddCh(Ch);} GetCh();} - if (Ch=='\''){GetCh();} - } else { - while ((!ChDef.IsWs(Ch))&&(Ch!='>')&&(Ch!=TCh::EofCh)){ - ArgVal.AddCh(Ch); GetCh();} - } - ArgNmValV.Add(TStrKd(ArgNm, ArgVal)); - } - } - } - } else { - while ((Ch!='>')&&(Ch!=TCh::EofCh)){GetCh();} - } - if (Ch!=TCh::EofCh){GetEscCh();} -} - -THtmlLxSym THtmlLx::GetSym(){ - // prepare symbol descriptions - ChA.Clr(); UcChA.Clr(); - PreSpaces=0; PreSpaceChA.Clr(); - ArgNmValV.Clr(); - // skip white-space - while (ChDef.IsSpace(Ch)){ - if (ChX>0){PreSpaceChA+=Ch; PreSpaces++;} GetEscCh();} - // parse symbol - SymChA.Clr(); SymChA+=Ch; SymBChX=ChX; - switch (ChDef.GetChTy(Ch)){ - case hlctAlpha: - Sym=hsyStr; - forever{ - do { - ChA.AddCh(Ch); UcChA.AddCh(ChDef.GetUc(Ch)); GetEscCh(); - } while (ChDef.IsAlNum(Ch)); - if (Ch=='.'){ - GetCh(); - if (ChDef.IsAlNum(Ch)){ChA.AddCh('.'); UcChA.AddCh('.');} - else {PutCh(Ch); Ch='.'; break;} - } else {break;} - } - break; - case hlctNum: - Sym=hsyNum; - forever{ - do { - ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh(); - } while (ChDef.IsNum(Ch)); - if (Ch=='.'){ - GetCh(); - if (ChDef.IsAlNum(Ch)){ChA.AddCh('.'); UcChA.AddCh('.');} - else {PutCh(Ch); Ch='.'; break;} - } else if (ChDef.IsAlpha(Ch)){ - Sym=hsyStr; - } else { - break; - } - } - break; - case hlctSym: - Sym=hsySSym; ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh(); - if ((ChA.LastCh()=='.')&&(ChDef.IsAlNum(Ch))){ - Sym=hsyStr; - do { - ChA.AddCh(Ch); UcChA.AddCh(ChDef.GetUc(Ch)); GetEscCh(); - } while (ChDef.IsAlNum(Ch)); - } - break; - case hlctLTag: - if (EscCh){ - Sym=hsySSym; ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh(); - } else { - GetCh(); - if (Ch=='!'){GetCh(); GetMetaTag();} else {GetTag();} - } - break; - case hlctRTag: - if (EscCh){ - Sym=hsySSym; ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh(); - } else { - Sym=hsySSym; ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh(); - } - break; - case hlctEof: Sym=hsyEof; break; - default: Sym=hsyUndef; GetEscCh(); - } - // set symbol last-character-position - SymEChX=ChX-1; - // delete last character - if (!SymChA.Empty()){SymChA.Pop();} - // return symbol - return Sym; -} - -PHtmlTok THtmlLx::GetTok(const bool& DoUc){ - if (DoUc){return PHtmlTok(new THtmlTok(Sym, UcChA, ArgNmValV));} - else {return PHtmlTok(new THtmlTok(Sym, ChA, ArgNmValV));} -} - -TStr THtmlLx::GetFullBTagStr() const { - IAssert(Sym==hsyBTag); - TChA BTagChA; - BTagChA+=ChA; BTagChA.Pop(); - for (int ArgN=0; ArgN0){OutChA+=' ';} - if ((Sym!=hsyBTag)&&(Sym!=hsyETag)){ - OutChA+=ChA;} - } - } - return OutChA; -} - -TStr THtmlLx::GetStrToBTag(const TStr& TagNm, const bool& TxtOnlyP){ - TChA OutChA; - forever { - GetSym(); - if ((Sym==hsyEof)||((Sym==hsyBTag)&&(UcChA==TagNm))){ - break; - } else { - if (PreSpaces>0){OutChA+=' ';} - if ((TxtOnlyP&&(Sym!=hsyBTag)&&(Sym!=hsyETag))||(!TxtOnlyP)){ - OutChA+=ChA;} - } - } - return OutChA; -} - -TStr THtmlLx::GetStrToBTag(const TStr& TagNm, const TStr& ArgNm, - const TStr& ArgVal, const bool& TxtOnlyP){ - TChA OutChA; - forever { - GetSym(); - if ((Sym==hsyEof)||((Sym==hsyBTag)&&(UcChA==TagNm)&& - (IsArg(ArgNm))&&(GetArg(ArgNm)==ArgVal))){ - break; - } else { - if (PreSpaces>0){OutChA+=' ';} - if ((TxtOnlyP&&(Sym!=hsyBTag)&&(Sym!=hsyETag))||(!TxtOnlyP)){ - OutChA+=ChA;} - } - } - return OutChA; -} - -TStr THtmlLx::GetStrToETag(const TStr& TagNm, const bool& TxtOnlyP){ - TChA OutChA; - forever { - GetSym(); - if ((Sym==hsyEof)||((Sym==hsyETag)&&(UcChA==TagNm))){ - break; - } else { - if (PreSpaces>0){OutChA+=' ';} - if ((TxtOnlyP&&(Sym!=hsyBTag)&&(Sym!=hsyETag))||(!TxtOnlyP)){ - OutChA+=ChA;} - } - } - return OutChA; -} - -TStr THtmlLx::GetStrToETag2(const TStr& TagNm1, - const TStr& TagNm2, const bool& TxtOnlyP){ - TChA OutChA; - forever { - GetSym(); - if ((Sym==hsyEof)||((Sym==hsyETag)&&(UcChA==TagNm1))||((Sym==hsyETag)&&(UcChA==TagNm2))){ - break; - } else { - if (PreSpaces>0){OutChA+=' ';} - if ((TxtOnlyP&&(Sym!=hsyBTag)&&(Sym!=hsyETag))||(!TxtOnlyP)){ - OutChA+=ChA;} - } - } - return OutChA; -} - -TStr THtmlLx::GetStrInTag(const TStr& TagNm, const bool& TxtOnlyP){ - MoveToBTagOrEof(TagNm); - return GetStrToETag(TagNm, TxtOnlyP); -} - -TStr THtmlLx::GetHRefBeforeStr(const TStr& Str){ - TStr HRefStr; - forever { - GetSym(); - if (Sym==hsyEof){HRefStr=""; break;} - if ((Sym==hsyBTag)&&(UcChA=="")){HRefStr=GetArg("HREF");} - if ((Sym==hsyStr)&&(ChA==Str)){break;} - } - return HRefStr; -} - -bool THtmlLx::IsGetBTag(const TStr& TagNm){ - if (GetSym()==hsyBTag){ - return ChA==TagNm; - } else {return false;} -} - -bool THtmlLx::IsGetETag(const TStr& TagNm){ - if (GetSym()==hsyETag){ - return ChA==TagNm; - } else {return false;} -} - -TStr THtmlLx::GetSymStr(const THtmlLxSym& Sym){ - switch (Sym){ - case hsyUndef: return "Undef"; - case hsyStr: return "Str"; - case hsyNum: return "Num"; - case hsySSym: return "SSym"; - case hsyUrl: return "Url"; - case hsyBTag: return "BTag"; - case hsyETag: return "ETag"; - case hsyMTag: return "MTag"; - case hsyEof: return "Eof"; - default: Fail; return TStr(); - } -} - -TStr THtmlLx::GetEscapedStr(const TChA& ChA){ - TChA EscapedChA; - for (int ChN=0; ChN': EscapedChA+=">"; break; - default: EscapedChA+=Ch; - } - } - return EscapedChA; -} - -TStr THtmlLx::GetAsciiStr(const TChA& ChA, const char& GenericCh){ - TChA AsciiChA; - for (int ChN=0; ChN 0) { ChA += ' '; } - ChA += Lx.ChA; - default: break; - } - Lx.GetSym(); - } - return ChA; -} - -///////////////////////////////////////////////// -// Html-Token -TStr THtmlTok::GetFullStr() const { - if ((Sym==hsyBTag)&&(ArgNmValV.Len()>0)){ - TChA FullChA; - FullChA+=Str.GetSubStr(0, Str.Len()-2); - for (int ArgNmValN=0; ArgNmValNPutStr(GetFullStr()); SOut->PutStr(" "); - } else { - SOut->PutStr(THtmlLx::GetSymStr(Sym)); SOut->PutStr(" "); - SOut->PutStr(GetFullStr()); SOut->PutStr(" "); - } -} - -const TStr THtmlTok::ATagNm=""; -const TStr THtmlTok::AreaTagNm=""; -const TStr THtmlTok::BrTagNm="
"; -const TStr THtmlTok::CardTagNm=""; -const TStr THtmlTok::CenterTagNm="
"; -const TStr THtmlTok::FrameTagNm=""; -const TStr THtmlTok::H1TagNm="

"; -const TStr THtmlTok::H2TagNm="

"; -const TStr THtmlTok::H3TagNm="

"; -const TStr THtmlTok::H4TagNm="

"; -const TStr THtmlTok::H5TagNm="

"; -const TStr THtmlTok::H6TagNm="
"; -const TStr THtmlTok::ImgTagNm=""; -const TStr THtmlTok::LiTagNm="
  • "; -const TStr THtmlTok::MetaTagNm=""; -const TStr THtmlTok::PTagNm="

    "; -const TStr THtmlTok::UlTagNm="