theory

Visitor pattern. Is void* an acceptable return type for a completely abstract interface?

こ雲淡風輕ζ 提交于 2019-12-03 14:46:30
I have an AST, represented in the usual way (a tree of nodes of an abstract type). I have several uses cases for traversing this tree (an optimizer, which returns another AST; IR code generation, which returns a llvm::Value* ; and a debug analyzer, which simply outputs to stdout and returns nothing). A visitor feels like the right way to go here, but the differing return types through each use case of the visitor make it hard to see how to implement an interface for this. I considered this: class Visitor; class ASTNode { public: virtual void accept(Visitor *visitor); }; class Visitor { public:

Partition Problems Brute Force Algorithm

…衆ロ難τιáo~ 提交于 2019-12-03 13:03:53
问题 I am trying to do the pseudocode for the partition problem below in bruteforce. a set of integers X and an integer k (k >1). Find k subsets of X such that the numbers in each subset sum to the same amount and no two subsets have an element in common, or conclude that no such k subsets exist. The problem is NP-Complete For example, with X = {2, 5, 4, 9, 1, 7, 6, 8} and k = 3, a possible solution would be: {2, 5, 7}, {4, 9, 1}, {6, 8} because all of them sum up to 14. for exhaustive search I

Why is multiplying cheaper than dividing?

对着背影说爱祢 提交于 2019-12-03 12:49:31
I recently wrote a Vector 3 class, and I submitted my normalize() function for reviewal to a friend. He said it was good, but that I should multiply by the reciprocal where possible because "multiplying is cheaper than dividing" in CPU time. My question simply is, why is that? Think about it in terms of elementary operations that hardware can more easily implement -- add, subtract, shift, compare. Multiplication even in a trivial setup requires fewer such elementary steps -- plus, it afford advances algorithms that are even faster -- see here for example... but hardware generally doesn't take

What do we call this (new?) higher-order function?

冷暖自知 提交于 2019-12-03 12:31:59
问题 I am trying to name what I think is a new idea for a higher-order function. To the important part, here is the code in Python and Haskell to demonstrate the concept, which will be explained afterward. Python: >>> def pleat(f, l): return map(lambda t: f(*t), zip(l, l[1:])) >>> pleat(operator.add, [0, 1, 2, 3]) [1, 3, 5] Haskell: Prelude> let pleatWith f xs = zipWith f xs (drop 1 xs) Prelude> pleatWith (+) [0,1,2,3] [1,3,5] As you may be able to infer, the sequence is being iterated through,

Is conditional branching a requirement of Turing-completeness?

自古美人都是妖i 提交于 2019-12-03 12:23:26
问题 I've been searching the web and I'm finding somewhat contradictory answers. Some sources assert that a language/machine/what-have-you is Turing complete if and only if it has both conditional and unconditional branching (which I guess is kind of redundant), some say that only unconditional is required, others that only conditional is required. Reading about the German Z3 and ENIAC, Wikipedia says: The German Z3 (shown working in May 1941) was designed by Konrad Zuse. It was the first general

Powerful algorithms too complex to implement [closed]

天大地大妈咪最大 提交于 2019-12-03 12:21:48
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . What are some algorithms of legitimate utility that are simply too complex to implement? Let me be clear: I'm not looking for algorithms like the current asymptotic optimal matrix multiplication algorithm, which

crossing edges in the travelling salesman problem

青春壹個敷衍的年華 提交于 2019-12-03 11:19:06
Does there exist a travelling salesman problem where the optimal solution has edges that cross? The nodes are in an x-y plane, so crossing in this case means if you were to draw the graph, two line segments connecting four separate nodes would intersect. If two edges in a closed polygonal line cross, then there is a polygonal line with the same vertices but with smaller perimeter. This is a consequence of the triangle inequality. So, a solution to the TSP must be a simple polygon. See this article (Figure 4). If you consider a non-Euclidean metric like L1 (Manhattan distance), then it's pretty

Partially Overriding a Virtual Auto-Property in a Child Class

蹲街弑〆低调 提交于 2019-12-03 11:01:41
问题 Time for a theoretical question I just ran across. The following code is valid and compiles: public class Parent { public virtual object TestProperty { get; set; } } public class Child : Parent { private string _testValue = "Hello World!"; public override object TestProperty { get { return _testValue; } } } public class Consumer { Parent p = new Child(); public Consumer(){ p.TestProperty = 3; } } My question is: Why does C# allow me to partially override the TestProperty auto property in a

What is the use of finite automata? [closed]

泪湿孤枕 提交于 2019-12-03 09:34:50
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . What is the use of finite automata? And all the concepts that we study in the theory of computation. I've never seen their uses yet. 回答1: They are the theoretical underpinnings of concepts widely used in computer science and programming, and understanding them helps you better

Split down a number in seconds to days, hours, minutes, and seconds?

蹲街弑〆低调 提交于 2019-12-03 09:32:17
问题 I've heard that it's possible to accomplish this using the modulus % operator present in most programming languages. The real question is, how? I'm unfamiliar with how modulus works, so I've had difficulties using it in the past. Given the present time here in seconds since 1970, 1307758473.484 , how can I calculate how many years that is, days that is, hours that is, and minutes that is using modulus? I'm essentially looking to format it like this: "5 years, 10 days, 12 hours, 7 minutes, and