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