diff options
author | Evan Martin <martine@danga.com> | 2011-12-07 16:55:46 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2011-12-07 16:55:46 (GMT) |
commit | 5394fefc2bc297e3c7bc13d8c54823f34d82877c (patch) | |
tree | 3fe298bd44b2d4562d1245b1d8007dbe69cb751c /src | |
parent | c6144ccfe366b694bf034bdafa07e7c47ac8bf30 (diff) | |
download | Ninja-5394fefc2bc297e3c7bc13d8c54823f34d82877c.zip Ninja-5394fefc2bc297e3c7bc13d8c54823f34d82877c.tar.gz Ninja-5394fefc2bc297e3c7bc13d8c54823f34d82877c.tar.bz2 |
merge StatCache into State
I think I had originally imagined StatCache would contain more state,
but at this point it's clear it was just managing a single map, which
could just as well be in the already-small State object.
Diffstat (limited to 'src')
-rw-r--r-- | src/stat_cache.cc | 68 | ||||
-rw-r--r-- | src/stat_cache.h | 41 | ||||
-rw-r--r-- | src/state.cc | 42 | ||||
-rw-r--r-- | src/state.h | 16 |
4 files changed, 48 insertions, 119 deletions
diff --git a/src/stat_cache.cc b/src/stat_cache.cc deleted file mode 100644 index e414d41..0000000 --- a/src/stat_cache.cc +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2011 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "stat_cache.h" - -#include <stdio.h> - -#include "edit_distance.h" -#include "graph.h" - -Node* StatCache::GetFile(const std::string& path) { - Node* node = LookupFile(path); - if (node) - return node; - node = new Node(path); - paths_[node->path().c_str()] = node; - return node; -} - -Node* StatCache::LookupFile(const std::string& path) { - Paths::iterator i = paths_.find(path.c_str()); - if (i != paths_.end()) - return i->second; - return NULL; -} - -Node* StatCache::SpellcheckFile(const std::string& path) { - const bool kAllowReplacements = true; - const int kMaxValidEditDistance = 3; - - int min_distance = kMaxValidEditDistance + 1; - Node* result = NULL; - for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) { - int distance = EditDistance( - i->first, path, kAllowReplacements, kMaxValidEditDistance); - if (distance < min_distance && i->second) { - min_distance = distance; - result = i->second; - } - } - return result; -} - -void StatCache::Dump() { - for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) { - Node* node = i->second; - printf("%s %s\n", - node->path().c_str(), - node->status_known() ? (node->dirty() ? "dirty" : "clean") - : "unknown"); - } -} - -void StatCache::Invalidate() { - for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) - i->second->ResetState(); -} diff --git a/src/stat_cache.h b/src/stat_cache.h deleted file mode 100644 index b7d6e8a..0000000 --- a/src/stat_cache.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2011 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef NINJA_STAT_CACHE_H_ -#define NINJA_STAT_CACHE_H_ - -#include <string> -using namespace std; - -#include "hash_map.h" - -#include <string.h> - -struct Node; - -/// Mapping of path -> Node. -struct StatCache { - Node* GetFile(const string& path); - Node* LookupFile(const string& path); - Node* SpellcheckFile(const string& path); - - /// Dump the mapping to stdout (useful for debugging). - void Dump(); - void Invalidate(); - - typedef ExternalStringHashMap<Node*>::Type Paths; - Paths paths_; -}; - -#endif // NINJA_STAT_CACHE_H_ diff --git a/src/state.cc b/src/state.cc index bbb2f6a..1635f31 100644 --- a/src/state.cc +++ b/src/state.cc @@ -15,7 +15,9 @@ #include "state.h" #include <assert.h> +#include <stdio.h> +#include "edit_distance.h" #include "graph.h" #include "util.h" @@ -46,15 +48,36 @@ Edge* State::AddEdge(const Rule* rule) { } Node* State::GetNode(const string& path) { - return stat_cache_.GetFile(path); + Node* node = LookupNode(path); + if (node) + return node; + node = new Node(path); + paths_[node->path().c_str()] = node; + return node; } Node* State::LookupNode(const string& path) { - return stat_cache_.LookupFile(path); + Paths::iterator i = paths_.find(path.c_str()); + if (i != paths_.end()) + return i->second; + return NULL; } Node* State::SpellcheckNode(const string& path) { - return stat_cache_.SpellcheckFile(path); + const bool kAllowReplacements = true; + const int kMaxValidEditDistance = 3; + + int min_distance = kMaxValidEditDistance + 1; + Node* result = NULL; + for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) { + int distance = EditDistance( + i->first, path, kAllowReplacements, kMaxValidEditDistance); + if (distance < min_distance && i->second) { + min_distance = distance; + result = i->second; + } + } + return result; } void State::AddIn(Edge* edge, const string& path) { @@ -106,7 +129,18 @@ vector<Node*> State::DefaultNodes(string* err) { } void State::Reset() { - stat_cache_.Invalidate(); + for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) + i->second->ResetState(); for (vector<Edge*>::iterator e = edges_.begin(); e != edges_.end(); ++e) (*e)->outputs_ready_ = false; } + +void State::Dump() { + for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) { + Node* node = i->second; + printf("%s %s\n", + node->path().c_str(), + node->status_known() ? (node->dirty() ? "dirty" : "clean") + : "unknown"); + } +} diff --git a/src/state.h b/src/state.h index 1e2cd30..6b8d8b2 100644 --- a/src/state.h +++ b/src/state.h @@ -19,11 +19,10 @@ #include <map> #include <string> #include <vector> +using namespace std; #include "eval_env.h" -#include "stat_cache.h" - -using namespace std; +#include "hash_map.h" struct BuildLog; struct Edge; @@ -45,16 +44,21 @@ struct State { void AddIn(Edge* edge, const string& path); void AddOut(Edge* edge, const string& path); bool AddDefault(const string& path, string* error); + /// Reset state. Keeps all nodes and edges, but restores them to the + /// state where we haven't yet examined the disk for dirty state. void Reset(); + /// Dump the nodes (useful for debugging). + void Dump(); + /// @return the root node(s) of the graph. (Root nodes have no output edges). /// @param error where to write the error message if somethings went wrong. vector<Node*> RootNodes(string* error); vector<Node*> DefaultNodes(string* error); - StatCache* stat_cache() { return &stat_cache_; } - - StatCache stat_cache_; + /// Mapping of path -> Node. + typedef ExternalStringHashMap<Node*>::Type Paths; + Paths paths_; /// All the rules used in the graph. map<string, const Rule*> rules_; |