language-implementation

Why must Python list addition be homogenous?

痴心易碎 提交于 2019-11-29 10:57:47
问题 Can anyone familiar with Python's internals (CPython, or other implementations) explain why list addition is required to be homogenous: In [1]: x = [1] In [2]: x+"foo" --------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\Users\Marcin\<ipython-input-2-94cd84126ddc> in <module>() ----> 1 x+"foo" TypeError: can only concatenate list (not "str") to list In [3]: x+="foo" In [4]: x Out[4]: [1, 'f', 'o', 'o'] Why shouldn't the x+

Understanding the implementation of memcpy()

隐身守侯 提交于 2019-11-29 09:34:26
I was looking the implementation of memcpy.c, I found a different memcpy code. I couldnt understand why do they do (((ADDRESS) s) | ((ADDRESS) d) | c) & (sizeof(UINT) - 1) #if !defined(__MACHDEP_MEMFUNC) #ifdef _MSC_VER #pragma function(memcpy) #undef __MEMFUNC_ARE_INLINED #endif #if !defined(__MEMFUNC_ARE_INLINED) /* Copy C bytes from S to D. * Only works if non-overlapping, or if D < S. */ EXTERN_C void * __cdecl memcpy(void *d, const void *s, size_t c) { if ((((ADDRESS) s) | ((ADDRESS) d) | c) & (sizeof(UINT) - 1)) { BYTE *pS = (BYTE *) s; BYTE *pD = (BYTE *) d; BYTE *pE = (BYTE *) ((

what exactly is a “register machine”?

不问归期 提交于 2019-11-28 20:05:13
问题 From http://code.google.com/p/unladen-swallow/wiki/ProjectPlan I quote: "Using a JIT will also allow us to move Python from a stack-based machine to a register machine, which has been shown to improve performance in other similar languages (Ierusalimschy et al, 2005; Shi et al, 2005)." In college I built a simple compiler for a language with recursive procedures - which maintained stack frames for each procedure called - so that they can be called recursively and so that parameters and return

Are there any Common Lisp implementations for .Net?

谁都会走 提交于 2019-11-28 18:28:45
问题 Are there any Common Lisp implementations for .Net? 回答1: I haven't looked at it recently, but at least in the past there were some problems with fully implementing common lisp on the CLR, and I'd be a little surprised if this has changed. The issues come up with things like the handling of floats where .net/clr has a way to do it that is a) subtly incorrect b) disagrees with the ANSI standard for common lisp but c) doesn't allow any way around this. There are other similar problems. This

How could one implement C++ virtual functions in C [duplicate]

家住魔仙堡 提交于 2019-11-28 16:50:55
问题 This question already has answers here : Closed 9 years ago . The C++ language provides virtual functions. Within the constraints of a pure C language implementation, how can a similar effect be achieved? 回答1: Stolen from here. From the C++ class class A { protected: int a; public: A() {a = 10;} virtual void update() {a++;} int access() {update(); return a;} }; a C code fragment can be derived. The three C++ member functions of class A are rewritten using out-of-line (standalone) code and

range for integer values of chars in c++

被刻印的时光 ゝ 提交于 2019-11-28 12:00:50
I'm reading The C++ Programming Language and in it Stroustrup states that the int value of a char can range from 0 to 255 or -127 to 127, depending on implementation. Is this correct? It seems like it should be from -128 to 127. If not, why are their only 255 possible values in the second implementation possibility, not 256. Roddy You're stuck in two's complement thinking - The C++ standard does not define the representation used for negative numbers! If your computer (god forbid) uses ones's complement to represent negative numbers, you have a range of -127 to + 127 in an 8-bit byte. On the

Understanding the implementation of memcpy()

半城伤御伤魂 提交于 2019-11-28 02:58:33
问题 I was looking the implementation of memcpy.c, I found a different memcpy code. I couldnt understand why do they do (((ADDRESS) s) | ((ADDRESS) d) | c) & (sizeof(UINT) - 1) #if !defined(__MACHDEP_MEMFUNC) #ifdef _MSC_VER #pragma function(memcpy) #undef __MEMFUNC_ARE_INLINED #endif #if !defined(__MEMFUNC_ARE_INLINED) /* Copy C bytes from S to D. * Only works if non-overlapping, or if D < S. */ EXTERN_C void * __cdecl memcpy(void *d, const void *s, size_t c) { if ((((ADDRESS) s) | ((ADDRESS) d)

Why is __FILE__ uppercase and __dir__ lowercase?

夙愿已清 提交于 2019-11-27 19:57:46
In Ruby 2.0.0-p0, the __dir__ variable was introduced for easy access to the directory of the file currently being executed. Why is __dir__ lowercase when __FILE__ is uppercase? Intrepidd I think that is because __FILE__ is a parse-time constant whereas __dir__ is a function and returns File.dirname(File.realpath(__FILE__)) For more details, see This discussion TL; DR The relative merits of language implementation choices are outside the scope of a reasonable Stack Overflow question. However, this is a good question because it identifies a potentially confusing use case in the language and

Internal implementation of java.util.HashMap and HashSet

怎甘沉沦 提交于 2019-11-27 18:50:45
I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet . Following are the doubts popping in my mind for a while: Whats is the importance of the @Override public int hashcode() in a HashMap/HashSet? Where is this hash code used internally? I have generally seen the key of the HashMap be a String like myMap<String,Object> . Can I map the values against someObject (instead of String) like myMap<someObject, Object> ? What all contracts do I need to obey for this happen successfully? Thanks in advance ! EDIT: Are we saying that the hash code of the

How are java interfaces implemented internally? (vtables?)

痴心易碎 提交于 2019-11-27 17:23:53
C++ has multiple inheritance. The implementation of multiple inheritance at the assembly level can be quite complicated, but there are good descriptions online on how this is normally done (vtables, pointer fixups, thunks, etc). Java doesn't have multiple implementation inheritance, but it does have multiple interface inheritance, so I don't think a straight forward implementation with a single vtable per class can implement that. How does java implement interfaces internally? I realize that contrary to C++, Java is Jit compiled, so different pieces of code might be optimized differently, and