theory

Initialization in definition vs. initialization in constructor [duplicate]

元气小坏坏 提交于 2019-12-01 22:17:11
This question already has an answer here: What are the best practices for determining the tasks of Constructor, Initialization and Reset methods 5 answers In Java, but in other OO languages as well, is there a difference between initializing an attribute in its definition, as in class Example { public Sample sample_attribute = new Sample(); } and using a constructor to initialize it? class Example { public Sample sample_attribute; public Example() { sample_attribute = new Sample(); } } I could not think of any practical difference, is there one? Otherwise, are there cases in which one method

Get X unique numbers from a set

我的未来我决定 提交于 2019-12-01 20:44:23
问题 What is the most elegant way to grab unique random numbers I ponder? At the moment I need random unique numbers, I check to see if it's not unique by using a while loop to see if I've used the random number before. So It looks like: int n = getRandomNumber % [Array Size]; for each ( Previously used n in list) Check if I've used n before, if I have...try again. There are many ways to solve this linear O(n/2) problem, I just wonder if there is a elegant way to solve it. Trying to think back to

Why can the 'as' operator not be used to parse non-nullable value types?

寵の児 提交于 2019-12-01 19:39:45
Every Developer has his/her own standards. Some developers like to <type>.TryParse() , some developers like to cast using (type)object; , and some developers love using the keywords instead. I noticed a hiccup with the 'as' operator - you cannot use it to perform conversions between non-nullable value types. I read the documentation on MSDN on the as Keyword and they also explain it as "You can use the as operator to perform certain types of conversions between compatible reference types or nullable types." I tested this with the following: int i = 0; var k = i as int; //Breaks int i = 0; var

Get X unique numbers from a set

眉间皱痕 提交于 2019-12-01 19:01:10
What is the most elegant way to grab unique random numbers I ponder? At the moment I need random unique numbers, I check to see if it's not unique by using a while loop to see if I've used the random number before. So It looks like: int n = getRandomNumber % [Array Size]; for each ( Previously used n in list) Check if I've used n before, if I have...try again. There are many ways to solve this linear O(n/2) problem, I just wonder if there is a elegant way to solve it. Trying to think back to MATH115 Discrete mathematics and remember if the old lecturer covered anything to do with a seemingly

How does addition work in Computers?

眉间皱痕 提交于 2019-12-01 18:14:52
I was watching a video on computer architecture and a question came to my mind. How does addition and basic operations work on computers? I mean, i know that 2+2 = 4 but i don't know why? i just know that if i add 2 apples to another 2 then i see 4, but is there a possible demonstration of this? My question being is, how does a computer know that 2 + 2 = 4 at the most basic level? i know there are functions that add numbers, but at a basic level how is that addition performed? I just want to know this to understand better how computers work as the most basic and used operation performed by a

Trick to divide a constant (power of two) by an integer

随声附和 提交于 2019-12-01 17:08:38
问题 NOTE This is a theoretical question. I'm happy with the performance of my actual code as it is. I'm just curious about whether there is an alternative. Is there a trick to do an integer division of a constant value, which is itself an integer power of two, by an integer variable value, without having to use do an actual divide operation? // The fixed value of the numerator #define SIGNAL_PULSE_COUNT 0x4000UL // The division that could use a neat trick. uint32_t signalToReferenceRatio(uint32_t

How do I mimic access modifiers in JavaScript with the Prototype library?

旧城冷巷雨未停 提交于 2019-12-01 10:47:17
I've been working with the prototype library for some time now and occasionally find myself wishing I had multiple access levels (public, private, and protected). The closest I've come so far is the following: SampleBase = Class.create({ /* virtual public constructor */ initialize: function(arg1, arg2) { // private variables var privateVar1, privateVar2; // private methods var privateMethod1 = function() { } function privateMethod2() { } // public (non virtual) methods this.publicNonVirtual1 = function() { return privateVar1; } this.publicNonVirtual2 = function() { return privateVar2; } }, //

how does cvFindContours work?

半城伤御伤魂 提交于 2019-12-01 09:18:13
问题 I'm using cvFindContours function in c++ and openCv 2.1, since I have to make a documentation of the function I'm using can someone tell me how this function works, which method for find contours uses and if it possible some theory at the basis of the function? 回答1: As you can read in the documentation, this function uses the method described in "Suzuki, S. and Abe, K., Topological Structural Analysis of Digitized Binary Images by Border Following. CVGIP 30 1, pp 32-46 (1985)". 来源: https:/

Theory, examples of reversible parsers?

六月ゝ 毕业季﹏ 提交于 2019-12-01 07:39:31
Does anyone out there know about examples and the theory behind parsers that will take (maybe) an abstract syntax tree and produce code, instead of vice-versa. Mathematically, at least intuitively, I believe the function of code->AST is reversible, but I'm trying to find work/examples of this... besides the usual resources like the Dragon book and such. Any ideas? Such thing is called a Visitor. Is traverses the tree and does whatever has to be done, for example optimize or generate code. I rather like lewap's response: find a mathematical way to express a visitor and you have a dual to the