scimpl_private.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "scimpl_private.h"
  2. #include <util/generic/algorithm.h>
  3. #include <utility>
  4. namespace NSc {
  5. namespace NImpl {
  6. struct TGetKey {
  7. static inline TStringBuf Do(const TDict::value_type& v) {
  8. return v.first;
  9. }
  10. };
  11. struct TMoveValue {
  12. static inline TValue&& Do(TDict::value_type& v) {
  13. return std::move(v.second);
  14. }
  15. static inline TValue&& Do(TArray::value_type& v) {
  16. return std::move(v);
  17. }
  18. };
  19. template <typename TAction, typename TElement, typename TColl>
  20. static inline void PutToVector(TVector<TElement>& vector, TColl& coll) {
  21. size_t i = vector.size();
  22. vector.resize(vector.size() + coll.size());
  23. for (auto& item : coll) {
  24. vector[i++] = TAction::Do(item);
  25. }
  26. }
  27. bool TKeySortContext::Process(const TDict& self) {
  28. size_t oldSz = Vector.size();
  29. PutToVector<TGetKey>(Vector, self);
  30. Sort(Vector.begin() + oldSz, Vector.end());
  31. return true;
  32. }
  33. bool TSelfOverrideContext::Process(TValue::TScCore& self) {
  34. if (self.GetDict().size()) {
  35. PutToVector<TMoveValue>(Vector, self.Dict);
  36. } else if (self.GetArray().size()) {
  37. PutToVector<TMoveValue>(Vector, self.Array);
  38. }
  39. return true;
  40. }
  41. bool TSelfLoopContext::Process(const TValue::TScCore& self) {
  42. const bool ok = (Vector.end() == Find(Vector.begin(), Vector.end(), &self));
  43. if (!ok) {
  44. switch (ReportingMode) {
  45. case EMode::Assert:
  46. Y_ASSERT(false); // make sure the debug build sees this
  47. break;
  48. case EMode::Throw:
  49. ythrow TSchemeException() << "REFERENCE LOOP DETECTED";
  50. case EMode::Abort:
  51. Y_ABORT("REFERENCE LOOP DETECTED");
  52. break;
  53. case EMode::Stderr:
  54. Cerr << "REFERENCE LOOP DETECTED: " << JoinStrings(Vector.begin(), Vector.end(), ", ")
  55. << " AND " << ToString((const void*)&self) << Endl;
  56. break;
  57. }
  58. }
  59. Vector.push_back(&self);
  60. return ok;
  61. }
  62. }
  63. }