noremap <F6> <ESC>:vimgrep /\C\(TODO\\|FIXME\\|XXX\\|BUG\)/ ./**/*.c<CR>:copen<CR>:cc<CR><CR>
пятница, 13 июня 2014 г.
TODO, FIXME, etc in Vim
Add this to .vimrc:
пятница, 16 мая 2014 г.
Small and fast build tools: SVMK, NINJA
There are many build tools. One of them tracks dependencies and build result output via special file with dependencies-tree description, another generates such file from universal format (Premake, Bakefile, CMake, etc.). There are 2 big classes of such tools: with special syntax in its' Makefiles, and with supporting of some general-purpose language (Waf, Scoons, etc.). Difference is that in the 2nd kind you can solve absolutely any tasks without writing special plugins (sometimes is impossible to extend "Makefile" in natural way, only with command line utilities)...
If you want very small and fast build tools (and not Make:) you can see next: Ninja and Tcl-based Svmk. Another Tcl-based make tools are here.
To run:
If you want very small and fast build tools (and not Make:) you can see next: Ninja and Tcl-based Svmk. Another Tcl-based make tools are here.
Ninja
Ninja is very simple and fast. It have not functions, if-else-for-while operators, nothing, only variables, rules... But it can be used to process automatically generated "Makefile". Example:DMD = c:/dmd2/windows/bin/dmd.exe
LINK = c:/dmd2/windows/bin/link.exe
INC = -Ic:/dmd2/src/phobos -Ic:/dmd2/src/druntime/import
rule mkprj
command = $DMD -I$PKGDIR $INC -of$out $in
description = Compile D program $out
PKGDIR = misc/scripts
build $PKGDIR/email.exe: mkprj $PKGDIR/characterencodings.d $PKGDIR/email.d
Save it in build.ninja, call ninja misc/scripts/email.exe and you will get result: email.exe in misc/scripts directory.
rule creates rule which has attribute command to call when it's applied. build set dependency: email.exe will be built from 2 .d files with mkprj rule by calling it command. Nothing else. Simple, fast. ninja is only ONE FILE: ninja.exe and is about 800Kb! If no target (misc/scripts/email.exe), then ninja will build it.
Svmk
It's Tcl-based make alternatives. So you have all power of Tcl (use Tclkit Shell!) in one little Tcl-script: svmk.tcl. Example of the same:# vi:set ft=tcl:
set DHOME c:/dmd2/windows/bin
set DMD $DHOME/dmd.exe
proc dcomp {out in {dflags ""}} {
global DMD
if [llength $dflags] {
system $DMD $dflags -of$out {*}$in
} else {
system $DMD -of$out {*}$in
}
}
target email.exe {
cd misc/scripts
set SRC "characterencodings.d email.d"
depend $SRC {
dcomp email.exe $SRC
}
}
We create dcomp procedure for compiling of D applications. Then create rule "email.exe" which is PHONY :) Common usage is target TARGET { depend DEPENDENCIES { commands... } }. You can see: we can use any Tcl commands, even download something via Net :) All what you need is: tclkitsh.exe: 740Kb, svmk.tcl: 10Kb!To run:
tclkitsh.exe svmk.tcl email.exeAnd sure you can (and have) to use original D build tool (if we are talking about D;) - dub.
пятница, 25 апреля 2014 г.
Installation and trying D
First, download ZIP-archive with D compiler. Then unpack it into... for example c:/dmd2. You need directories:
html and man (documentation), samples, src (for including), windows (binary, libraries) and README.TXT. Create somewhere test file, for example a.d:
import std.stdio;
int main(string[] args) {
int[string] d;
d["qwe"] = 12;
d["asd"] = 13;
d["zxc"] = 14;
writef("%d\n", d["asd"]);
int i = 10;
typeof(i) j = 90;
writef("%d", j);
return 0;
}
Then create dmd.conf file (or sc.ini) with next content:
[Environment] LIB="c:\dmd2\windows\lib" DFLAGS="-Ic:\dmd2\src\phobos" "-Ic:\dmd2\src\druntime\import" LINKCMD="c:\dmd2\windows\bin\link" DDOCFILE=mysettings.ddocNow create Makefile:
DBIN=c:/dmd2/windows/bin LINK=$(DBIN)/link DMD=$(DBIN)/dmd RM=rm -rf DFLAGS= LFLAGS= SRC=a.d OBJS=$(SRC:.d=.o) EXES=$(SRC:.d=.exe) .SUFFIXES: .d .d.o: $(DMD) $(DFLAGS) -c $< -of$@ clean: $(RM) $(OBJS) $(EXES) ###################################################################### a: a.o $(LINK) $(LFLAGS) $<Now you can compile first D program:
make aand run it:
a.exe 13 90
воскресенье, 20 апреля 2014 г.
Multiplication of big numbers represented as strings
This is the usual task in interview: solve multiplication of integer numbers presented as strings, for ex., "25" * "25" = "625". Length of these strings may be any (not 16 bits or 32 bits only).
Implementation will use school way of multiplication of numbers (sign is ignored), see:
Implementation will use school way of multiplication of numbers (sign is ignored), see:
1856 1856 ---- 11136 9280 14848 1856 ======= 3444736So, "1856" * "1856" = "3444736", we multiply each digit of second number on each digit of first one, shift result number by one to left, summarize them and don't forget about carry. Carry is of 2 kinds: carry of multiplication and carry of sum. Algorithm is:
- multiply last digit of 2nd number by last digit of 1st one
- write result (only low digit) in result string at LAST POSITION
- if resulting number is > 10, save carry (divided by 10)
- make the same with the 2nd digit of 2nd number, but instead of saving resulting digit, make summation: with carry of multiplication AND carry of sum
- don't forget to make shifting to left when go to next digit of 1nd number
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "algutils.h"
#define ASCII2I(CH) ((CH)?((CH)-'0'):0)
#define I2ASCII(I) ((I)+'0')
char* mul(const char *s1, const char *s2, char *res, int reslen)
{
int r, // index in result string
i, // index in s1
_i, // offset (to left) of next middle number
j, // index in s2
len1 = strlen(s1), len2 = strlen(s2);
int d1, // digit in s1
d2, // digit in s2
ts, // temporary summ
c, // curry in multiplication
cs; // curry in summation
memset(res, 0, reslen);
for (i=len2-1, _i=0, cs=0; i>=0; i--, _i++) {
d2 = ASCII2I(s2[i]);
r = reslen - 2 - _i;
for (j=len1-1, c=0; j>=0; j--) {
d1 = ASCII2I(s1[j]);
d1 = d1*d2 + c;
ts = ASCII2I(res[r]) + (d1%10) + cs;
res[r] = I2ASCII(ts%10);
cs = ts/10;
c = d1/10;
r--;
}
do {
// do rest of sum (high digits)
ts = ASCII2I(res[r]) + (c%10) + cs;
if (!ts) break;
res[r] = I2ASCII(ts%10);
cs = ts/10;
r--;
} while (cs);
}
return (res + r + 1);
}
///////////////////////////////////////////////
// Main: tests
///////////////////////////////////////////////
#define RND(N) (srand(time(NULL)), rand()%(N))
int main(void)
{
char _res[32], *res;
char s1[32] = {0};
char s2[32] = {0};
int n1, n2, rn, resn, chk;
//printf("%s\n", mul("1856", "1856", _res, sizeofarray(_res)));
//return 0;
for (int i=0; i<100; i++) {
sprintf(s1, "%d", RND(9999999));
sprintf(s2, "%d", 1+RND(9999999));
printf("STR: %s * %s = %s\n", s1, s2, (res=mul(s1, s2, _res, sizeofarray(_res))));
n1 = atoi(s1); n2 = atoi(s2); rn = n1*n2; resn = atoi(res);
printf("CTR: %d * %d = %d\n", n1, n2, rn);
printf("verify <%d>: %d (%d ? %d)\n", i, (chk=(resn==rn)), rn, resn);
if (!chk) { printf("FAILED!\n"); return (1); }
else printf("\n\n");
}
printf("All tests are passed\n");
return (0);
}
Tests are cycle of multiplication of random numbers. If test is failed, cycle is break.
Подписаться на:
Сообщения (Atom)