Name: Anonymous 2014-07-22 13:29
Can be yours or not, I just want to see something good.
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
void main(void){
int i;
while(i<10){
int i2;
printf_s("\rstarting");
while(i2<10){
putchar('.');
i2 = i2 + 1;
}
i = i + 1;
sleep(1);
}
printf_s("press any key...");
getch();
}
conio.htop lel
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
typedef struct {
unsigned char *p, *q;
size_t n, tmp, bits;
} bitptr;
void bitinit(bitptr *, void *, size_t, size_t);
void bitnext(bitptr *);
void bitprev(bitptr *);
void bitset(bitptr *, bool);
bool bitval(bitptr *);
void bitrewind(bitptr *);
size_t bitsize(bitptr *);
void bitpack(bitptr *, bitptr *, size_t, size_t);
void bitunpack(bitptr *, bitptr *, size_t, size_t);
void bitinit(bitptr *p, void *q, size_t n, size_t bits) {
p->p = p->q = q;
p->tmp = n;
p->bits = bits;
p->n = 0;
while(n--) bitnext(p);
}
void bitnext(bitptr *p) {
if(++p->n == CHAR_BIT) {
p->n = 0;
p->q++;
}
}
void bitprev(bitptr *p) {
if(p->n-- == 0) {
p->n = CHAR_BIT - 1;
p->q--;
}
}
void bitset(bitptr *p, bool b) {
if(b)
*p->q |= (b << p->n);
else
*p->q &= ~(b << p->n);
}
bool bitval(bitptr *p) {
return *p->q & (1 << p->n);
}
void bitrewind(bitptr *p) {
size_t n;
p->q = p->p;
p->n = p->tmp;
for(n = p->tmp; n--; bitnext(p));
}
size_t bitsize(bitptr *p) {
return p->bits;
}
#define bitsize(p) ((p)->bits)
void bitpack(bitptr *p, bitptr *q, size_t size, size_t nmemb) {
size_t n;
while(nmemb--) {
n = size;
while(n--) {
bitset(p, bitval(q));
bitnext(p);
bitnext(q);
}
if((n = bitsize(q) - size) != 0)
while(n--) bitnext(q);
}
}
void bitunpack(bitptr *p, bitptr *q, size_t size, size_t nmemb) {
size_t n;
while(nmemb--) {
n = size;
while(n--) {
bitset(p, bitval(q));
bitnext(p);
bitnext(q);
}
if((n = bitsize(p) - size) != 0)
while(n--) bitnext(p);
}
}
#define objsize(obj) (sizeof obj * CHAR_BIT)
static const char map[] = "abcdefghijklmnopqrstuvwxyz";
#define mapletter(x) (strchr(map, x) - map)
int main(void) {
bitptr p, s;
char buf[3] = {0}, x[] = { mapletter('h'), mapletter('e'), mapletter('l'), mapletter('l'), mapletter('o'), 0 };
size_t n;
bitinit(&p, buf, 0, objsize(buf[0]));
bitinit(&s, x, 0, objsize(x[0]));
bitpack(&p, &s, 4, sizeof x - 1);
bitrewind(&p);
bitrewind(&s);
printf("Message 'hello' stored in 3 bytes array. contents of s.p:\n");
memset(x, 0, sizeof x);
bitunpack(&s, &p, 4, sizeof x - 1);
for(n = 0; n < sizeof x - 1; n++)
s.p[n] = map[s.p[n]];
puts((void *)s.p);
return 0;
}
sizeof(unsigned) * CHAR_BIT
bits at a time wherever possible. The cfbcopyarea routines in the Linux kernel do this but the implementation there is rather wizardly. bitptr *p, *q;
/* p gets initialized to something */
q = p; // now q points to the same bitptr, incrementing q will increment p
*q = *p; // q is a copy of p, incrementing q does not affect p
/* With gcc3.1, must omit -ansi to compile eol comments */
#include <stdio.h>
#include <stdlib.h>
static int ch, lastch;
/* ---------------- */
static void putlast(void)
{
if (0 != lastch) fputc(lastch, stdout);
lastch = ch;
ch = 0;
} /* putlast */
/* ---------------- */
/* gobble chars until star slash appears */
static int stdcomment(void)
{
int ch, lastch;
ch = 0;
do {
lastch = ch;
if (EOF == (ch = fgetc(stdin))) return EOF;
} while (!(('*' == lastch) && ('/' == ch)));
return ch;
} /* stdcomment */
/* ---------------- */
/* gobble chars until EOLine or EOF. i.e. // comments */
static int eolcomment(void)
{
int ch, lastch;
ch = '\0';
do {
lastch = ch;
if (EOF == (ch = fgetc(stdin))) return EOF;
} while (!(('\n' == ch) && ('\\' != lastch)));
return ch;
} /* eolcomment */
/* ---------------- */
/* echo chars until '"' or EOF */
static int echostring(void)
{
putlast();
if (EOF == (ch = fgetc(stdin))) return EOF;
do {
putlast();
if (EOF == (ch = fgetc(stdin))) return EOF;
if (('\\' == ch) && ('\\' == lastch)) {
putlast();
ch = '\0';
}
} while (!(('"' == ch) && ('\\' != lastch)));
return ch;
} /* echostring */
/* ---------------- */
int main(void)
{
lastch = '\0';
while (EOF != (ch = fgetc(stdin))) {
if ('/' == lastch)
if (ch == '*') {
lastch = '\0';
if (EOF == stdcomment()) break;
ch = ' ';
putlast();
}
else if (ch == '/') {
lastch = '\0';
if (EOF == eolcomment()) break;
ch = '\n';
putlast(); // Eolcomment here
// Eolcomment line \
with continuation line.
}
else {
putlast();
}
else if (('"' == ch) && ('\\' != lastch)
&& ('\'' != lastch)) {
if ('"' != (ch = echostring())) {
fputs("\"Unterminated\" string\n", stderr);
/* You can exercise this code by feeding the
program an unterminated string, then EOF */
fputs("checking for\
continuation line string\n", stderr);
fputs("checking for" "concat string\n", stderr);
printf("What\\" /* embedded string */ "%s joke!\n",
/* doh */ "a /*#(K1N5");
return EXIT_FAILURE;
}
putlast();
}
else {
putlast();
}
} /* while */
putlast(/* embedded comment */);
return 0;
} /* main */
function [tblNode, tblPath, mapx] = initGraph(map, m2, node)
moveset = [1,0; 0,-1; -1,0; 0,1];
n = size(node,1);
p = sum(node(:,3)) / 2;
pcount=1;
tblNode = zeros(n, 6);
tblPath = zeros(p, 3);
mapx = map; %% .- (m2>0);
for(iterN = 1:n)
nodeXY = node(iterN, 1:2);
mapx(nodeXY(1), nodeXY(2)) = 0;
for(iterP = 1:4)
pid = 0;
cyx = nodeXY .+ moveset(iterP, :);
if(mapx(cyx(1), cyx(2)) > 1)
pid = mapx(cyx(1), cyx(2)) - 1;
nids = tblPath(pid, [1,2]);
nid = nids(nids!=iterN)
endif;
if(mapx(cyx(1), cyx(2)) == 1)
pid = pcount;
pcount += 1;
[mapx, dC, nid] = scanGraph(mapx, m2, cyx, pid);
tblPath(pid,:) = [iterN, nid, dC];
tblPath(pid,:)
endif;
tblNode(iterN, iterP) = pid;
endfor;
tblNode(iterN, [5,6]) = node(iterN, [1,2]);
tblNode(iterN,:)
mapx(nodeXY(1), nodeXY(2)) = 1;
endfor;
endfunction;
std::vector<bool>
to behave exactly like that) are used to represent the edges. The magic of this is in the functions Index2Edge and Edge2Index which translate an edge {u, v} to its corresponding index i in the bool vector.#include <vector>
#include <deque>
#include <iostream>
#include <algorithm>
#include <cstdlib>
class Grid {
public:
typedef struct { int x, y; } Point;
typedef struct { Point u, v; } Edge;
Grid(void);
Grid(int, int, bool = false);
void Init(int, int, bool = false);
bool IsCorner(const Point&) const;
bool IsOuter(const Point&) const;
bool IsInner(const Point&) const;
int N(const Point&) const;
int N(const Point&, std::deque<Point>&) const;
int Neighbours(const Point&) const;
int Neighbours(const Point&, std::deque<Point>&) const;
void AddEdge(const Edge&);
void RemoveEdge(const Edge&);
bool ExistsEdge(const Edge&) const;
void Print(std::ostream& o, const char * = nullptr) const;
int GetHeight(void) const;
int GetWidth(void) const;
void Edge2Index(const Edge&, int&) const;
void Index2Edge(int, Edge&) const;
private:
int w, h;
std::vector<bool> edges;
};
Grid::Grid(void) {
;
}
Grid::Grid(int w, int h, bool value) {
Grid::Init(w, h, value);
}
void Grid::Init(int w, int h, bool value) {
Grid::w = w;
Grid::h = h;
Grid::edges.resize(2 * h*w - h - w, value);
}
int Grid::GetHeight(void) const {
return Grid::h;
}
int Grid::GetWidth(void) const {
return Grid::w;
}
/* Number of neighbours connected to p */
int Grid::N(const Grid::Point& p) const {
int i, n = 0;
if (p.x != 0) {
Grid::Edge2Index({ p, { p.x - 1, p.y } }, i);
n += Grid::edges[i];
}
if (p.y != 0) {
Grid::Edge2Index({ p, { p.x, p.y - 1 } }, i);
n += Grid::edges[i];
}
if (p.x != Grid::w - 1) {
Grid::Edge2Index({ p, { p.x + 1, p.y } }, i);
n += Grid::edges[i];
}
if (p.y != Grid::h - 1) {
Grid::Edge2Index({ p, { p.x, p.y + 1 } }, i);
n += Grid::edges[i];
}
return n;
}
/* Number and deque list of neighbours connected to p */
int Grid::N(const Grid::Point& p, std::deque<Point>& d) const {
int i, n = 0;
if (p.x != 0) {
Grid::Edge2Index({ p, { p.x - 1, p.y } }, i);
if (Grid::edges[i]) {
n++;
d.push_back({ p.x - 1, p.y });
}
}
if (p.y != 0) {
Grid::Edge2Index({ p, { p.x, p.y - 1 } }, i);
if (Grid::edges[i]) {
n++;
d.push_back({ p.x, p.y - 1 });
}
}
if (p.x != Grid::w - 1) {
Grid::Edge2Index({ p, { p.x + 1, p.y } }, i);
if (Grid::edges[i]) {
n++;
d.push_back({ p.x + 1, p.y });
}
}
if (p.y != Grid::h - 1) {
Grid::Edge2Index({ p, { p.x, p.y + 1 } }, i);
if (Grid::edges[i]) {
n++;
d.push_back({ p.x, p.y + 1 });
}
}
return n;
}
/* Number of points that can possibly be connected to p */
int Grid::Neighbours(const Point& p) const {
if (IsInner(p)) return 4;
else if (IsCorner(p)) return 2;
else return 3;
}
/* List of points that can possibly be connected to p */
int Grid::Neighbours(const Point& p, std::deque<Point>& d) const {
int n = 0;
if (p.x > 0)
n++, d.push_back({ p.x - 1, p.y });
if (p.y > 0)
n++, d.push_back({ p.x, p.y - 1 });
if (p.x < w - 1)
n++, d.push_back({ p.x + 1, p.y });
if (p.y < h - 1)
n++, d.push_back({ p.x, p.y + 1 });
return n;
}
bool Grid::IsCorner(const Grid::Point& p) const {
return (p.x == 0 || p.x == Grid::w - 1)
&& (p.y == 0 || p.y == Grid::h - 1);
}
bool Grid::IsOuter(const Grid::Point& p) const {
return !p.x || !p.y || p.x == Grid::w - 1 || p.y == Grid::h - 1;
}
bool Grid::IsInner(const Grid::Point& p) const {
return p.x && p.y && p.x != Grid::w - 1 && p.y != Grid::h - 1;
}
/* Converts a user edge {p, q} to private class index for the edges vector */
void Grid::Edge2Index(const Grid::Edge& e, int &i) const {
if (e.u.y == e.v.y)
i = e.u.y * (w - 1) + std::min({ e.u.x, e.v.x });
else
i = h*(w - 1) + std::min({ e.u.y, e.v.y }) * w + e.u.x;
}
/* Translates an index to an edge {p, q} */
void Grid::Index2Edge(int i, Grid::Edge& e) const {
div_t x;
if (i < (w - 1)*h) {
x = div(i, w - 1);
e.v.x = 1 + (e.u.x = x.rem);
e.u.y = e.v.y = x.quot;
}
else {
i -= (w - 1)*h;
x = div(i, w);
e.u.x = e.v.x = x.rem;
e.v.y = 1 + (e.u.y = x.quot);
}
}
void Grid::AddEdge(const Grid::Edge& e) {
int i;
Grid::Edge2Index(e, i);
Grid::edges[i] = true;
}
void Grid::RemoveEdge(const Grid::Edge& e) {
int i;
Grid::Edge2Index(e, i);
Grid::edges[i] = false;
}
bool Grid::ExistsEdge(const Grid::Edge& e) const {
int i;
Grid::Edge2Index(e, i);
return Grid::edges[i];
}
void Grid::Print(std::ostream& o, const char *prefix) const {
for (int j = 0; j < Grid::h; j++) {
if (prefix) o << prefix;
for (int i = 0; i < Grid::w; i++) {
if (Grid::N({ i, j }))
o << '*';
else
o << ' ';
if (i != Grid::w - 1) {
if (Grid::ExistsEdge({ { i, j }, { i + 1, j } }))
o << "---";
else
o << " ";
}
}
o << std::endl;
if (prefix) o << prefix;
if (j != Grid::h - 1)
for (int i = 0; i < Grid::w; i++) {
if (Grid::ExistsEdge({ { i, j }, { i, j + 1 } }))
o << '|';
else
o << ' ';
if (i != Grid::w - 1)
o << " ";
}
o << std::endl;
}
}
00000000
02221330
02004030
02221330
00000000
Packing/unpacking one bit at a time like that is going to be horribly slow.It won't be, because memory is the bottleneck and superscalar architecture can unpack several bit at once.
x = word & 1;
y = word & 2;
z = word & 4;
x = word & 1
#include <climits> // CHAR_BIT
#include <cstdlib> // div, div_t
class bitref;
class bitptr {
friend class bitref;
public:
bitptr(void *memory = nullptr, int offset = 0);
bitptr& operator+=(int j);
bitptr& operator-=(int j);
bitptr& operator++();
bitptr& operator--();
bitptr operator++(int);
bitptr operator--(int);
bitref operator*() const;
bool operator==(const bitptr& rhs) const;
bitptr& operator=(void *rhs);
private:
unsigned char *p;
int i;
};
class bitref {
public:
bitref(const bitptr& p) : p(p.p), i(p.i) {};
bitref& operator=(bool b) {
if (b)
*p |= 1 << i;
else
*p &= ~(unsigned char)(1 << i);
return *this;
}
bool operator==(bool b) {
return b == (*p & (1 << i));
}
explicit operator bool() const { return *p & (1 << i); }
private:
unsigned char *p;
int i;
};
bitptr::bitptr(void *memory, int offset) {
div_t d;
p = static_cast<unsigned char *>(memory);
d = div(offset, CHAR_BIT);
p += d.quot;
i = d.rem;
}
bitptr& bitptr::operator+=(int j) {
div_t d;
d = div(j + i, CHAR_BIT);
p += d.quot;
i = d.rem;
return *this;
}
bitptr& bitptr::operator-=(int j) {
return *this += -j;
}
bitptr& bitptr::operator++() {
if (++i == CHAR_BIT) ++p, i = 0;
return *this;
}
bitptr& bitptr::operator--() {
if (--i == -1) --p, i = CHAR_BIT - 1;
return *this;
}
bitptr bitptr::operator++(int) {
bitptr val{ *this };
++*this;
return val;
}
bitptr bitptr::operator--(int) {
bitptr val{ *this };
--*this;
return val;
}
bitref bitptr::operator*(void) const {
return bitref(*this);
}
bool bitptr::operator==(const bitptr& q) const {
return p == q.p && i == q.i;
}
bitptr& bitptr::operator=(void *q) {
p = static_cast<unsigned char *>(q);
i = 0;
return *this;
}
// Example
int main(void) {
bitptr p, q;
char c = 0;
p = static_cast<void*>(&c);
q = p;
*p = *q;
q++;
*q = 1;
return 0;
}
bitptr bitptr::operator+(int j) const {
bitptr val{ *this };
val += j;
return val;
}
bitptr bitptr::operator-(int j) const {
bitptr val{ *this };
val -= j;
return val;
}
bitref bitptr::operator[](int j) const {
return *(*this + j);
};
char s = "Hello world";
bitptr p{static_cast<void*>(s)};
for(int i = 0; i < 11*CHAR_BIT; i++)
std::cout << p[i] ? "1" : "0";
std::cout << std::endl;
%% initmap.m %%
Fname = "classic.txt"
b = getMap(Fname);
b2 = b != tvalues(2);
bx = b == tvalues(6);
%%imshow(bx)
%%pause;
bz = reMap(bx, b2);
%%break;
zf = [0,1,0; 1,4,1; 0,1,0];
x = size(b2,2);
y = size(b2,1);
b3 = zeros(y,x);
b3(2:end-1, 2:end-1) = zfilter(bz, zf);
%imshow(b3 ./ 8)
bx = ((b3 > 4) .- (b3 == 6)) == 1;
by = zeros(y,x);
by(bx) = [1:sum(bx(:))];
imshow(by ./ sum(bx(:)));
nodes = sum(bx(:));
node = zeros(nodes, 3);
for(itery = 1:y)
for(iterx = 1:x)
if(bx(itery, iterx))
item = by(itery, iterx);
link = b3(itery, iterx) - 4;
node(item, :) = [itery, iterx, link];
endif;
endfor;
endfor;
figure(2);
plot(node(:,1), '-r', node(:,2), '-b', node(:,3), '-g');
maxTicks = fun_Eol(y,x);
%% end %%
function bx = reMap(bx, b2);
z2 = [0,1,0; 1,1,1; 0,1,0];
oldbx = b2;
i=0;
%%imshow(bx)
%%pause;
while(sum(oldbx(:) == bx(:)) < length(bx(:)))
oldbx = bx;
bn = (zfilter(bx, z2) > 0);
bx(2:end-1, 2:end-1) = bn;
bx = bx .* b2;
%%imshow(bx);
%%sleep(0.5);
%%++i
endwhile;
endfunction;
%% simstep.m %%
for(iterx = 1:ghosts+1)
tick = 0;
inktick = min([gtcounter, lmancounter] .- tick);
tick += inktick;
g_move = sum(gtcounter == tick);
g_id = [1:ghosts];
if(g_move>0)
gidx = g_id(gtcounter == tick);
for(iterg = 1:g_move)
id = gidx(iterg);
[gl, gs] = MoveGhost(glocate(id, :), gstatus(id,:), bz);
glocate(id,:) = gl;
gstatus(id, 2:3) = gs;
gtcounter(id) += tickgh(1, mod(id-1, 4)+1);
endfor;
endif;
lmove = lmancounter == tick;
if(lmove)
[xl, xs] = MoveGhost(lmanlocate, lmanstatus, bz); %%MoveLman();
lmanlocate = xl;
lmanstatus([2,3]) = xs;
lmancounter += ticklman(1);
endif;
endfor;
clf;
imshow(bz);
hold on; plot(glocate(:,2), glocate(:,1), 'rx', 'linewidth', 2);
plot(lmanlocate(:,2), lmanlocate(:,1), 'b+', 'linewidth', 2);
%% end %%
function [nloc,nstat] = MoveGhost(loc, status, map)
moveset = [1,0; 0,-1; -1,0; 0,1];
moves = zeros(4,1);
mid = [1:4];
for(iter = 1:4)
y = loc(1) + moveset(iter,1);
x = loc(2) + moveset(iter,2);
if( (status(2) == -moveset(iter,1)) + (status(3) == -moveset(iter,2))== 2)
lastmove = iter;
endif;
moves(iter) = map(y,x);
endfor;
nmoves = sum(moves);
if(nmoves == 1)
nstat = moveset(lastmove, :);
endif;
if(nmoves == 2)
moves(lastmove) = 0;
nstat = moveset(mid(moves==1),:);
endif;
if(nmoves > 2)
moves(lastmove) = 0;
rp = randperm(nmoves-1);
xm = mid(moves==1);
xm = xm(rp(1));
nstat = moveset(xm, :);
endif;
%%nstat
nloc = loc .+ nstat(1,:);
nstat = nstat(1,:);
endfunction;
val _ = print ("Hello, world!\n")
implement main0 () = ()