For 5, I think we can use a doubly linked list plus a hash_map.
The linked list should have a tail pointer. Each node is of a structure
struct Node {
Obj* obj;
long id;
Node* next;
Node* prev;
}
The linked list has both a header pointer and a tail pointer. The hash_map's
key is its id. It also has a Node*, which point to its corresponding Node
in the linked list.
add(id,*obj), erase(id), deque() can all be done in O(1).
add(id,*obj) adds at the head, and adds it to the hash_map too.
... 阅读全帖
My understanding is, the current coding infrastructure, i.e., high-level language source files --> obj/lib files --> executable files, does not support OO, and in particular, does not support template.
Under this infrastructure, if the implementation of STL were put in the .cpp files, which would be compiled to obj/lib files without any information from the users/caller of these STL class/functions, then the obj/lib files had to link to, and work correctly with the users/callers for all possible
I wrote some Java code afterwards. It seems not very clean. Please comment.
import java.util.*;
//Assumptions:
//1. We can move either up or down or left or right at any point in time,
but not diagonally.
//2. One item in the matrix can not be used twice to compose of the pattern
string.
//3. If different routes lead to the same indexes of matrix items to compose
of the pattern string, display them.
//4. The maximum number of movable steps is (matrix.length - 1) + (matrix[0]
.length - 1) = matri... 阅读全帖
我也被问过这个问题,当时没写好,现在再写一个:
public class DeepIterator {
private Stack stack;
public DeepIterator(List list) {
stack = new Stack();
stack.push(list);
advanceToNext();
}
public boolean hasNext() {
return !stack.isEmpty();
}
public int next() {
if (!hasNext())
throw new RuntimeException("no next");
int result = (Integer) stack.pop();
advanceToNext();
class BlockingQueue {
const size_t kBufSize = 10;
// string _buf(10, '\0'); // have to use = for in-place member
initialization
string _buf = string(kBufSize, '\0');
size_t _head = 0, _tail = 0;
condition_variable _cv;
mutex _mx;
public:
/** Retrieve and remove the head of the queue, waiting if no elements
are present. */
char take() {
unique_lock lg(_mx);
_cv.wait(lg, [this](){return _head != _tail;});
char res = _buf[_head];
_head = (1 + _head) % kBufSi... 阅读全帖
networking背景。和web公司的风格不大一样。多个公司的面筋。希望对后来人,有帮
助。
implement a hash table
lru cache
two sum
void store(int val);
store the value
bool test(int target);
return true if two stored values who sum equals target
otherwise false
We want test really fast.
O(1) test
O(n) store
Given a binary tree where parent node value is minimum of two children node
values, find the second min.
given nested list, return sum. Sum is defined as depth*current sum
what is dead lock? how to prevent it?
implement a readlock
im... 阅读全帖
下面这段是从 https://github.com/joyent/node/wiki/Installing-Node.js-via-
package-manager 抄来的哈:
Debian
Node.js is available in official repo for Debian Sid(unstable).
For Debian Squeeze, your best bet is to compile node by yourself (as root):
apt-get install python g++ make
mkdir ~/nodejs && cd $_
wget -N http://nodejs.org/dist/node-latest.tar.gz
tar xzvf node-latest.tar.gz && cd `ls -rd node-v*`
./configure
make install
我最后一步"make install"没过。
按你的说法,./configure以后我试着make,结果一样的错误:
root@debian:~/nodejs/no... 阅读全帖
1. For example:
public class BaseClass
{
public virtual void TraceSelf()
{
Trace.WriteLine("BaseClass");
}
}
public class SubClass:BaseClass
{
public override void TraceSelf()
{
Trace.WriteLine("SubClass");
}
}
BaseClass obj=new SubClass();
obj.TraceSelf(); //What's the output here?
public class BaseClass
{
public virtual void TraceSelf()
{
Trace.WriteLine("BaseClass");
}
}
public class SubClass:BaseClass
{
public new void TraceSelf()
{
Trace.WriteLine("SubClass");
}
}
BaseClass obj=new
according to the book, for an obj to be GC,
finalize() is never run more than once on any object,
I am puzzled here, since it is possible that the obj is made reachable again
in its finalize(), then after a while, this obj may again become unreachable,
then finalize might again be called. 如此往复。 so the finalize() is called
more than once.
望指教,谢谢啦。
synchornized(obj)
{
Codes to be protected
}
1 what i want to protected is the code, not the obj. To be sure only 1 thread
excute the codes a time. It is ok.
But, Question
2 so how should I choice the Obj, for design? When should I use synchornized(this)? what does it mean, for design.
synchornized(this) {
......
}
3 for
Class myclass {
int myObj=1;
synchornized void myMethod() { // not static
int something = myObj;
..........
}
}
myclass mca,mcb;
mca.myMethod();
mcb.myMethod()
Hmm, this is correct but isn't entirely accurate - obj does not necessarily
cover the resource that need to be controlled, if there is some resource
at all. Technically you can synchronize on obj and do things inside
the synch block that completely don't have to do with obj. In fact,
it's a useful technique to use an Object instance as a primitive
semaphore, and create a critical section by synchronizing on it.
obj = select * from OBJ_TABLE;
update OBJ_TABLE set name=obj.name, version = version +1
where id=1001 and version=obj.version
only 1 update will succeed.
hehe, not related to compilor ba.
when a.cpp is compiled, myFunc(vector) is generated in a.obj
when b.cpp is compiled, myFunc(vector) is generated in b.obj
when these two obj get linked, same function definition colided.
Is there a way to tell compiler (VC6) not to use MFC during debugging? I am
tring to build an win32 app without using MFC. However, I keep getting foll
owing errors:
nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsign
ed int)" (??2@YAPAXI@Z) already defined in msvcrtd.lib(MSVCRTD.dll)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void
*)" (??3@YAXPAX@Z) already defined in msvcrtd.lib(MSVCRTD.dll)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void *
I got a linking error:
>cl 1.cpp 2.cpp /EHsc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80
x86
Copyright (C) Microsoft Corporation. All rights reserved.
1.cpp
2.cpp
Generating Code...
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:1.exe
1.obj
2.obj
2.obj : error LNK2019: unresolved external symbol "int i" (?i@@3HA) referenc
ed in function _main
1.exe : fatal error LNK1120: 1 unresolved extern... 阅读全帖
in Makefile
you can do
obj/%.o: src/%.c
[ Tab here ]gcc $(CFLAGS) -c $? -o $@
also you can do it in linux shell
mkdir -p obj
for i in *c; do gcc -c $i -o obj/`basename $i|sed 's/.c$/.o/g'`;done
Every JavaScript object has an internal property called [[Prototype]]. If
you look up a property via obj.propName or obj['propName'] and the object
does not have such a property - which can be checked via obj.hasOwnProperty(
'propName') - the runtime looks up the property in the object referenced by
[[Prototype]] instead. If the prototype-object also doesn't have such a
property, its prototype is checked in turn, thus walking the original object
's prototype-chain until a match is found or its e... 阅读全帖
这个c++ ref是ptr的别名,ref本身不是一个单独的obj,没有自己的地址,只用来调用
new obj
Because references are not objects, they don’t have
addresses. Hence, we may not define a pointer to a reference.
你的那个Obj *ptr=&ref是有问题的
我就是这样用的,但是不行呀
OBJS = a1.o a2.o
run:$(OBJS)
cc -g -o run $(OBJ)
a1.o:a1.c
cc -g -c a1.c
a2.o:a2.c
cc -g -c a2.c
when gdb open run, there echo:
No debugging symbols found..