{"action":"created","comment":{"url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/comments/28657773","id":28657773,"diff_hunk":"@@ -0,0 +1,107 @@\n+#ifndef WEIGHTED_LIST_H\n+#define WEIGHTED_LIST_H\n+\n+#include \"rng.h\"\n+#include <vector>\n+\n+template <typename T> struct weighted_obect {\n+    T obj;\n+    int weight;\n+};\n+\n+template <typename T> struct weighted_list {\n+    weighted_list() : total_weight(0) { };\n+\n+    /**\n+     * This will add a new object to the weighted list.\n+     * @param obj The object that will be added to the list.\n+     * @param weight The weight of the object.\n+     */\n+    void add_item(T obj, int weight) {\n+        if(weight >= 0) {\n+            weighted_obect<T> new_weighted = { obj, weight };\n+            objects.push_back(new_weighted);\n+            total_weight += weight;\n+        }\n+    }\n+\n+    /**\n+     * This will check to see if the given object is already in the weighted\n+       list. If it is, we update its weight with the new weight value. If it\n+       is not, we add it normally.\n+     * @param obj The object that will be updated or added to the list.\n+     * @param weight The new weight of the object.\n+     */\n+    void add_or_replace_item(T obj, int weight) {\n+        if(weight >= 0) {\n+            for(auto &itr : objects) {\n+                if(itr.obj == obj) {\n+                    total_weight += (weight - itr.weight);\n+                    itr.weight = weight;\n+                    return;\n+                }\n+            }\n+\n+            // if not found, add to end of list\n+            add_item(obj, weight);\n+        }\n+    }\n+\n+    /**\n+     * This will call the given callback function once for every object in the weighted list.\n+     * @param func The callback function.\n+     */\n+    void apply(void (*func)(T &obj)) {\n+        for(auto &itr : objects) {\n+            func(itr.obj);\n+        }\n+    }\n+\n+    /**\n+     * This will return a pointer to an object from the list randomly selected\n+     * and biased by weight. If the weighted list is empty or all items in it\n+     * have a weight of zero, it will return a NULL pointer.","path":"src/weighted_list.h","position":63,"original_position":63,"commit_id":"cda592b47307fb65c43c22b8cefceae52ce8b521","original_commit_id":"cda592b47307fb65c43c22b8cefceae52ce8b521","user":{"login":"i2amroy","id":4275617,"avatar_url":"https://avatars.githubusercontent.com/u/4275617?v=3","gravatar_id":"","url":"https://api.github.com/users/i2amroy","html_url":"https://github.com/i2amroy","followers_url":"https://api.github.com/users/i2amroy/followers","following_url":"https://api.github.com/users/i2amroy/following{/other_user}","gists_url":"https://api.github.com/users/i2amroy/gists{/gist_id}","starred_url":"https://api.github.com/users/i2amroy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/i2amroy/subscriptions","organizations_url":"https://api.github.com/users/i2amroy/orgs","repos_url":"https://api.github.com/users/i2amroy/repos","events_url":"https://api.github.com/users/i2amroy/events{/privacy}","received_events_url":"https://api.github.com/users/i2amroy/received_events","type":"User","site_admin":false},"body":"It wouldn't, but for things that are JSON based we need to be able to handle floats there so that the default entry weight for 1 isn't \"10000\" in some cases. If handle that internally in the class by just multiplying all the values to deal with the smallest float when picking one at random or if we do something else it doesn't matter, but that is functionality we need if we want to use this project wide.","created_at":"2015-04-19T22:41:14Z","updated_at":"2015-04-19T22:41:14Z","html_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12074#discussion_r28657773","pull_request_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12074","_links":{"self":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/comments/28657773"},"html":{"href":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12074#discussion_r28657773"},"pull_request":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12074"}}},"pull_request":{"url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12074","id":33621716,"html_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12074","diff_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12074.diff","patch_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12074.patch","issue_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12074","number":12074,"state":"open","locked":false,"title":"Merging weighted lists","user":{"login":"KiwiTrinsic","id":11889244,"avatar_url":"https://avatars.githubusercontent.com/u/11889244?v=3","gravatar_id":"","url":"https://api.github.com/users/KiwiTrinsic","html_url":"https://github.com/KiwiTrinsic","followers_url":"https://api.github.com/users/KiwiTrinsic/followers","following_url":"https://api.github.com/users/KiwiTrinsic/following{/other_user}","gists_url":"https://api.github.com/users/KiwiTrinsic/gists{/gist_id}","starred_url":"https://api.github.com/users/KiwiTrinsic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KiwiTrinsic/subscriptions","organizations_url":"https://api.github.com/users/KiwiTrinsic/orgs","repos_url":"https://api.github.com/users/KiwiTrinsic/repos","events_url":"https://api.github.com/users/KiwiTrinsic/events{/privacy}","received_events_url":"https://api.github.com/users/KiwiTrinsic/received_events","type":"User","site_admin":false},"body":"While investigating what it would take to improve the vehicle spawning, I noticed that there was no generic weighted list class. It seems there are many, slightly different versions of weighted lists throughout the code. Since improving the vehicle spawning would require yet another weighted list, I thought it would be useful to simply create a generic version that could be applied whenever such a list was needed. I've also gone ahead and replaced two of the weighted list instances with the new generic version.","created_at":"2015-04-19T16:50:44Z","updated_at":"2015-04-19T22:41:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12074/commits","review_comments_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12074/comments","review_comment_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/comments{/number}","comments_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12074/comments","statuses_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/statuses/cda592b47307fb65c43c22b8cefceae52ce8b521","head":{"label":"KiwiTrinsic:merging-weighted-lists","ref":"merging-weighted-lists","sha":"cda592b47307fb65c43c22b8cefceae52ce8b521","user":{"login":"KiwiTrinsic","id":11889244,"avatar_url":"https://avatars.githubusercontent.com/u/11889244?v=3","gravatar_id":"","url":"https://api.github.com/users/KiwiTrinsic","html_url":"https://github.com/KiwiTrinsic","followers_url":"https://api.github.com/users/KiwiTrinsic/followers","following_url":"https://api.github.com/users/KiwiTrinsic/following{/other_user}","gists_url":"https://api.github.com/users/KiwiTrinsic/gists{/gist_id}","starred_url":"https://api.github.com/users/KiwiTrinsic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KiwiTrinsic/subscriptions","organizations_url":"https://api.github.com/users/KiwiTrinsic/orgs","repos_url":"https://api.github.com/users/KiwiTrinsic/repos","events_url":"https://api.github.com/users/KiwiTrinsic/events{/privacy}","received_events_url":"https://api.github.com/users/KiwiTrinsic/received_events","type":"User","site_admin":false},"repo":{"id":34054260,"name":"Cataclysm-DDA","full_name":"KiwiTrinsic/Cataclysm-DDA","owner":{"login":"KiwiTrinsic","id":11889244,"avatar_url":"https://avatars.githubusercontent.com/u/11889244?v=3","gravatar_id":"","url":"https://api.github.com/users/KiwiTrinsic","html_url":"https://github.com/KiwiTrinsic","followers_url":"https://api.github.com/users/KiwiTrinsic/followers","following_url":"https://api.github.com/users/KiwiTrinsic/following{/other_user}","gists_url":"https://api.github.com/users/KiwiTrinsic/gists{/gist_id}","starred_url":"https://api.github.com/users/KiwiTrinsic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KiwiTrinsic/subscriptions","organizations_url":"https://api.github.com/users/KiwiTrinsic/orgs","repos_url":"https://api.github.com/users/KiwiTrinsic/repos","events_url":"https://api.github.com/users/KiwiTrinsic/events{/privacy}","received_events_url":"https://api.github.com/users/KiwiTrinsic/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/KiwiTrinsic/Cataclysm-DDA","description":"Cataclysm - Dark Days Ahead. A fork/variant of Cataclysm Roguelike by Whales.","fork":true,"url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA","forks_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/forks","keys_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/keys{/key_id}","collaborators_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/teams","hooks_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/hooks","issue_events_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/issues/events{/number}","events_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/events","assignees_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/assignees{/user}","branches_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/branches{/branch}","tags_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/tags","blobs_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/git/refs{/sha}","trees_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/git/trees{/sha}","statuses_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/statuses/{sha}","languages_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/languages","stargazers_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/stargazers","contributors_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/contributors","subscribers_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/subscribers","subscription_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/subscription","commits_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/commits{/sha}","git_commits_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/git/commits{/sha}","comments_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/comments{/number}","issue_comment_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/issues/comments{/number}","contents_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/contents/{+path}","compare_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/compare/{base}...{head}","merges_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/merges","archive_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/downloads","issues_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/issues{/number}","pulls_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/pulls{/number}","milestones_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/milestones{/number}","notifications_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/labels{/name}","releases_url":"https://api.github.com/repos/KiwiTrinsic/Cataclysm-DDA/releases{/id}","created_at":"2015-04-16T12:23:40Z","updated_at":"2015-04-16T12:23:46Z","pushed_at":"2015-04-19T16:42:16Z","git_url":"git://github.com/KiwiTrinsic/Cataclysm-DDA.git","ssh_url":"git@github.com:KiwiTrinsic/Cataclysm-DDA.git","clone_url":"https://github.com/KiwiTrinsic/Cataclysm-DDA.git","svn_url":"https://github.com/KiwiTrinsic/Cataclysm-DDA","homepage":"http://en.cataclysmdda.com/","size":409475,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"CleverRaven:master","ref":"master","sha":"f7c2af13a94d88b4cd6ab23068231987ce8e42b9","user":{"login":"CleverRaven","id":4367009,"avatar_url":"https://avatars.githubusercontent.com/u/4367009?v=3","gravatar_id":"","url":"https://api.github.com/users/CleverRaven","html_url":"https://github.com/CleverRaven","followers_url":"https://api.github.com/users/CleverRaven/followers","following_url":"https://api.github.com/users/CleverRaven/following{/other_user}","gists_url":"https://api.github.com/users/CleverRaven/gists{/gist_id}","starred_url":"https://api.github.com/users/CleverRaven/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CleverRaven/subscriptions","organizations_url":"https://api.github.com/users/CleverRaven/orgs","repos_url":"https://api.github.com/users/CleverRaven/repos","events_url":"https://api.github.com/users/CleverRaven/events{/privacy}","received_events_url":"https://api.github.com/users/CleverRaven/received_events","type":"Organization","site_admin":false},"repo":{"id":5973855,"name":"Cataclysm-DDA","full_name":"CleverRaven/Cataclysm-DDA","owner":{"login":"CleverRaven","id":4367009,"avatar_url":"https://avatars.githubusercontent.com/u/4367009?v=3","gravatar_id":"","url":"https://api.github.com/users/CleverRaven","html_url":"https://github.com/CleverRaven","followers_url":"https://api.github.com/users/CleverRaven/followers","following_url":"https://api.github.com/users/CleverRaven/following{/other_user}","gists_url":"https://api.github.com/users/CleverRaven/gists{/gist_id}","starred_url":"https://api.github.com/users/CleverRaven/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CleverRaven/subscriptions","organizations_url":"https://api.github.com/users/CleverRaven/orgs","repos_url":"https://api.github.com/users/CleverRaven/repos","events_url":"https://api.github.com/users/CleverRaven/events{/privacy}","received_events_url":"https://api.github.com/users/CleverRaven/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CleverRaven/Cataclysm-DDA","description":"Cataclysm - Dark Days Ahead. A fork/variant of Cataclysm Roguelike by Whales.","fork":false,"url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA","forks_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/forks","keys_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/teams","hooks_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/hooks","issue_events_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/events{/number}","events_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/events","assignees_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/assignees{/user}","branches_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/branches{/branch}","tags_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/tags","blobs_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/git/refs{/sha}","trees_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/statuses/{sha}","languages_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/languages","stargazers_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/stargazers","contributors_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/contributors","subscribers_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/subscribers","subscription_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/subscription","commits_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/commits{/sha}","git_commits_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/git/commits{/sha}","comments_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/comments{/number}","issue_comment_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/comments{/number}","contents_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/contents/{+path}","compare_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/merges","archive_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/downloads","issues_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues{/number}","pulls_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls{/number}","milestones_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/milestones{/number}","notifications_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/labels{/name}","releases_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/releases{/id}","created_at":"2012-09-26T22:57:43Z","updated_at":"2015-04-19T06:32:37Z","pushed_at":"2015-04-19T21:20:09Z","git_url":"git://github.com/CleverRaven/Cataclysm-DDA.git","ssh_url":"git@github.com:CleverRaven/Cataclysm-DDA.git","clone_url":"https://github.com/CleverRaven/Cataclysm-DDA.git","svn_url":"https://github.com/CleverRaven/Cataclysm-DDA","homepage":"http://en.cataclysmdda.com/","size":515700,"stargazers_count":641,"watchers_count":641,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":690,"mirror_url":null,"open_issues_count":926,"forks":690,"open_issues":926,"watchers":641,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12074"},"html":{"href":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12074"},"issue":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12074"},"comments":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12074/comments"},"review_comments":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12074/comments"},"review_comment":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12074/commits"},"statuses":{"href":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/statuses/cda592b47307fb65c43c22b8cefceae52ce8b521"}}}}