counting

Reference Counting in C++

五迷三道 提交于 2019-12-05 00:43:44
问题 I'm implementing a math library in C++. The library will be compiled to a DLL so those who use it will only need the header files the classes' definitions. The users of my classes will be people who are new to the language. However, there are some objects that might be referenced in several parts of their programs. Since I don't expect them to do the memory management, I'd like to do it myself. Therefore, I have to implement reference counting (garbage collection is not a possibility). I want

Finding all closed loops in a graph

半城伤御伤魂 提交于 2019-12-04 16:39:27
I am trying to write a Python code which will identify all of the closed loops within an arbitrary graph. By a closed loop, I mean a loop which visits no vertex more than once, with the exception of the vertex the loop starts on (in the case of this picture , DGHD is an example, as is BCDB , or BCEFDB , or etc). I tried doing this with matrix multiplication, writing the graph as a matrix with 1s where 2 verticies are connected, and a 0 where they aren't, and putting them to the nth power, however this will take into account non closed loops also. This person seems to have had the same job, but

Counting Overlaps of Integer Ranges

我的梦境 提交于 2019-12-04 11:06:28
I've been stumped on this algorithm for quite a bit. Say there are four ranges of integers. Each range has a Start and an End value. Range A: 0,5 Range B: 4,12 Range C: 2,10 Range D: 8,14 From these values I would like to get a new set which counts of the number of the ranges that fall in a particular span of ints. Each of these would have Start, End and Count values, producing something like this: (Start, End, Count) 0,1,1 (Only 1 range (A) falls between 0 and 1 inclusive) 2,3,2 (2 ranges (A,C)) 4,5,3 (3 ranges (A,B,C)) 6,7,2 (2 ranges (B,C)) 8,10,3 (3 ranges (B,C,D)) 11,12,2 (2 ranges (B,D))

Find the number of files in a directory

我只是一个虾纸丫 提交于 2019-12-04 09:57:17
问题 Is there any method in Linux to calculate the number of files in a directory (that is, immediate children) in O(1) (independently of the number of files) without having to list the directory first? If not O(1), is there a reasonably efficient way? I'm searching for an alternative to ls | wc -l . 回答1: readdir is not as expensive as you may think. The knack is avoid stat'ing each file, and (optionally) sorting the output of ls. /bin/ls -1U | wc -l avoids aliases in your shell, doesn't sort the

Grouping and counting to get a closerate

走远了吗. 提交于 2019-12-04 02:31:39
I want to count per country the number of times the status is open and the number of times the status is closed . Then calculate the closerate per country . Data: customer <- c(1,2,3,4,5,6,7,8,9) country <- c('BE', 'NL', 'NL','NL','BE','NL','BE','BE','NL') closeday <- c('2017-08-23', '2017-08-05', '2017-08-22', '2017-08-26', '2017-08-25', '2017-08-13', '2017-08-30', '2017-08-05', '2017-08-23') closeday <- as.Date(closeday) df <- data.frame(customer,country,closeday) Adding status : df$status <- ifelse(df$closeday < '2017-08-20', 'open', 'closed') customer country closeday status 1 1 BE 2017-08

Function to count hashtags

旧巷老猫 提交于 2019-12-03 21:21:42
I'm trying to get a function that counts and shows hashtags of a list. Example input: ["Hey, im in the #pool", "beautiful #city", "#city is nice", "Have a nice #weekend", "#weekend <3", "Nice #"] Output: {"pool" : 1, "city" : 2, "weekend" : 2} But if there is only a # followed by no words, it should not count as a hashtag. Same with stuff before the hashtag, something like „%#“ is not allowed to count as a hashtag. Hashtags are defined (a-z,A-Z,0-9) every other char ends the hashtag My current code: def analyze(posts): tag = {} for sentence in posts: words = sentence.split(' ') for word in

How do I count the characters, words, and lines in a file, using Perl?

点点圈 提交于 2019-12-03 19:23:00
问题 What is a good/best way to count the number of characters, words, and lines of a text file using Perl (without using wc)? 回答1: Here's the perl code. Counting words can be somewhat subjective, but I just say it's any string of characters that isn't whitespace. open(FILE, "<file.txt") or die "Could not open file: $!"; my ($lines, $words, $chars) = (0,0,0); while (<FILE>) { $lines++; $chars += length($_); $words += scalar(split(/\s+/, $_)); } print("lines=$lines words=$words chars=$chars\n");

Reference Counting in C++

纵然是瞬间 提交于 2019-12-03 15:17:29
I'm implementing a math library in C++. The library will be compiled to a DLL so those who use it will only need the header files the classes' definitions. The users of my classes will be people who are new to the language. However, there are some objects that might be referenced in several parts of their programs. Since I don't expect them to do the memory management, I'd like to do it myself. Therefore, I have to implement reference counting (garbage collection is not a possibility). I want to make that reference counting as transparent as possible, for example... // Define a Bézier curve

Fastest way to count number of bit transitions in an unsigned int

与世无争的帅哥 提交于 2019-12-03 07:51:11
I'm looking for the fastest way of counting the number of bit transitions in an unsigned int . If the int contains: 0b00000000000000000000000000001010 The number of transitions are: 4 If the int contains: 0b00000000000000000000000000001001 The number of transitions are: 3 Language is C. int numTransitions(int a) { int b = a >> 1; // sign-extending shift properly counts bits at the ends int c = a ^ b; // xor marks bits that are not the same as their neighbors on the left return CountBits(c); // count number of set bits in c } For an efficient implementation of CountBits see http://graphics

Find the number of files in a directory

旧街凉风 提交于 2019-12-03 05:29:18
Is there any method in Linux to calculate the number of files in a directory (that is, immediate children) in O(1) (independently of the number of files) without having to list the directory first? If not O(1), is there a reasonably efficient way? I'm searching for an alternative to ls | wc -l . readdir is not as expensive as you may think. The knack is avoid stat'ing each file, and (optionally) sorting the output of ls. /bin/ls -1U | wc -l avoids aliases in your shell, doesn't sort the output, and lists 1 file-per-line (not strictly necessary when piping the output into wc). The original