lookup

Which is faster, Hash lookup or Binary search?

对着背影说爱祢 提交于 2019-11-26 17:57:01
问题 When given a static set of objects (static in the sense that once loaded it seldom if ever changes) into which repeated concurrent lookups are needed with optimal performance, which is better, a HashMap or an array with a binary search using some custom comparator? Is the answer a function of object or struct type? Hash and/or Equal function performance? Hash uniqueness? List size? Hashset size/set size? The size of the set that I'm looking at can be anywhere from 500k to 10m - incase that

What is the best data structure for storing a set of four (or more) values?

南楼画角 提交于 2019-11-26 16:38:11
问题 Say I have the following variables and its corresponding values which represents a record . name = 'abc' age = 23 weight = 60 height = 174 Please note that the value could be of different types ( string , integer , float , reference-to-any-other-object, etc). There will be many records (at least >100,000). Each and every record will be unique when all these four variables (actually its values ) are put together. In other words, there exists no record with all 4 values are the same. I am

How to get the Country according to a certain IP?

二次信任 提交于 2019-11-26 15:47:13
Does anyone know of a simple way to retrieve the country for a given IP Address? Preferably in ISO_3166-1 format? A lot of people (including my company) seem to use MaxMind GeoIP. They have a free version GeoLite which is not as accurate as the paid version, but if you're just after something simple, it may be good enough. Warren Blanchet There are two approaches: using an Internet service and using some kind of local list (perhaps wrapped in a library). What you want will depend on what you are building. For services: http://www.hostip.info/use.html (as mentioned by Mark ) http://www.team

Checking if Date is Between two Dates in R

柔情痞子 提交于 2019-11-26 14:52:07
问题 I have two large datasets, df1 and df2. The first dataset, df1, contains the columns 'ID' and 'actual.data'. df1 <- data.frame(ID=c(1,1,1,2,3,4,4), actual.date=c('10/01/1997','2/01/1998','5/01/2002','7/01/1999','9/01/2005','5/01/2006','2/03/2003')); dcis <- grep('date$',names(df1)); df1[dcis] <- lapply(df1[dcis],as.Date,'%m/%d/%Y'); df1; ID actual.date 1 1 1997-10-01 2 1 1998-02-01 3 1 2002-05-01 4 2 1999-07-01 5 3 2005-09-01 6 4 2006-05-01 7 4 2003-02-03 The second dataset, df2, contains two

Fast computing of log2 for 64-bit integers

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 12:54:32
A great programming resource, Bit Twiddling Hacks, proposes ( here ) the following method to compute log2 of a 32-bit integer: #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n static const char LogTable256[256] = { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7) }; unsigned int v; // 32-bit word to find the log of unsigned r; // r will be lg(v) register unsigned int t, tt; // temporaries if (tt = v >> 16) { r = (t = tt >> 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt]; } else {

How can I lookup a Java enum from its String value?

旧巷老猫 提交于 2019-11-26 12:48:07
I would like to lookup an enum from its string value (or possibly any other value). I've tried the following code but it doesn't allow static in initialisers. Is there a simple way? public enum Verbosity { BRIEF, NORMAL, FULL; private static Map<String, Verbosity> stringMap = new HashMap<String, Verbosity>(); private Verbosity() { stringMap.put(this.toString(), this); } public static Verbosity getVerbosity(String key) { return stringMap.get(key); } }; Gareth Davis Use the valueOf method which is automatically created for each Enum. Verbosity.valueOf("BRIEF") == Verbosity.BRIEF For arbitrary

pandas loc vs. iloc vs. ix vs. at vs. iat?

旧巷老猫 提交于 2019-11-26 10:59:09
Recently began branching out from my safe place (R) into Python and and am a bit confused by the cell localization/selection in Pandas . I've read the documentation but I'm struggling to understand the practical implications of the various localization/selection options. Is there a reason why I should ever use .loc or .iloc over the most general option .ix ? I understand that .loc , iloc , at , and iat may provide some guaranteed correctness that .ix can't offer, but I've also read where .ix tends to be the fastest solution across the board. Please explain the real-world, best-practices

A dictionary object that uses ranges of values for keys

て烟熏妆下的殇ゞ 提交于 2019-11-26 09:03:07
问题 I have need of a sort of specialized dictionary. My use case is this: The user wants to specify ranges of values (the range could be a single point as well) and assign a value to a particular range. We then want to perform a lookup using a single value as a key. If this single value occurs within one of the ranges then we will return the value associated to the range. For example: // represents the keyed value struct Interval { public int Min; public int Max; } // some code elsewhere in the

How to get the index of an item in a list in a single step?

扶醉桌前 提交于 2019-11-26 07:17:23
问题 How can I find the index of an item in a list without looping through it? Currently this doesn\'t look very nice - searching through the list for the same item twice, just to get the index: var oProp = something; int theThingIActuallyAmInterestedIn = myList.IndexOf(myList.Single(i => i.Prop == oProp)); 回答1: How about the List.FindIndex Method: int index = myList.FindIndex(a => a.Prop == oProp); This method performs a linear search; therefore, this method is an O(n) operation, where n is Count

What is the point of Lookup<TKey, TElement>?

和自甴很熟 提交于 2019-11-26 06:14:28
问题 The MSDN explains Lookup like this: A Lookup<TKey, TElement> resembles a Dictionary<TKey, TValue>. The difference is that a Dictionary<TKey, TValue> maps keys to single values, whereas a Lookup<TKey, TElement> maps keys to collections of values. I don\'t find that explanation particularly helpful. What is Lookup used for? 回答1: It's a cross between an IGrouping and a dictionary. It lets you group items together by a key, but then access them via that key in an efficient manner (rather than